fix(cli): prevent informational logs from polluting json output#26264
fix(cli): prevent informational logs from polluting json output#26264
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where informational logs were polluting stdout when the CLI was running in non-interactive mode with JSON output enabled. By introducing early console redirection and a flexible event transformation mechanism during the backlog flushing process, the changes ensure that diagnostic messages are correctly routed to stderr, preserving the integrity of the JSON output stream. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Size Change: +890 B (0%) Total Size: 33.9 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Code Review
This pull request introduces console output redirection to stderr in headless mode and enhances the event backlog flushing mechanism to support event transformations. The initializeOutputListenersAndFlush function was updated to force output to stderr based on the CLI configuration, specifically for JSON output. Feedback suggests defaulting this behavior to true when the configuration is not yet initialized to prevent stdout pollution during early initialization failures.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements output redirection to stderr for headless mode and JSON output formats to prevent stdout pollution. Key changes include patching early console output, updating initializeOutputListenersAndFlush to handle configuration-based redirection, and enhancing the core event emitter's drainBacklogs method to support event transformation. A review comment identifies a logic issue where listener registration for console logs and user feedback is incorrectly dependent on the absence of an output listener, which could lead to lost information.
|
/gemini review |
65648ff to
5b9b813
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to redirect buffered output to stderr when the CLI is in headless mode or using JSON output format. This is achieved by enhancing the core event emitter's drainBacklogs method to support event transformation and updating the CLI initialization to utilize this feature. Feedback points out a critical Temporal Dead Zone (TDZ) risk where the config variable is captured by a cleanup closure before its declaration, potentially causing a ReferenceError. Additionally, manual patching of console methods was flagged as redundant and potentially disruptive to the existing ConsolePatcher logic.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to redirect output to stderr when the CLI is configured for JSON output or during early initialization failures. This is achieved by enhancing the drainBacklogs method in CoreEventEmitter to support an optional transformation function. The initializeOutputListenersAndFlush function in the CLI package now utilizes this transformation to force output chunks to stderr when necessary. New unit tests have been added to verify the redirection logic and the event transformation capabilities. I have no feedback to provide.
Summary
This PR prevents informational logs (e.g. MCP discovery messages, debug logs) from polluting the
stdoutstream when the CLI is running in non-interactive mode with JSON output.Details
The issue occurred because
patchStdio()starts buffering output very early, but the fullConsolePatcher(which redirectsconsole.logtostderr) is initialized later. Any logs during this window were captured asstdoutand then flushed to the realstdoutduring the pre-session drain.Key changes:
console.logandconsole.infoare shallow-patched to point toconsole.errorif in headless mode.coreEvents.drainBacklogsnow supports an optional transformer.initializeOutputListenersAndFlushuses this to force any bufferedstdoutitems tostderrif JSON output is active.gemini.tsxto manage theconfigvariable more reliably during the initialization and cleanup phases.Related Issues
Fixes #19198
How to Validate
console.log('STRAY')at the beginning ofmain()ingemini.tsx.npm run bundle.node bundle/gemini.js --prompt 'hi' --output-format json > out.json.out.jsonis valid JSON and does not contain 'STRAY', while 'STRAY' appears instderr.Pre-Merge Checklist