Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/cli/src/utils/relaunch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ import {
import { EventEmitter } from 'node:events';
import { RELAUNCH_EXIT_CODE } from './processUtils.js';
import { spawn, type ChildProcess } from 'node:child_process';
import { writeFileSync } from 'node:fs';

const mocks = vi.hoisted(() => ({
writeToStderr: vi.fn(),
}));

vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
vi.mock('node:fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:fs')>();
return {
...actual,
writeToStderr: mocks.writeToStderr,
writeFileSync: vi.fn(),
};
});

const mockedWriteFileSync = vi.mocked(writeFileSync);

vi.mock('node:child_process', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:child_process')>();
return {
Expand All @@ -55,7 +53,7 @@ describe('relaunchOnExitCode', () => {
.spyOn(process.stdin, 'resume')
.mockImplementation(() => process.stdin);
vi.clearAllMocks();
mocks.writeToStderr.mockClear();
mockedWriteFileSync.mockClear();
});

afterEach(() => {
Expand Down Expand Up @@ -100,7 +98,8 @@ describe('relaunchOnExitCode', () => {
);

expect(runner).toHaveBeenCalledTimes(1);
expect(mocks.writeToStderr).toHaveBeenCalledWith(
expect(mockedWriteFileSync).toHaveBeenCalledWith(
2,
expect.stringContaining(
'Fatal error: Failed to relaunch the CLI process.',
),
Expand All @@ -123,7 +122,7 @@ describe('relaunchAppInChildProcess', () => {

beforeEach(() => {
vi.clearAllMocks();
mocks.writeToStderr.mockClear();
mockedWriteFileSync.mockClear();

process.env = { ...originalEnv };
delete process.env['GEMINI_CLI_NO_RELAUNCH'];
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/src/utils/relaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { writeFileSync } from 'node:fs';
import { spawn } from 'node:child_process';
import { RELAUNCH_EXIT_CODE } from './processUtils.js';
import {
writeToStderr,
type AdminControlsSettings,
} from '@google/gemini-cli-core';
import { type AdminControlsSettings } from '@google/gemini-cli-core';

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.

high

The PR description states that writeToStderr() is kept for compatibility, but it has been removed from the imports. Please restore the import.

Suggested change
import { type AdminControlsSettings } from '@google/gemini-cli-core';
import { writeToStderr, type AdminControlsSettings } from '@google/gemini-cli-core';


export async function relaunchOnExitCode(runner: () => Promise<number>) {
while (true) {
Expand All @@ -23,9 +21,11 @@ export async function relaunchOnExitCode(runner: () => Promise<number>) {
process.stdin.resume();
const errorMessage =
error instanceof Error ? (error.stack ?? error.message) : String(error);
writeToStderr(
`Fatal error: Failed to relaunch the CLI process.\n${errorMessage}\n`,
);
const message = `Fatal error: Failed to relaunch the CLI process.\n${errorMessage}\n`;
// Use synchronous write to fd 2 (stderr) to guarantee the message is
// visible before process.exit() terminates. The buffered writeToStderr()
// can lose output on macOS when stderr is piped.
writeFileSync(2, message);
process.exit(1);
}
}
Expand Down
Loading