Skip to content

fix(vscode): stop DEBUG leaking into the CLI version check - #11385

Open
abhiramshaji-cyber wants to merge 2 commits into
firebase:mainfrom
abhiramshaji-cyber:fix-vscode-cli-version-debug
Open

abhiramshaji-cyber wants to merge 2 commits into
firebase:mainfrom
abhiramshaji-cyber:fix-vscode-cli-version-debug

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown

Description

The VS Code extension read a year from the CLI's debug log as the installed CLI version, so its outdated CLI warning never showed; now it reads the real version.

What this code is for

When the extension starts, activate() calls logSetup() to set up logging and then checkCLIInstallation(). That check runs firebase --version and npm --version in a child process, compares the result with the latest tag from the npm registry, and shows a warning if the CLI is missing or older than latest.

Before this PR

A developer with firebase-tools 15.33.0 installed opens VS Code with the extension.

  1. logSetup() runs process.env.DEBUG = "true" so that useConsoleLoggers() adds the debug console logger.
  2. checkCLIInstallation() builds env = { ...process.env, VSCODE_CWD: "" }, so DEBUG=true goes into the child process.
  3. firebase --version sees DEBUG and prints its debug banner first, lines like [2026-10-24T18:16:05.021Z] CLI Version: 15.33.0, then 15.33.0 on the last line.
  4. semver.valid() on the whole output returns null, so semver.coerce() takes the first number it finds: the year in the first timestamp.
  5. The extension reads the version as 2026.0.0, which is never older than latest, so the outdated CLI warning can never show. (On the released 2.4.3, which has no coerce(), the same output shows "The Firebase CLI is not installed" on every start.)

After this PR

Same developer, same start.

  1. logSetup() calls useConsoleLoggers(true). The debug console logger is added as before, and process.env.DEBUG is left alone.
  2. firebase --version no longer gets DEBUG from the extension, so it prints 15.33.0.
  3. parseVersionOutput() splits the output into lines and keeps the last line that semver.valid() accepts as a whole version.
  4. If DEBUG still arrives some other way, for example from the user's shell profile through the -l login shell, the banner lines are not whole versions and are skipped; the 15.33.0 line is still found.
  5. The extension reads 15.33.0, and shows the outdated CLI warning when latest is newer.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: version read as 2026.0.0"]
direction TB
  B1["developer opens VS Code, CLI 15.33.0"] --> B2["logSetup sets process.env.DEBUG"]
  B2 --> B3["firebase --version prints banner, then 15.33.0"]
  B3 --> B4{"semver.coerce on the whole output"}
  B4 -->|"first number is the year"| B5["2026.0.0, warning never shows"]
  B4 -->|"output is only a version"| B6["version read correctly"]
end

subgraph AFTER["AFTER: version read as 15.33.0"]
direction TB
  A1["developer opens VS Code, CLI 15.33.0"] --> A2["logSetup calls useConsoleLoggers(true)"]
  A2 --> A3["firebase --version prints 15.33.0"]
  A3 --> A4{"THE CHANGE: keep the line that is a whole version"}
  A4 -->|"banner lines skipped"| A5["15.33.0, outdated warning works"]
  A4 -->|"no version line at all"| A6["CLI not installed warning, as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

The fix for the reported bug: logSetup() in firebase-vscode/src/logger-wrapper.ts no longer sets process.env.DEBUG. To keep the debug console logger, useConsoleLoggers() in src/logger.ts now takes debug = !!process.env.DEBUG as an argument. The CLI calls it with no argument, so the CLI behaves exactly as before.

The second check: checkCLIInstallation() now passes both the firebase --version and npm --version output to a small exported parseVersionOutput(), which returns the last line that is a whole version and undefined when there is none. This replaces semver.valid(out) || semver.coerce(out)?.version, so the update notices that #10874 handled are still covered (they are never a whole version on their own line), and a timestamp can no longer be read as a version.

Many thanks to @vgerasymenko for the very clear report and root cause, and to @aalej for the minimal reproduction and for inviting a PR. vgerasymenko suggested two options: delete DEBUG from the env of the spawned check, or keep the logging flag out of process.env. I went with the second one because every child process the extension starts inherits process.env, not only this check, and I added the stricter line parsing because the login shell can still bring DEBUG in from a user's profile. I am very happy to be corrected on either: if you would rather keep src/logger.ts untouched and only delete DEBUG from this one spawn, or drop the parsing change, I will gladly change it. What do you think fits the extension best?

What changes for a user

  • The extension reads the installed CLI version correctly, and the outdated CLI warning shows when the CLI is behind latest.
  • The "CLI not installed" warning still shows when firebase --version fails or prints no version.
  • The extension's debug output in the VS Code console and in vsce-debug.log is unchanged.
  • Child processes started by the extension, such as npm --version and firebase --tool:setup-check, no longer get DEBUG=true from the extension.
  • The CLI's own --debug and DEBUG handling is unchanged.

Scenarios Tested

  • Built the CLI from main and ran DEBUG=true node lib/bin/firebase.js --version: the old parsing read 2026.0.0; parseVersionOutput() on the same output reads 15.34.2.
  • New tests in firebase-vscode/src/test/suite/src/extension.test.ts cover a bare version, the debug banner, Windows line endings, text after the version, and output with no version, plus a check that logSetup() leaves DEBUG unset. With the old code, 4 of these fail; with this change the VS Code unit suite passes (19 passing).
  • New tests in src/logger.spec.ts for useConsoleLoggers(); 2 of them fail without the change, and all pass with it.
  • npm run lint and npm run build in firebase-vscode, and tsc --noEmit at the root, with no new warnings.

Sample Commands

None, no command or flag changes.

Closes #11363

logSetup() set process.env.DEBUG = "true" only to turn on the CLI's
console logger, but every child process the extension starts inherits
process.env. The spawned `firebase --version` then printed its debug
banner before the version, so semver.valid() saw no version and the
semver.coerce() fallback picked up the year of the first timestamp
(2026.0.0). The outdated CLI warning could never fire.

useConsoleLoggers() now takes the debug flag as an argument that
defaults to DEBUG, so the CLI behaves as before while the extension
asks for debug output without touching the environment.

The version check also reads the line of output that is exactly a
version instead of coercing the first number it finds. The login
shell can still bring in DEBUG or other text from a user's profile,
and only a line that is a whole version is the CLI's answer.

Fixes firebase#11363

@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 improves Firebase CLI version parsing by introducing a robust parseVersionOutput helper that correctly handles debug banners and Windows line endings, and updates useConsoleLoggers to accept an optional debug parameter to avoid polluting process.env.DEBUG. It also adds comprehensive unit tests for these changes. The review feedback suggests aligning the test code with the repository style guide by avoiding any or unknown type assertions, importing Transport from winston-transport, and replacing .forEach loops with for..of loops when cleaning up logger transports.

Comment thread src/logger.spec.ts Resolved
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Thanks for the review notes. All four are addressed in 90da488, and the logger spec plus the VS Code unit suite pass locally. Happy to change anything the maintainers would prefer.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi team, hope you're doing well! Just a gentle check in on this one, no rush at all. The workflow run is still waiting for approval, and I'm happy to merge in the latest main whenever that helps. If you'd like the fix to look different, or have any tips, I'd love to hear them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant