mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
fix(cli): gemini command stuck in git bash (#6397)
Co-authored-by: Arya Gummadi <aryagummadi@google.com>
This commit is contained in:
@@ -342,7 +342,9 @@ export async function main() {
|
||||
}
|
||||
}
|
||||
if (!input) {
|
||||
console.error('No input provided via stdin.');
|
||||
console.error(
|
||||
`No input provided via stdin. Input can be provided by piping data into gemini or using the --prompt option.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
112
packages/cli/src/utils/readStdin.test.ts
Normal file
112
packages/cli/src/utils/readStdin.test.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { vi, describe, expect, it, beforeEach, afterEach } from 'vitest';
|
||||
import { readStdin } from './readStdin.js';
|
||||
|
||||
// Mock process.stdin
|
||||
const mockStdin = {
|
||||
setEncoding: vi.fn(),
|
||||
read: vi.fn(),
|
||||
on: vi.fn(),
|
||||
removeListener: vi.fn(),
|
||||
destroy: vi.fn(),
|
||||
};
|
||||
|
||||
describe('readStdin', () => {
|
||||
let originalStdin: typeof process.stdin;
|
||||
let onReadableHandler: () => void;
|
||||
let onEndHandler: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
originalStdin = process.stdin;
|
||||
|
||||
// Replace process.stdin with our mock
|
||||
Object.defineProperty(process, 'stdin', {
|
||||
value: mockStdin,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
// Capture event handlers
|
||||
mockStdin.on.mockImplementation((event: string, handler: () => void) => {
|
||||
if (event === 'readable') onReadableHandler = handler;
|
||||
if (event === 'end') onEndHandler = handler;
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
Object.defineProperty(process, 'stdin', {
|
||||
value: originalStdin,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should read and accumulate data from stdin', async () => {
|
||||
mockStdin.read
|
||||
.mockReturnValueOnce('I love ')
|
||||
.mockReturnValueOnce('Gemini!')
|
||||
.mockReturnValueOnce(null);
|
||||
|
||||
const promise = readStdin();
|
||||
|
||||
// Trigger readable event
|
||||
onReadableHandler();
|
||||
|
||||
// Trigger end to resolve
|
||||
onEndHandler();
|
||||
|
||||
await expect(promise).resolves.toBe('I love Gemini!');
|
||||
});
|
||||
|
||||
it('should handle empty stdin input', async () => {
|
||||
mockStdin.read.mockReturnValue(null);
|
||||
|
||||
const promise = readStdin();
|
||||
|
||||
// Trigger end immediately
|
||||
onEndHandler();
|
||||
|
||||
await expect(promise).resolves.toBe('');
|
||||
});
|
||||
|
||||
// Emulate terminals where stdin is not TTY (eg: git bash)
|
||||
it('should timeout and resolve with empty string when no input is available', async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
const promise = readStdin();
|
||||
|
||||
// Fast-forward past the timeout (to run test faster)
|
||||
vi.advanceTimersByTime(500);
|
||||
|
||||
await expect(promise).resolves.toBe('');
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should clear timeout once when data is received and resolve with data', async () => {
|
||||
const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout');
|
||||
mockStdin.read
|
||||
.mockReturnValueOnce('chunk1')
|
||||
.mockReturnValueOnce('chunk2')
|
||||
.mockReturnValueOnce(null);
|
||||
|
||||
const promise = readStdin();
|
||||
|
||||
// Trigger readable event
|
||||
onReadableHandler();
|
||||
|
||||
expect(clearTimeoutSpy).toHaveBeenCalledOnce();
|
||||
|
||||
// Trigger end to resolve
|
||||
onEndHandler();
|
||||
|
||||
await expect(promise).resolves.toBe('chunk1chunk2');
|
||||
});
|
||||
});
|
||||
@@ -9,86 +9,57 @@ export async function readStdin(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = '';
|
||||
let totalSize = 0;
|
||||
let hasReceivedData = false;
|
||||
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
function cleanup() {
|
||||
clearTimeout(timeout);
|
||||
const pipedInputShouldBeAvailableInMs = 500;
|
||||
let pipedInputTimerId: null | NodeJS.Timeout = setTimeout(() => {
|
||||
// stop reading if input is not available yet, this is needed
|
||||
// in terminals where stdin is never TTY and nothing's piped
|
||||
// which causes the program to get stuck expecting data from stdin
|
||||
onEnd();
|
||||
}, pipedInputShouldBeAvailableInMs);
|
||||
|
||||
const onReadable = () => {
|
||||
let chunk;
|
||||
while ((chunk = process.stdin.read()) !== null) {
|
||||
if (pipedInputTimerId) {
|
||||
clearTimeout(pipedInputTimerId);
|
||||
pipedInputTimerId = null;
|
||||
}
|
||||
|
||||
if (totalSize + chunk.length > MAX_STDIN_SIZE) {
|
||||
const remainingSize = MAX_STDIN_SIZE - totalSize;
|
||||
data += chunk.slice(0, remainingSize);
|
||||
console.warn(
|
||||
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
|
||||
);
|
||||
process.stdin.destroy(); // Stop reading further
|
||||
break;
|
||||
}
|
||||
data += chunk;
|
||||
totalSize += chunk.length;
|
||||
}
|
||||
};
|
||||
|
||||
const onEnd = () => {
|
||||
cleanup();
|
||||
resolve(data);
|
||||
};
|
||||
|
||||
const onError = (err: Error) => {
|
||||
cleanup();
|
||||
reject(err);
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
if (pipedInputTimerId) {
|
||||
clearTimeout(pipedInputTimerId);
|
||||
pipedInputTimerId = null;
|
||||
}
|
||||
process.stdin.removeListener('readable', onReadable);
|
||||
process.stdin.removeListener('end', onEnd);
|
||||
process.stdin.removeListener('error', onError);
|
||||
}
|
||||
|
||||
function processChunk(chunk: string): boolean {
|
||||
hasReceivedData = true;
|
||||
if (totalSize + chunk.length > MAX_STDIN_SIZE) {
|
||||
const remainingSize = MAX_STDIN_SIZE - totalSize;
|
||||
data += chunk.slice(0, remainingSize);
|
||||
console.warn(
|
||||
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
|
||||
);
|
||||
process.stdin.destroy();
|
||||
return true; // Indicates truncation occurred
|
||||
} else {
|
||||
data += chunk;
|
||||
totalSize += chunk.length;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function checkInitialState(): boolean {
|
||||
if (process.stdin.destroyed || process.stdin.readableEnded) {
|
||||
cleanup();
|
||||
resolve('');
|
||||
return true;
|
||||
}
|
||||
|
||||
const chunk = process.stdin.read();
|
||||
if (chunk !== null) {
|
||||
processChunk(chunk);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!process.stdin.readable) {
|
||||
cleanup();
|
||||
resolve('');
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (checkInitialState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
function onReadable() {
|
||||
let chunk;
|
||||
while ((chunk = process.stdin.read()) !== null) {
|
||||
const truncated = processChunk(chunk);
|
||||
if (truncated) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onEnd() {
|
||||
cleanup();
|
||||
resolve(data);
|
||||
}
|
||||
|
||||
function onError(err: Error) {
|
||||
cleanup();
|
||||
reject(err);
|
||||
}
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
if (!hasReceivedData) {
|
||||
cleanup();
|
||||
resolve('');
|
||||
}
|
||||
}, 50);
|
||||
};
|
||||
|
||||
process.stdin.on('readable', onReadable);
|
||||
process.stdin.on('end', onEnd);
|
||||
|
||||
Reference in New Issue
Block a user