Skip to content

fix: dynamically locate command path in argv for flags() offset#9295

Draft
reggi wants to merge 1 commit intolatestfrom
reggi/flags-fix
Draft

fix: dynamically locate command path in argv for flags() offset#9295
reggi wants to merge 1 commit intolatestfrom
reggi/flags-fix

Conversation

@reggi
Copy link
Copy Markdown
Contributor

@reggi reggi commented May 2, 2026

Summary

When global flags like --registry appear before the command name in argv (e.g., npm --registry http://... trust list), the subcommand parser miscalculated the offset into argv. It used a fixed 2 + depth offset assuming commands always start at position 2, but global flags push command names to higher positions. This caused subcommand names to be treated as unknown positional arguments.

Changes

  • lib/base-cmd.js: Changed flags(depth) to flags(commandPath) and added #findCommandOffset() which searches for the full contiguous command path sequence in config.argv, correctly skipping any preceding global flags.
  • lib/npm.js: Updated call site to pass the commandPath array instead of commandPath.length.
  • test/lib/base-cmd.js: Added 3 new test cases:
    • Global flags before a top-level command name
    • Global flags before subcommand names (depth 2)
    • Command name collision (same string appears as both flag value and command name)

Global flags before the command name (e.g., npm --registry url trust list)
shifted command positions in argv, causing flags() to miscalculate the
nopt offset with its fixed 2+depth formula. Now flags() accepts the full
commandPath array and searches for the contiguous command sequence in
argv, correctly skipping any preceding global flags.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@reggi reggi requested a review from a team as a code owner May 2, 2026 02:34
@reggi reggi marked this pull request as draft May 4, 2026 16:58
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.

1 participant