Skip to content

fix(npm): add 15 missing subcommands to prevent incorrect run injection - #2664

Closed
guyoron1 wants to merge 4 commits into
rtk-ai:developfrom
guyoron1:fix/npm-missing-subcommands
Closed

guyoron1 wants to merge 4 commits into
rtk-ai:developfrom
guyoron1:fix/npm-missing-subcommands

Conversation

@guyoron1

Copy link
Copy Markdown
Contributor

Summary

Fixes #2663

  • Add 15 missing official npm subcommands to NPM_SUBCOMMANDS list
  • Without these, RTK incorrectly injects run (e.g. npm query → npm run query → fails)
  • Add regression test for hyphenated subcommands (find-dupes, help-search, etc.)

Missing subcommands added: completion, edit, explore, find-dupes, help-search, hook, install-ci-test, install-test, ll, org, query, run-script, sbom, shrinkwrap, unstar

Verified against npm help output (npm 10.8.2).

Test plan

  • All existing npm_cmd tests pass
  • New test_hyphenated_subcommands_no_run_injection covers compound subcommands
  • cargo fmt && cargo clippy --all-targets && cargo test --all clean

@aeppling

aeppling commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Hey @guyoron1

Root-cause concern: the logic injects run for any unrecognized first arg, which makes NPM_SUBCOMMANDS a fail dangerous denylist. Issue is the default "run" prefix.
Even with this PR, npm's aliases still break, npm add, npm x, npm ln, npm un, npm s, so #2663 isn't fully closed, and every future npm release reopens it.

The self-maintaining fix: only inject run when the first arg is an actual script in the nearest package.json (walking up like npm does); otherwise pass through.

Should cause no regression keep your list additions: their job becomes builtin-precedence (so a query/sbom script can't shadow the real subcommand).

We can merge this but this is just a patch that does not fix the root issue.
Ball on your side for this, we can still merge this as a patch for now.

@aeppling aeppling self-assigned this Jul 2, 2026
@reneleonhardt

Copy link
Copy Markdown

Great review, thank you!
@guyoron1 could you add a corresponding test for npm run, and then fix it?

Comment thread src/cmds/js/npm_cmd.rs Outdated
}

fn args(strs: &[&str]) -> Vec<String> {
strs.iter().map(|s| s.to_string()).collect()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

More idiomatic:

strs.iter().map(String::from).collect()

Comment thread src/cmds/js/npm_cmd.rs Outdated
Comment on lines 113 to 118
if is_run_explicit || is_npm_subcommand {
effective_args.extend_from_slice(args);
} else {
// "rtk npm build" → "npm run build" (assume script name)
effective_args.push("run".to_string());
effective_args.extend_from_slice(args);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This could be simplified:

if !(is_run_explicit || is_npm_subcommand) {
    effective_args.push("run".to_string());
}
effective_args.extend_from_slice(args);

@guyoron1

guyoron1 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Heyaaaa :) thanks for the review!!

Pushed a fix that addresses both points:

  1. npm run test added — test_npm_run_no_double_injection verifies npm run build passes through without injecting a second run

  2. Missing aliases fixed — Added 11 short npm aliases (add, x, ln, un, r, s, se, rb, ddp, it, cit) that were still getting run injected

Also refactored the tests to call the actual production function (build_effective_npm_args()) instead of reimplementing the logic in local test helpers — follows the same pattern as dotnet_cmd.rs.

Comment thread src/cmds/js/npm_cmd.rs
// Only inject "run" when args look like a script name, not a known npm subcommand.
/// Build the effective npm args, injecting "run" when the first arg looks
/// like a script name rather than a known npm subcommand or flag.
fn build_effective_npm_args(args: &[String]) -> Vec<String> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

But this is still guessing, right?
Not actually walking up to the nearest package.json and extracting script keys like npm does.
Maybe something like this:

let script_names: Vec<String> = env::current_dir()
    .ok()
    .and_then(|p| p.ancestors().map(|a| a.join("package.json")).find(|f| f.exists()))
    .and_then(|f| File::open(f).ok())
    .and_then(|f| serde_json::from_reader(f).ok())
    .and_then(|v| v.get("scripts")?.as_object().map(|m| m.keys().cloned().collect()))
    .unwrap_or_default();

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.

Heyaaaa! Thanks for the review :)

