Skip to content

Follow the command run by pnpm dlx and pnpx - #2387

Merged
webpro merged 21 commits into
webpro-nl:mainfrom
everton-dgn:fix/pnpm-dlx-args
Nov 17, 2026
Merged

webpro merged 21 commits into
webpro-nl:mainfrom
everton-dgn:fix/pnpm-dlx-args

Conversation

@everton-dgn

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

Copy link
Copy Markdown
Contributor

With "typegen": "pnpx tsx scripts/typegen.ts" (kazupon/eslint-config), Knip reports scripts/typegen.ts as unused unless it's added to entry, as that knip.config.ts does. pnpm passes the arguments after the package on to its binary (dlx.ts), but resolveDlx dropped them. Knip now follows them, as it already does for npx and bunx, and removes only the dlx command word, so a pnpm dlx nested in the arguments is followed too. With -C or --dir they're left alone, since only recent pnpm runs the binary in that directory (pnpm/pnpm#17479).

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 4fa89fe

@everton-dgn
everton-dgn marked this pull request as ready for review November 16, 2026 07:18

@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! This discovers the wrapped scripts, but a few forwarding cases still produce incorrect results.

const dependencyInputs = packageSpecifier ? toDependencyInputs(packageSpecifier) : [];
return [...dependencyInputs, ...packages.flatMap(toDependencyInputs), ...command];
const forwarded =
packageSpecifier && !parsed.dir ? options.fromArgs(words.slice(parsed.positionalIndices![0])).slice(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.

Forwarding these Words through fromArgs drops the child's literal --. For pnpx node ./job.js -- --import unused-dep, Knip now reports unused-dep as unresolved, although Node receives it as a script argument. Please preserve the child's argument boundary.

@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 child's separator survives. Added pnpx node ./script.js -- --import unused-dep, which no longer yields unused-dep. A wrapped node -- ./script.js still misses the script, like the direct call; that part is in #2410.

const dependencyInputs = packageSpecifier ? toDependencyInputs(packageSpecifier) : [];
return [...dependencyInputs, ...packages.flatMap(toDependencyInputs), ...command];
const forwarded =
packageSpecifier && !parsed.dir ? options.fromArgs(words.slice(parsed.positionalIndices![0])).slice(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.

The parsed.dir guard also sees arguments belonging to the child. For example, pnpx tsx ./main.ts --dir output does not discover main.ts, although --dir output is passed to that script. Could you apply this guard only to pnpm's own options?

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.

pnpm's own options (--dir, --package, -c) are now read only from the words before the package, so --dir or -C passed to the child no longer stops forwarding.

const dependencyInputs = packageSpecifier ? toDependencyInputs(packageSpecifier) : [];
return [...dependencyInputs, ...packages.flatMap(toDependencyInputs), ...command];
const forwarded =
packageSpecifier && !parsed.dir ? options.fromArgs(words.slice(parsed.positionalIndices![0])).slice(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.

The unconditional .slice(1) assumes the child resolver emits its own binary first. For pnpx --package=pnpm pnpm exec tsx ./main.ts, the nested pnpm resolver returns tsx first, so this removes a required runner reference. Please only omit an input when it represents the outer command itself.

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.

The first forwarded input is now dropped only when it's the binary of the dlx package itself, so pnpx --package=pnpm pnpm exec tsx ./main.ts keeps tsx.

@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, this fixes all three issues from my previous review: the child separator, child option ownership and nested runner input.

Comment thread packages/knip/src/binaries/util.ts Outdated
if (arg !== '--') parts.push(arg);
} else if (arg.value !== '--') parts.push(arg.text);
if (arg !== '--' || parts.length > 0) parts.push(arg);
} else if (arg.value !== '--' || parts.length > 0) parts.push(arg.text);

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.

Keeping -- here changes existing c8 and cross-env handling too. With this PR applied to current main, c8 node -- ./job.js, c8 node -r ./preload.cjs -- ./job.js and cross-env MODE=test node -- ./job.js all report the executed job.js as unused; main keeps it used. Could you preserve that entry discovery while keeping arguments after the child's separator opaque, and add regression coverage for these wrapper cases?

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.

This comes from the same toScript change as #2361, #2362 and #2374, and #2410 replaces it: forwarding drops a child's -- as main did, and only keeps it after the child's operand or for a nested wrapper. I merged #2410 into this branch, so c8 node -- ./job.js, c8 node -r ./preload.cjs -- ./job.js and cross-env MODE=test node -- ./job.js keep job.js used again, while pnpx node ./job.js -- --import unused-dep still doesn't report unused-dep. Added those three cases as regressions.

@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.

All findings from my previous reviews are resolved, including the shared entry-discovery regression. LGTM!

@webpro
webpro merged commit 187de06 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