move stdio (#13528)

This commit is contained in:
Jacob Richman
2025-11-20 14:16:46 -08:00
committed by GitHub
parent f92e79eba0
commit fec0eba07e
9 changed files with 51 additions and 56 deletions

View File

@@ -40,6 +40,28 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
return {
...actual,
recordSlowRender: vi.fn(),
writeToStdout: vi.fn((...args) =>
process.stdout.write(
...(args as Parameters<typeof process.stdout.write>),
),
),
patchStdio: vi.fn(() => () => {}),
createInkStdio: vi.fn(() => ({
stdout: {
write: vi.fn((...args) =>
process.stdout.write(
...(args as Parameters<typeof process.stdout.write>),
),
),
columns: 80,
rows: 24,
on: vi.fn(),
removeListener: vi.fn(),
},
stderr: {
write: vi.fn(),
},
})),
};
});
@@ -149,35 +171,6 @@ vi.mock('./ui/utils/mouse.js', () => ({
isIncompleteMouseSequence: vi.fn(),
}));
vi.mock('./utils/stdio.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('./utils/stdio.js')>();
return {
...actual,
writeToStdout: vi.fn((...args) =>
process.stdout.write(
...(args as Parameters<typeof process.stdout.write>),
),
),
patchStdio: vi.fn(() => () => {}),
createInkStdio: vi.fn(() => ({
stdout: {
write: vi.fn((...args) =>
process.stdout.write(
...(args as Parameters<typeof process.stdout.write>),
),
),
columns: 80,
rows: 24,
on: vi.fn(),
removeListener: vi.fn(),
},
stderr: {
write: vi.fn(),
},
})),
};
});
describe('gemini.tsx main function', () => {
let originalEnvGeminiSandbox: string | undefined;
let originalEnvSandbox: string | undefined;

View File

@@ -49,6 +49,10 @@ import {
recordSlowRender,
coreEvents,
CoreEvent,
createInkStdio,
patchStdio,
writeToStdout,
writeToStderr,
} from '@google/gemini-cli-core';
import {
initializeApp,
@@ -85,12 +89,6 @@ import { disableMouseEvents, enableMouseEvents } from './ui/utils/mouse.js';
import { ScrollProvider } from './ui/contexts/ScrollProvider.js';
import ansiEscapes from 'ansi-escapes';
import { isAlternateBufferEnabled } from './ui/hooks/useAlternateBuffer.js';
import {
createInkStdio,
patchStdio,
writeToStderr,
writeToStdout,
} from './utils/stdio.js';
import { profiler } from './ui/components/DebugProfiler.js';

View File

@@ -54,6 +54,21 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
...actual,
coreEvents: mockCoreEvents,
IdeClient: mockIdeClient,
writeToStdout: vi.fn((...args) =>
process.stdout.write(
...(args as Parameters<typeof process.stdout.write>),
),
),
writeToStderr: vi.fn((...args) =>
process.stderr.write(
...(args as Parameters<typeof process.stderr.write>),
),
),
patchStdio: vi.fn(() => () => {}),
createInkStdio: vi.fn(() => ({
stdout: process.stdout,
stderr: process.stderr,
})),
};
});
import type { LoadedSettings } from '../config/settings.js';
@@ -126,19 +141,6 @@ vi.mock('./utils/mouse.js', () => ({
enableMouseEvents: vi.fn(),
disableMouseEvents: vi.fn(),
}));
vi.mock('../utils/stdio.js', () => ({
writeToStdout: vi.fn((...args) =>
process.stdout.write(...(args as Parameters<typeof process.stdout.write>)),
),
writeToStderr: vi.fn((...args) =>
process.stderr.write(...(args as Parameters<typeof process.stderr.write>)),
),
patchStdio: vi.fn(() => () => {}),
createInkStdio: vi.fn(() => ({
stdout: process.stdout,
stderr: process.stderr,
})),
}));
import { useHistory } from './hooks/useHistoryManager.js';
import { useThemeCommand } from './hooks/useThemeCommand.js';
@@ -163,10 +165,9 @@ import { useLoadingIndicator } from './hooks/useLoadingIndicator.js';
import { useKeypress, type Key } from './hooks/useKeypress.js';
import { measureElement } from 'ink';
import { useTerminalSize } from './hooks/useTerminalSize.js';
import { ShellExecutionService } from '@google/gemini-cli-core';
import { ShellExecutionService, writeToStdout } from '@google/gemini-cli-core';
import { type ExtensionManager } from '../config/extension-manager.js';
import { enableMouseEvents, disableMouseEvents } from './utils/mouse.js';
import { writeToStdout } from '../utils/stdio.js';
describe('AppContainer State Management', () => {
let mockConfig: Config;

View File

@@ -110,7 +110,7 @@ import { disableMouseEvents, enableMouseEvents } from './utils/mouse.js';
import { useAlternateBuffer } from './hooks/useAlternateBuffer.js';
import { useSettings } from './contexts/SettingsContext.js';
import { enableSupportedProtocol } from './utils/kittyProtocolDetector.js';
import { writeToStdout } from '../utils/stdio.js';
import { writeToStdout } from '@google/gemini-cli-core';
const WARNING_PROMPT_DURATION_MS = 1000;
const QUEUE_ERROR_DISPLAY_DURATION_MS = 3000;

View File

@@ -5,7 +5,7 @@
*/
import { useEffect } from 'react';
import { writeToStdout } from '../../utils/stdio.js';
import { writeToStdout } from '@google/gemini-cli-core';
const ENABLE_BRACKETED_PASTE = '\x1b[?2004h';
const DISABLE_BRACKETED_PASTE = '\x1b[?2004l';

View File

@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { writeToStdout } from '../../utils/stdio.js';
import { writeToStdout } from '@google/gemini-cli-core';
import {
SGR_MOUSE_REGEX,
X11_MOUSE_REGEX,

View File

@@ -1,68 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { patchStdio, createInkStdio } from './stdio.js';
import { coreEvents } from '@google/gemini-cli-core';
vi.mock('@google/gemini-cli-core', () => ({
coreEvents: {
emitOutput: vi.fn(),
},
}));
describe('stdio utils', () => {
let originalStdoutWrite: typeof process.stdout.write;
let originalStderrWrite: typeof process.stderr.write;
beforeEach(() => {
originalStdoutWrite = process.stdout.write;
originalStderrWrite = process.stderr.write;
});
afterEach(() => {
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
vi.restoreAllMocks();
});
it('patchStdio redirects stdout and stderr to coreEvents', () => {
const cleanup = patchStdio();
process.stdout.write('test stdout');
expect(coreEvents.emitOutput).toHaveBeenCalledWith(
false,
'test stdout',
undefined,
);
process.stderr.write('test stderr');
expect(coreEvents.emitOutput).toHaveBeenCalledWith(
true,
'test stderr',
undefined,
);
cleanup();
// Verify cleanup
expect(process.stdout.write).toBe(originalStdoutWrite);
expect(process.stderr.write).toBe(originalStderrWrite);
});
it('createInkStdio writes to real stdout/stderr bypassing patch', () => {
const cleanup = patchStdio();
const { stdout: inkStdout, stderr: inkStderr } = createInkStdio();
inkStdout.write('ink stdout');
expect(coreEvents.emitOutput).not.toHaveBeenCalled();
inkStderr.write('ink stderr');
expect(coreEvents.emitOutput).not.toHaveBeenCalled();
cleanup();
});
});

View File

@@ -1,113 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { coreEvents } from '@google/gemini-cli-core';
// Capture the original stdout and stderr write methods before any monkey patching occurs.
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
/**
* Writes to the real stdout, bypassing any monkey patching on process.stdout.write.
*/
export function writeToStdout(
...args: Parameters<typeof process.stdout.write>
): boolean {
return originalStdoutWrite(...args);
}
/**
* Writes to the real stderr, bypassing any monkey patching on process.stderr.write.
*/
export function writeToStderr(
...args: Parameters<typeof process.stderr.write>
): boolean {
return originalStderrWrite(...args);
}
/**
* Monkey patches process.stdout.write and process.stderr.write to redirect output to the provided logger.
* This prevents stray output from libraries (or the app itself) from corrupting the UI.
* Returns a cleanup function that restores the original write methods.
*/
export function patchStdio(): () => void {
const previousStdoutWrite = process.stdout.write;
const previousStderrWrite = process.stderr.write;
process.stdout.write = (
chunk: Uint8Array | string,
encodingOrCb?:
| BufferEncoding
| ((err?: NodeJS.ErrnoException | null) => void),
cb?: (err?: NodeJS.ErrnoException | null) => void,
) => {
const encoding =
typeof encodingOrCb === 'string' ? encodingOrCb : undefined;
coreEvents.emitOutput(false, chunk, encoding);
const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb;
if (callback) {
callback();
}
return true;
};
process.stderr.write = (
chunk: Uint8Array | string,
encodingOrCb?:
| BufferEncoding
| ((err?: NodeJS.ErrnoException | null) => void),
cb?: (err?: NodeJS.ErrnoException | null) => void,
) => {
const encoding =
typeof encodingOrCb === 'string' ? encodingOrCb : undefined;
coreEvents.emitOutput(true, chunk, encoding);
const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb;
if (callback) {
callback();
}
return true;
};
return () => {
process.stdout.write = previousStdoutWrite;
process.stderr.write = previousStderrWrite;
};
}
/**
* Creates proxies for process.stdout and process.stderr that use the real write methods
* (writeToStdout and writeToStderr) bypassing any monkey patching.
* This is used by Ink to render to the real output.
*/
export function createInkStdio() {
const inkStdout = new Proxy(process.stdout, {
get(target, prop, receiver) {
if (prop === 'write') {
return writeToStdout;
}
const value = Reflect.get(target, prop, receiver);
if (typeof value === 'function') {
return value.bind(target);
}
return value;
},
});
const inkStderr = new Proxy(process.stderr, {
get(target, prop, receiver) {
if (prop === 'write') {
return writeToStderr;
}
const value = Reflect.get(target, prop, receiver);
if (typeof value === 'function') {
return value.bind(target);
}
return value;
},
});
return { stdout: inkStdout, stderr: inkStderr };
}