Yes, it's still heuristic-based — the subcommand list covers npm's built-in commands, so anything not in the list gets run injected. Walking package.json for script keys would be more accurate but adds I/O on every invocation and complexity around monorepos / workspaces. Happy to explore that as a follow-up if there's appetite for it.

@reneleonhardt

Copy link
Copy Markdown

What's the source for NPM_SUBCOMMANDS?

When I diff it against npm help (npm 11.17.0 node 24.12.0), I get.

All commands

  • approve-scripts
  • deny-scripts
  • run
  • stage
  • trust
  • undeprecate

NPM_SUBCOMMANDS

  • bin
  • create
  • home
  • hook
  • info
  • list
  • remove
  • rm
  • run-script
  • show
  • up
  • why

(and i t aliases).

@aeppling

aeppling commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Hey @guyoron1 same update as for #2776

This is related to issues #2663 and #2094

Two related PR that both address a temporary fix :

The issue is at a higher level, and maintaining those hard coded lists is not a correct solution.

Proposed fix :

  • npm: forward the args untouched. npm build then fails exactly as it would without RTK, that's the agent's own command, and it self-corrects to npm run build. npm query, npm add, npm sbom, and anything npm adds in future just work. NPM_SUBCOMMANDS gets deleted.
  • lint: run the real pnpm lint (the actual script, with its real flags, chain, and exit code) and route its output through the lint filter, don't rebuild an eslint invocation. The invocation allowlist gets deleted.

This fixes both issues at the root, removes both lists (no more chasing npm versions or package-manager forms), and keeps every exit code faithful.

#2776 currently drops pnpm lint to raw with no filter (0% savings), running it verbatim and filtering the output preserves the gain.

…tion

Fixes rtk-ai#2663

Official npm subcommands like `query`, `sbom`, `completion`, `edit`,
`explore`, `find-dupes`, `help-search`, `hook`, `install-ci-test`,
`install-test`, `ll`, `org`, `run-script`, `shrinkwrap`, and `unstar`
were missing from NPM_SUBCOMMANDS. Running any of these through RTK
would incorrectly inject `run`, e.g. `npm query` became `npm run query`.

Verified against `npm help` output (npm 10.8.2). Added regression test
for hyphenated subcommands which are the most likely to be missed.
- Extract `build_effective_npm_args()` so tests exercise the actual
  production function instead of reimplementing the logic locally
- Add 11 missing npm short aliases (add, x, ln, un, r, s, se, rb,
  ddp, it, cit) that were causing incorrect `run` injection
- Add explicit `npm run` test (no double injection)
- Remove redundant `test_hyphenated_subcommands_no_run_injection`
  (already covered by iterating all NPM_SUBCOMMANDS)
- Add 6 npm 11 commands: approve-scripts, deny-scripts, run, stage,
  trust, undeprecate
- Sort NPM_SUBCOMMANDS alphabetically with clear sections: primary
  commands, npm-10 aliases kept for compat, short aliases
- Add test_npm11_commands_no_run_injection for new commands
- Document source as union of `npm help` across npm 10 and 11
@guyoron1
guyoron1 force-pushed the fix/npm-missing-subcommands branch from 72e3bb7 to c86c241 Compare August 14, 2026 19:03
@guyoron1

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #2860. The 15 subcommands here landed separately in #3087, and the aliases I added afterwards (add, x, ln, un, r, s, se, rb, ddp, it, cit) are covered there too: #2860 deletes NPM_SUBCOMMANDS outright and forwards npm's argv untouched, so every alias works, plus the npm 11 commands @reneleonhardt listed (approve-scripts, deny-scripts, stage, trust, undeprecate) that this PR never added. Checked against a stand-in npm on both builds: develop still injects run for 16 of them, #2860 for none.

Thanks @aeppling and @reneleonhardt for pushing this towards the root cause.

@guyoron1 guyoron1 closed this Sep 21, 2026
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.

Bug: npm query, npm sbom, and 13 other subcommands get incorrect run injection

3 participants