Skip to content

Commit 2684465

Browse files
authored
Refactor: Replace direct child_process.spawn calls with spawnProcess for consistent environment handling and add PYTHONUTF8=1 for subprocesses (#1087)
fixes #989
1 parent 9ae8e83 commit 2684465

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/common/childProcess.apis.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ export function spawnProcess(
2424
args: string[],
2525
options?: cp.SpawnOptions,
2626
): cp.ChildProcess | cp.ChildProcessWithoutNullStreams {
27-
return cp.spawn(command, args, options ?? {});
27+
// Set PYTHONUTF8=1; user-provided PYTHONUTF8 values take precedence.
28+
const env = {
29+
PYTHONUTF8: '1',
30+
...(options?.env ?? process.env),
31+
};
32+
return cp.spawn(command, args, { ...options, env });
2833
}

src/managers/common/nativePythonFinder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as ch from 'child_process';
21
import * as fs from 'fs-extra';
32
import * as path from 'path';
43
import { PassThrough } from 'stream';
54
import { Disposable, ExtensionContext, LogOutputChannel, Uri } from 'vscode';
65
import * as rpc from 'vscode-jsonrpc/node';
76
import { PythonProjectApi } from '../../api';
7+
import { spawnProcess } from '../../common/childProcess.apis';
88
import { ENVS_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../../common/constants';
99
import { getExtension } from '../../common/extension.apis';
1010
import { traceError, traceLog, traceVerbose, traceWarn } from '../../common/logging';
@@ -213,7 +213,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
213213
const writable = new PassThrough();
214214
const disposables: Disposable[] = [];
215215
try {
216-
const proc = ch.spawn(this.toolPath, ['server'], { env: process.env });
216+
const proc = spawnProcess(this.toolPath, ['server'], { env: process.env, stdio: 'pipe' });
217217
proc.stdout.pipe(readable, { end: false });
218218
proc.stderr.on('data', (data) => this.outputChannel.error(`[pet] ${data.toString()}`));
219219
writable.pipe(proc.stdin, { end: false });

src/managers/conda/condaUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as ch from 'child_process';
21
import * as fse from 'fs-extra';
32
import * as os from 'os';
43
import * as path from 'path';
@@ -25,6 +24,7 @@ import {
2524
PythonEnvironmentInfo,
2625
PythonProject,
2726
} from '../../api';
27+
import { spawnProcess } from '../../common/childProcess.apis';
2828
import { ENVS_EXTENSION_ID, EXTENSION_ROOT_DIR } from '../../common/constants';
2929
import { showErrorMessageWithLogs } from '../../common/errors/utils';
3030
import { Common, CondaStrings, PackageManagement, Pickers } from '../../common/localize';
@@ -206,7 +206,7 @@ async function _runConda(
206206
const quotedConda = quoteStringIfNecessary(conda);
207207
const timer = new StopWatch();
208208
deferred.promise.finally(() => traceInfo(`Ran conda in ${timer.elapsedTime}: ${quotedConda} ${args.join(' ')}`));
209-
const proc = ch.spawn(quotedConda, args, { shell: true });
209+
const proc = spawnProcess(quotedConda, args, { shell: true });
210210

211211
token?.onCancellationRequested(() => {
212212
proc.kill();

src/managers/poetry/poetryPackageManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as ch from 'child_process';
21
import * as fsapi from 'fs-extra';
32
import * as path from 'path';
43
import {
@@ -22,6 +21,7 @@ import {
2221
PythonEnvironment,
2322
PythonEnvironmentApi,
2423
} from '../../api';
24+
import { spawnProcess } from '../../common/childProcess.apis';
2525
import { showErrorMessage, showInputBox, withProgress } from '../../common/window.apis';
2626
import { PoetryManager } from './poetryManager';
2727
import { getPoetry } from './poetryUtils';
@@ -271,7 +271,7 @@ export async function runPoetry(
271271
log?.info(`Running: ${poetry} ${args.join(' ')}`);
272272

273273
return new Promise<string>((resolve, reject) => {
274-
const proc = ch.spawn(poetry, args, { cwd });
274+
const proc = spawnProcess(poetry, args, { cwd });
275275
token?.onCancellationRequested(() => {
276276
proc.kill();
277277
reject(new CancellationError());

0 commit comments

Comments
 (0)