fix(cli): prevent subcommand shadowing and skip auth for commands#23177
fix(cli): prevent subcommand shadowing and skip auth for commands#23177mattKorwel merged 6 commits intomainfrom
Conversation
|
Size Change: +1.11 kB (0%) Total Size: 26.1 MB
ℹ️ View Unchanged
|
c5dc749 to
7c53de0
Compare
… subcommand shadowing
7c53de0 to
e80c2ce
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses two main issues: yargs command shadowing and a TypeError when accessing potentially undefined settings.admin properties. The command shadowing fix involves reordering yargs command registration, adding a middleware to detect subcommands, and using an isCommand flag to skip unnecessary authentication and sandboxing. The TypeError is resolved by adding optional chaining to settings access. The reviewer suggests an improvement for the isCommand middleware, recommending a dynamic approach to building the subcommand list instead of a hardcoded one to enhance maintainability.
Note: Security Review did not run due to the size of the PR.

Summary
This PR fixes an issue where the Gemini CLI's greedy default command (
$0 [query..]) shadowed administrative subcommands likemcp,extensions,skills, andhooks. This caused subcommands to be incorrectly treated as conversational queries, triggering unnecessary authentication checks and sandbox relaunches that would fail if aGEMINI_API_KEYwas missing.Details
packages/cli/src/config/config.ts, explicit subcommands are now registered before the default query command to ensure priority matching.isCommandMiddleware: Implemented a yargs middleware to explicitly flag when a management subcommand is matched viaargv['isCommand'] = true.packages/cli/src/gemini.tsxto skiprefreshAuthand sandbox relaunching whenargv.isCommandis true. This enables administrative tasks to run without an API key..check(),.fail(), and globaldebugoption that were inadvertently removed during recent refactoring, ensuring robust flag validation (e.g., preventing conflicting prompt flags).yargsto handle help and validation correctly even if the underlying feature is disabled in settings.Related Issues
Fixes #23173
How to Validate
gemini extensions listorgemini mcp listwithout aGEMINI_API_KEYenvironment variable. The command should execute successfully (showing an empty list or help) instead of failing with an authentication error.npm test -w @google/gemini-cli -- src/config/config.test.tsisCommand middlewaretest suite passes and that existing validation tests (like conflicting flags) are restored and passing.Pre-Merge Checklist