Skip to content
Merged
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
13 changes: 13 additions & 0 deletions packages/core/src/services/shellExecutionService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ describe('ShellExecutionService child_process fallback', () => {
configurable: true,
});

mockChildProcess.once = mockChildProcess.on.bind(mockChildProcess);

mockCpSpawn.mockReturnValue(mockChildProcess);
});

Expand Down Expand Up @@ -491,6 +493,16 @@ describe('ShellExecutionService child_process fallback', () => {
});
});

it('should resolve when only the close event fires', async () => {
const { result } = await simulateExecution('ls -l', (cp) => {
cp.stdout?.emit('data', Buffer.from('file1.txt\n'));
cp.emit('close', 0, null);
});

expect(result.exitCode).toBe(0);
expect(result.output).toBe('file1.txt');
});

it('should strip ANSI codes from output', async () => {
const { result } = await simulateExecution('ls --color=auto', (cp) => {
cp.stdout?.emit('data', Buffer.from('a\u001b[31mred\u001b[0mword'));
Expand Down Expand Up @@ -831,6 +843,7 @@ describe('ShellExecutionService execution method selection', () => {
value: 54321,
configurable: true,
});
mockChildProcess.once = mockChildProcess.on.bind(mockChildProcess);
mockCpSpawn.mockReturnValue(mockChildProcess);
});

Expand Down
24 changes: 21 additions & 3 deletions packages/core/src/services/shellExecutionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,16 @@ export class ShellExecutionService {
}
};

let hasResolved = false;

const handleExit = (
code: number | null,
signal: NodeJS.Signals | null,
) => {
if (hasResolved) {
return;
}
hasResolved = true;
const { finalBuffer } = cleanup();
// Ensure we don't add an extra newline if stdout already ends with one.
const separator = stdout.endsWith('\n') ? '' : '\n';
Expand Down Expand Up @@ -321,9 +327,21 @@ export class ShellExecutionService {

abortSignal.addEventListener('abort', abortHandler, { once: true });

child.on('exit', (code, signal) => {
handleExit(code, signal);
});
if (child.once) {
child.once('exit', (code, signal) => {
handleExit(code, signal);
});
child.once('close', (code, signal) => {
handleExit(code, signal);
});
} else {
child.on('exit', (code, signal) => {
handleExit(code, signal);
});
child.on('close', (code, signal) => {
handleExit(code, signal);
});
}

function cleanup() {
exited = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const fakeChildFactory = () => {
stdout: { on: vi.fn<(event: string, cb: Listener) => void>() },
stderr: { on: vi.fn<(event: string, cb: Listener) => void>() },
on: vi.fn<(event: string, cb: Listener) => void>(),
once: vi.fn<(event: string, cb: Listener) => void>(),
pid: 2222,
kill: vi.fn<(signal?: NodeJS.Signals) => boolean>(),
};
Expand Down
Loading