Skip to content

Follow the shell command wrapped by cross-env-shell - #2362

Merged
webpro merged 22 commits into
webpro-nl:mainfrom
everton-dgn:fix/cross-env-shell-command
Nov 17, 2026
Merged

webpro merged 22 commits into
webpro-nl:mainfrom
everton-dgn:fix/cross-env-shell-command

Conversation

@everton-dgn

@everton-dgn everton-dgn commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

With cross-env-shell TZ=UTC jest (example), jest is reported as an unused devDependency. The quoted form is common too, as in cross-env-shell PLATFORM=web "tsc && vite build" (example), and nothing inside the quotes was followed.

cross-env-shell hands its arguments to a shell (source), and everything after the leading NAME=value pairs is the command (parser). getWrappedCommand now passes that command on as script text, so getInputsFromScripts and getScriptCommands both see what's inside the quotes.

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2362
npm i https://pkg.pr.new/@knip/language-server@2362
npm i https://pkg.pr.new/@knip/mcp@2362

commit: 539e27c

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Éverton! The shell-command support works, but the unquoted form needs to preserve literal argument separators.

Comment thread packages/knip/src/binaries/plugins.ts Outdated
for (const arg of command) {
const value = typeof arg === 'string' ? arg : arg.value;
if (!isEnvAssignment.test(value)) isCommand = true;
script.push(isCommand ? value : typeof arg === 'string' ? arg : arg.text.replace(quotedEnvAssignment, '$2$1'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For cross-env-shell NODE_ENV=test node ./job.js -- --import unused-dep, this returns -- as a separate script element, which toScript removes. Native execution preserves it as a script argument, but Knip now reports unused-dep as an unresolved module. getScriptCommands loses the separator too.

Could you preserve it for unquoted commands in both dependency extraction and command inspection, with a regression test? Quoting the entire command already works.

@everton-dgn everton-dgn Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toScript now only drops a leading --, so the separator survives for unquoted commands in both dependency extraction and getScriptCommands. Added a regression for each. The script in cross-env-shell node -- ./script.js isn't picked up yet, which #2410 covers for node -- in general.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Éverton! I pushed a commit that replaces the cross-env-shell step in getWrappedCommand: a fromArgs function can now return a string to have it parsed as shell script text, and cross-env-shell's args re-emit the leading NAME=value arguments as single-quoted assignments before the command. NODE_OPTIONS modules and quoted assignments keep working without the quote rewrite, and plugins can use the same return value, like env-cmd for --use-shell in #2374. Happy to merge.

@webpro
webpro merged commit fcd8c43 into webpro-nl:main Nov 17, 2026
33 checks passed
@webpro

webpro commented Nov 18, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.46.0. See Release 6.46.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants