Skip to content

fix(command): don't mutate stored options when registering#10715

Open
kyungseopk1m wants to merge 3 commits into
firebase:mainfrom
kyungseopk1m:fix/command-options-no-mutate
Open

fix(command): don't mutate stored options when registering#10715
kyungseopk1m wants to merge 3 commits into
firebase:mainfrom
kyungseopk1m:fix/command-options-no-mutate

Conversation

@kyungseopk1m

Copy link
Copy Markdown

register() used args.shift(), which mutated the stored option arrays in place. Registering the same command instance a second time (e.g. when firebase-tools is imported as a module across multiple invocations) then passed undefined to commander and threw Cannot read properties of undefined (reading 'indexOf').

Switched to destructuring so the stored options aren't modified, and added a regression test that registers a command multiple times.

This is the small fix that was split out of #9935.

Fixes #9929

register() used args.shift() on the stored option arrays, so registering
the same command instance more than once (e.g. when firebase-tools is
imported as a module across multiple invocations) lost the flags and threw
"Cannot read properties of undefined (reading 'indexOf')". Use destructuring
so the stored options aren't modified.

Fixes firebase#9929

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue where registering a command multiple times mutated its stored options array by shifting the flags out of it. This was resolved by destructuring the options array in src/command.ts instead of using shift(). A corresponding unit test was added in src/command.spec.ts. The review feedback recommends avoiding the use of as unknown type assertions in the new test file to comply with the repository style guide, suggesting proper mock definitions and using Reflect.get to access private properties instead.

Comment thread src/command.spec.ts Outdated
Comment thread src/command.spec.ts

@IzaakGough IzaakGough left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@7781a66). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10715   +/-   ##
=======================================
  Coverage        ?   57.88%           
=======================================
  Files           ?      609           
  Lines           ?    39226           
  Branches        ?     7867           
=======================================
  Hits            ?    22707           
  Misses          ?    14693           
  Partials        ?     1826           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

Command.js mutates the options array

4 participants