Skip to content

Commit

Permalink
Use execFile rather than spawn (#544)
Browse files Browse the repository at this point in the history
## Summary

Recommended in #539.
  • Loading branch information
charliermarsh authored Jul 19, 2024
1 parent 45fb46d commit 564a37a
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
import { updateServerKind, updateStatus } from "./status";
import { getProjectRoot } from "./utilities";
import { isVirtualWorkspace } from "./vscodeapi";
import { spawn } from "child_process";
import { execFile } from "child_process";
import which = require("which");

export type IInitializationOptions = {
Expand All @@ -49,40 +49,23 @@ export type IInitializationOptions = {
/**
* Function to execute a command and return the stdout.
*/
function executeCommand(cmd: string, args: string[] = []): Promise<string> {
function executeFile(file: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args);

let stdout = "";
let stderr = "";

child.stdout.on("data", (data) => {
stdout += data.toString();
});

child.stderr.on("data", (data) => {
stderr += data.toString();
});

child.on("close", (code) => {
if (code !== 0) {
reject(new Error(stderr));
execFile(file, args, (error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || error.message));
} else {
resolve(stdout);
}
});

child.on("error", (error) => {
reject(error);
});
});
}

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

0 comments on commit 564a37a

Please sign in to comment.