Skip to content

Commit 564a37a

Browse files
Use execFile rather than spawn (#544)
## Summary Recommended in #539.
1 parent 45fb46d commit 564a37a

1 file changed

Lines changed: 7 additions & 24 deletions

File tree

src/common/server.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import { updateServerKind, updateStatus } from "./status";
3939
import { getProjectRoot } from "./utilities";
4040
import { isVirtualWorkspace } from "./vscodeapi";
41-
import { spawn } from "child_process";
41+
import { execFile } from "child_process";
4242
import which = require("which");
4343

4444
export type IInitializationOptions = {
@@ -49,40 +49,23 @@ export type IInitializationOptions = {
4949
/**
5050
* Function to execute a command and return the stdout.
5151
*/
52-
function executeCommand(cmd: string, args: string[] = []): Promise<string> {
52+
function executeFile(file: string, args: string[] = []): Promise<string> {
5353
return new Promise((resolve, reject) => {
54-
const child = spawn(cmd, args);
55-
56-
let stdout = "";
57-
let stderr = "";
58-
59-
child.stdout.on("data", (data) => {
60-
stdout += data.toString();
61-
});
62-
63-
child.stderr.on("data", (data) => {
64-
stderr += data.toString();
65-
});
66-
67-
child.on("close", (code) => {
68-
if (code !== 0) {
69-
reject(new Error(stderr));
54+
execFile(file, args, (error, stdout, stderr) => {
55+
if (error) {
56+
reject(new Error(stderr || error.message));
7057
} else {
7158
resolve(stdout);
7259
}
7360
});
74-
75-
child.on("error", (error) => {
76-
reject(error);
77-
});
7861
});
7962
}
8063

8164
/**
8265
* Get the version of the Ruff executable at the given path.
8366
*/
8467
async function getRuffVersion(executable: string): Promise<VersionInfo> {
85-
const stdout = await executeCommand(executable, ["--version"]);
68+
const stdout = await executeFile(executable, ["--version"]);
8669
const version = stdout.trim().split(" ")[1];
8770
const [major, minor, patch] = version.split(".").map((x) => parseInt(x, 10));
8871
return { major, minor, patch };
@@ -131,7 +114,7 @@ async function findRuffBinaryPath(
131114
// Otherwise, we'll call a Python script that tries to locate a binary.
132115
let ruffBinaryPath: string | undefined;
133116
try {
134-
const stdout = await executeCommand(settings.interpreter[0], [FIND_RUFF_BINARY_SCRIPT_PATH]);
117+
const stdout = await executeFile(settings.interpreter[0], [FIND_RUFF_BINARY_SCRIPT_PATH]);
135118
ruffBinaryPath = stdout.trim();
136119
} catch (err) {
137120
await vscode.window

0 commit comments

Comments
 (0)