fix: stop dependencies from logging to stdout when in tui mode
This commit is contained in:
parent
2f45640211
commit
7a18f37a9e
5 changed files with 135 additions and 119 deletions
|
|
@ -10,6 +10,7 @@ import {
|
|||
getHistoryForLLM,
|
||||
} from "../compaction.js";
|
||||
import { processCommandFlags } from "../flags/flagProcessor.js";
|
||||
import { safeStdout } from "../init.js";
|
||||
import { configureLogger } from "../logger.js";
|
||||
import * as logging from "../logging.js";
|
||||
import { sentryService } from "../sentry.js";
|
||||
|
|
@ -22,7 +23,6 @@ import { constructSystemMessage } from "../systemMessage.js";
|
|||
import { posthogService } from "../telemetry/posthogService.js";
|
||||
import { telemetryService } from "../telemetry/telemetryService.js";
|
||||
import { startTUIChat } from "../ui/index.js";
|
||||
import { safeStdout } from "../util/consoleOverride.js";
|
||||
import { formatAnthropicError, formatError } from "../util/formatError.js";
|
||||
import { logger } from "../util/logger.js";
|
||||
import {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// MUST be the first import - intercepts console/stdout/stderr before any dependencies load
|
||||
import "./init.js";
|
||||
|
||||
import { Command } from "commander";
|
||||
|
||||
import { chat } from "./commands/chat.js";
|
||||
|
|
@ -10,12 +13,12 @@ import { remoteTest } from "./commands/remote-test.js";
|
|||
import { remote } from "./commands/remote.js";
|
||||
import { serve } from "./commands/serve.js";
|
||||
import {
|
||||
validateFlags,
|
||||
handleValidationErrors,
|
||||
validateFlags,
|
||||
} from "./flags/flagValidator.js";
|
||||
import { configureConsoleForHeadless } from "./init.js";
|
||||
import { sentryService } from "./sentry.js";
|
||||
import { addCommonOptions, mergeParentOptions } from "./shared-options.js";
|
||||
import { configureConsoleForHeadless } from "./util/consoleOverride.js";
|
||||
import { logger } from "./util/logger.js";
|
||||
import { readStdinSync } from "./util/stdin.js";
|
||||
import { getVersion } from "./version.js";
|
||||
|
|
|
|||
128
extensions/cli/src/init.ts
Normal file
128
extensions/cli/src/init.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* Early initialization that must happen before ANY other imports
|
||||
* This file intercepts console and stdout/stderr to prevent dependency logging
|
||||
*/
|
||||
|
||||
// Check if we're in headless mode by looking at process arguments
|
||||
// We need to do this before any imports to catch early logging
|
||||
const args = process.argv.slice(2);
|
||||
const isHeadlessMode = args.includes("-p") || args.includes("--print");
|
||||
|
||||
// Store original methods before ANY dependencies can use them
|
||||
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
||||
const originalConsole = {
|
||||
log: console.log,
|
||||
info: console.info,
|
||||
warn: console.warn,
|
||||
error: console.error,
|
||||
debug: console.debug,
|
||||
trace: console.trace,
|
||||
table: console.table,
|
||||
dir: console.dir,
|
||||
dirxml: console.dirxml,
|
||||
group: console.group,
|
||||
groupEnd: console.groupEnd,
|
||||
groupCollapsed: console.groupCollapsed,
|
||||
time: console.time,
|
||||
timeEnd: console.timeEnd,
|
||||
timeLog: console.timeLog,
|
||||
clear: console.clear,
|
||||
count: console.count,
|
||||
countReset: console.countReset,
|
||||
assert: console.assert,
|
||||
};
|
||||
|
||||
// Track if we're inside our own code vs dependency code
|
||||
let isHeadlessModeConfigured = false;
|
||||
|
||||
// Override console methods if in headless mode
|
||||
if (isHeadlessMode) {
|
||||
// Override ALL console methods to no-ops
|
||||
Object.keys(originalConsole).forEach((method) => {
|
||||
(console as any)[method] = () => {};
|
||||
});
|
||||
|
||||
// Override stdout/stderr to block everything by default
|
||||
process.stdout.write = function (
|
||||
chunk: any,
|
||||
encoding?: any,
|
||||
callback?: any,
|
||||
): boolean {
|
||||
// Always return true to indicate success, but don't actually write
|
||||
if (typeof encoding === "function") {
|
||||
encoding();
|
||||
return true;
|
||||
}
|
||||
if (typeof callback === "function") {
|
||||
callback();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
process.stderr.write = function (
|
||||
chunk: any,
|
||||
encoding?: any,
|
||||
callback?: any,
|
||||
): boolean {
|
||||
// Always return true to indicate success, but don't actually write
|
||||
if (typeof encoding === "function") {
|
||||
encoding();
|
||||
return true;
|
||||
}
|
||||
if (typeof callback === "function") {
|
||||
callback();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure console for headless mode
|
||||
* Since init.ts already blocks everything, this just tracks state
|
||||
* and provides the safe output functions
|
||||
*/
|
||||
export function configureConsoleForHeadless(headless: boolean): void {
|
||||
isHeadlessModeConfigured = headless;
|
||||
|
||||
// init.ts should have already handled the blocking if in headless mode
|
||||
// This function now mainly exists for compatibility and state tracking
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe stdout output that bypasses all overrides
|
||||
* Use this for intentional output in headless mode
|
||||
*/
|
||||
export function safeStdout(message: string): void {
|
||||
if (isHeadlessMode || isHeadlessModeConfigured) {
|
||||
// Use the original stdout that was saved before any overrides
|
||||
originalStdoutWrite(message);
|
||||
} else {
|
||||
// In non-headless mode, just use normal stdout
|
||||
process.stdout.write(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe stderr output that bypasses all overrides
|
||||
* Use this for error messages that should always be visible
|
||||
*/
|
||||
export function safeStderr(message: string): void {
|
||||
if (isHeadlessMode || isHeadlessModeConfigured) {
|
||||
// Use the original stderr that was saved before any overrides
|
||||
originalStderrWrite(message);
|
||||
} else {
|
||||
// In non-headless mode, just use normal stderr
|
||||
process.stderr.write(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset console overrides to original state
|
||||
* Note: This won't affect init.ts overrides which happen at module load
|
||||
*/
|
||||
export function resetConsoleOverrides(): void {
|
||||
isHeadlessModeConfigured = false;
|
||||
// The actual console restoration would need to happen in init.ts
|
||||
// This is mainly for testing purposes
|
||||
}
|
||||
|
|
@ -1,115 +0,0 @@
|
|||
/**
|
||||
* Console override utility to prevent unwanted output in headless mode
|
||||
* This ensures that only intentional stdout output reaches the user
|
||||
*/
|
||||
|
||||
interface ConsoleBackup {
|
||||
log: typeof console.log;
|
||||
info: typeof console.info;
|
||||
warn: typeof console.warn;
|
||||
error: typeof console.error;
|
||||
debug: typeof console.debug;
|
||||
}
|
||||
|
||||
let originalConsole: ConsoleBackup | null = null;
|
||||
let isHeadlessMode = false;
|
||||
|
||||
/**
|
||||
* Override console methods to prevent unwanted output in headless mode
|
||||
* In headless mode, only console.error and explicitly allowed outputs should reach stdout
|
||||
*/
|
||||
export function configureConsoleForHeadless(headless: boolean): void {
|
||||
isHeadlessMode = headless;
|
||||
|
||||
if (headless && !originalConsole) {
|
||||
// Backup original console methods
|
||||
originalConsole = {
|
||||
log: console.log,
|
||||
info: console.info,
|
||||
warn: console.warn,
|
||||
error: console.error,
|
||||
debug: console.debug,
|
||||
};
|
||||
|
||||
// Override console methods to suppress output in headless mode
|
||||
console.log = () => {}; // Suppress all console.log
|
||||
console.info = () => {}; // Suppress all console.info
|
||||
console.warn = () => {}; // Suppress all console.warn
|
||||
console.debug = () => {}; // Suppress all console.debug
|
||||
|
||||
// Keep console.error for genuine errors, but send to stderr
|
||||
console.error = (...args: any[]) => {
|
||||
originalConsole!.error(...args);
|
||||
};
|
||||
} else if (!headless && originalConsole) {
|
||||
// Restore original console methods
|
||||
console.log = originalConsole.log;
|
||||
console.info = originalConsole.info;
|
||||
console.warn = originalConsole.warn;
|
||||
console.error = originalConsole.error;
|
||||
console.debug = originalConsole.debug;
|
||||
|
||||
originalConsole = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe stdout output that bypasses console overrides
|
||||
* Use this for intentional output in headless mode
|
||||
*/
|
||||
export function safeStdout(message: string): void {
|
||||
process.stdout.write(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe stderr output that bypasses console overrides
|
||||
* Use this for error messages that should always be visible
|
||||
*/
|
||||
export function safeStderr(message: string): void {
|
||||
process.stderr.write(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporarily restore console for a specific operation
|
||||
* Useful for debugging or when you need original console behavior
|
||||
*/
|
||||
export function withOriginalConsole<T>(fn: () => T): T {
|
||||
if (!originalConsole || !isHeadlessMode) {
|
||||
return fn();
|
||||
}
|
||||
|
||||
// Temporarily restore
|
||||
const tempConsole = { ...console };
|
||||
Object.assign(console, originalConsole);
|
||||
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
// Restore overrides
|
||||
Object.assign(console, tempConsole);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if currently in headless mode
|
||||
*/
|
||||
export function isInHeadlessMode(): boolean {
|
||||
return isHeadlessMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset console overrides to original state
|
||||
* Useful for testing to ensure clean state
|
||||
*/
|
||||
export function resetConsoleOverrides(): void {
|
||||
if (originalConsole) {
|
||||
console.log = originalConsole.log;
|
||||
console.info = originalConsole.info;
|
||||
console.warn = originalConsole.warn;
|
||||
console.error = originalConsole.error;
|
||||
console.debug = originalConsole.debug;
|
||||
|
||||
originalConsole = null;
|
||||
}
|
||||
isHeadlessMode = false;
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { vi } from "vitest";
|
||||
|
||||
import { resetConsoleOverrides } from "./src/util/consoleOverride.js";
|
||||
import { resetConsoleOverrides } from "./src/init.js";
|
||||
|
||||
// Disable telemetry for tests
|
||||
process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0";
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue