Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint-staged stringifies my commands, changing their meaning (and breaking them) #539

Closed
trusktr opened this issue Nov 9, 2018 · 19 comments

Comments

@trusktr
Copy link

trusktr commented Nov 9, 2018

For example, I have the following in package.json:

	"scripts": {
		"typecheck": "tsc -p ./tsconfig.json --noEmit && :",
		"dev-or-master": "git branch | grep \\* | cut -d ' ' -f2 | grep -E 'develop|master' > /dev/null"
	},
	"lint-staged": {
		"*.{ts,tsx}": [
			"npm run dev-or-master && npm run typecheck || :"
		],
	},

and it doesn't work because lint-staged converts into this:

git branch | grep \* | cut -d ' ' -f2 | grep -E 'develop|master' > /dev/null "&&" "npm" "run" "typecheck" "||" ":" "/Users/trusktr/src/Signafy+mapper-annotator/src/annotator/App.tsx"

As you can see there, some parts of the command are wrapped with "", which causes those parts to be treated as arguments rather than interpreted by the shell.

It would be nice if you could fix this. I've already ran into in a few times, and as a result have to find some other way to write it.

@trusktr
Copy link
Author

trusktr commented Nov 9, 2018

I think I see what the problem is: lint-staged is not handling npm run commands in the same way that npm actually handles them.

In npm, args can only be passed in by explicitly using --. For example, to pass args to a command using npm:

npm run something -- arg1 arg2

The -- is required there, in order for arg1 and arg2 to be passed into the something command as arguments.

However, it seems that lint-staged is assuming it works like this:

npm run something arg1 arg2

without requiring the --, and plus it is not actually passing args, but simply concatenating all the parts into a strings, so it just simply won't work.

@trusktr
Copy link
Author

trusktr commented Nov 9, 2018

Just an idea, but instead of adding "", maybe you can just make the whole thing a string and run it in a subshell? My guess npm run does something like this.

@okonet
Copy link
Collaborator

okonet commented Nov 9, 2018

Yeah thanks. That’s a good catch and we should fix it

@mAAdhaTTah
Copy link

This is messing up trying to integrate sed into lint-staged, as the -i '' part of the command gets wrapped in "" and thus always creates a backup with '' as the extension.

@raajnadar
Copy link

I think no one noticed this git branch | grep \\* the \ got escaped I am facing a serious issue with JEST test.

D:\native\react-native this is the original path but in the cmd the \n is converted into line break like this

D:
ative\react-native-paper

@okonet
Copy link
Collaborator

okonet commented Nov 16, 2018

Please come up with a fix and submit a PR

@raajnadar
Copy link

raajnadar commented Nov 16, 2018

@okonet in which file I can find the JEST testing code?? Any idea..! I went through each file but it is hard to understand. This issue looks very much similar #486

@okonet
Copy link
Collaborator

okonet commented Nov 16, 2018

Tests are located in /tests

@okonet
Copy link
Collaborator

okonet commented Dec 20, 2018

Does anyone want to take this one?

@okonet okonet unpinned this issue Dec 20, 2018
@okonet okonet pinned this issue Dec 20, 2018
@mogafk
Copy link

mogafk commented Dec 20, 2018

now i think only about one solving. It split cmd string by && and || and then use something like reverse polsky notation(in case with ()). And then execute command-by-command. But it sophisticated and overkill. Any another suggestions?

@okonet
Copy link
Collaborator

okonet commented Dec 22, 2018

lint-staged is not handling npm run commands in the same way that npm actually handles them.

We removed this handling in one of the breaking releases so it's now should be done explicitly AFAIK.

I'm also not quite sure what's going on since we're not escaping anything and just passing arguments to execa. So it might be an issue in execa?

We're using https://github.com/mccormicka/string-argv to parse arguments. Probably it has an issue? We'd need a better test suite for all corner cases in order to fix it. Anyone a PR?

@okonet
Copy link
Collaborator

okonet commented Dec 22, 2018

@iiroj
Copy link
Member

iiroj commented Jun 25, 2019

Now that execa v2.0.0 has been released with support for execa.command, this should be simple to fix. I’ll incorporate it in #639 if it works.

@okonet
Copy link
Collaborator

okonet commented Jun 25, 2019

Be aware of the fact we rely on this to be true:

Default preferLocal option to false. If you are executing locally installed binaries, you'll need to manually specify preferLocal: true (#314)

@iiroj
Copy link
Member

iiroj commented Jun 26, 2019

I think there's something wrong with using preferLocal: true with execa v2.0.0 (sindresorhus/execa#196), so it no longer works at all. I won't touch this now because it wasn't so simple...

@iiroj
Copy link
Member

iiroj commented Jun 26, 2019

We could try with execa's shell: true option, so that the command is executed in a shell environment. This might help, but introduces other issues so it would be nice to be behind a config option in lint-staged as well.

@iiroj iiroj unpinned this issue Jun 28, 2019
@iiroj iiroj pinned this issue Jun 28, 2019
@okonet
Copy link
Collaborator

okonet commented Jul 1, 2019

Can we verify and close this?

@iiroj
Copy link
Member

iiroj commented Jul 2, 2019

According to #640 (comment), at least the command parsing has been improved somewhat.

If @trusktr can verify that it now works, we can close this issue.

@iiroj
Copy link
Member

iiroj commented Jul 21, 2019

I'll close this due to inactivity. @trusktr if it still doesn't work, let's reopen and investigate.

@iiroj iiroj closed this as completed Jul 21, 2019
@iiroj iiroj unpinned this issue Jul 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants