Repository navigation
Follow the command run by pnpm dlx and pnpx - #2387
Conversation
commit: |
webpro
left a comment
There was a problem hiding this comment.
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) : []; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) : []; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) : []; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thanks, this fixes all three issues from my previous review: the child separator, child option ownership and nested runner input.
| 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
# Conflicts: # packages/knip/src/binaries/util.ts
webpro
left a comment
There was a problem hiding this comment.
All findings from my previous reviews are resolved, including the shared entry-discovery regression. LGTM!
|
π 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. |
With
"typegen": "pnpx tsx scripts/typegen.ts"(kazupon/eslint-config), Knip reportsscripts/typegen.tsas unused unless it's added toentry, as that knip.config.ts does. pnpm passes the arguments after the package on to its binary (dlx.ts), butresolveDlxdropped them. Knip now follows them, as it already does fornpxandbunx, and removes only thedlxcommand word, so apnpm dlxnested in the arguments is followed too. With-Cor--dirthey're left alone, since only recent pnpm runs the binary in that directory (pnpm/pnpm#17479).