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
28 changes: 28 additions & 0 deletions src/common/childProcess.apis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as cp from 'child_process';

/**
* Spawns a new process using the specified command and arguments.
* This function abstracts cp.spawn to make it easier to mock in tests.
*
* When stdio: 'pipe' is used, returns ChildProcessWithoutNullStreams.
* Otherwise returns the standard ChildProcess.
*/

// Overload for stdio: 'pipe' - guarantees non-null streams
export function spawnProcess(
command: string,
args: string[],
options: cp.SpawnOptions & { stdio: 'pipe' },
): cp.ChildProcessWithoutNullStreams;

// Overload for general case
export function spawnProcess(command: string, args: string[], options?: cp.SpawnOptions): cp.ChildProcess;

// Implementation - delegates to cp.spawn to preserve its typing magic
export function spawnProcess(
command: string,
args: string[],
options?: cp.SpawnOptions,
): cp.ChildProcess | cp.ChildProcessWithoutNullStreams {
return cp.spawn(command, args, options ?? {});
}
8 changes: 6 additions & 2 deletions src/features/execution/runInBackground.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cp from 'child_process';
import { PythonBackgroundRunOptions, PythonEnvironment, PythonProcess } from '../../api';
import { spawnProcess } from '../../common/childProcess.apis';
import { traceError, traceInfo, traceWarn } from '../../common/logging';
import { quoteStringIfNecessary } from './execUtils';

Expand Down Expand Up @@ -39,7 +39,11 @@ export async function runInBackground(
traceWarn(`Error checking if executable exists: ${err instanceof Error ? err.message : String(err)}`);
}

const proc = cp.spawn(executable, allArgs, { stdio: 'pipe', cwd: options.cwd, env: options.env });
const proc = spawnProcess(executable, allArgs, {
stdio: 'pipe',
cwd: options.cwd,
env: options.env,
});

return {
pid: proc.pid,
Expand Down
Loading
Loading