Skip to content

Commit 1a5e669

Browse files
author
Andy
authored
Don't crash if spawnSync result's stderr is null (microsoft#27616)
1 parent ca94d8e commit 1a5e669

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/testRunner/externalCompileRunner.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const fs = require("fs");
2-
const path = require("path");
3-
const del = require("del");
1+
const fs: typeof import("fs") = require("fs");
2+
const path: typeof import("path") = require("path");
3+
const del: typeof import("del") = require("del");
44

55
interface ExecResult {
66
stdout: Buffer;
@@ -49,12 +49,9 @@ abstract class ExternalCompileRunnerBase extends RunnerBase {
4949
let types: string[] | undefined;
5050
if (fs.existsSync(path.join(cwd, "test.json"))) {
5151
const submoduleDir = path.join(cwd, directoryName);
52-
const reset = cp.spawnSync("git", ["reset", "HEAD", "--hard"], { cwd: submoduleDir, timeout, shell: true, stdio });
53-
if (reset.status !== 0) throw new Error(`git reset for ${directoryName} failed: ${reset.stderr.toString()}`);
54-
const clean = cp.spawnSync("git", ["clean", "-f"], { cwd: submoduleDir, timeout, shell: true, stdio });
55-
if (clean.status !== 0) throw new Error(`git clean for ${directoryName} failed: ${clean.stderr.toString()}`);
56-
const update = cp.spawnSync("git", ["submodule", "update", "--init", "--remote", "."], { cwd: submoduleDir, timeout, shell: true, stdio });
57-
if (update.status !== 0) throw new Error(`git submodule update for ${directoryName} failed: ${update.stderr.toString()}`);
52+
exec("git", ["reset", "HEAD", "--hard"], { cwd: submoduleDir });
53+
exec("git", ["clean", "-f"], { cwd: submoduleDir });
54+
exec("git", ["submodule", "update", "--init", "--remote", "."], { cwd: submoduleDir });
5855

5956
const config = JSON.parse(fs.readFileSync(path.join(cwd, "test.json"), { encoding: "utf8" })) as UserConfig;
6057
ts.Debug.assert(!!config.types, "Bad format from test.json: Types field must be present.");
@@ -69,18 +66,23 @@ abstract class ExternalCompileRunnerBase extends RunnerBase {
6966
if (fs.existsSync(path.join(cwd, "node_modules"))) {
7067
del.sync(path.join(cwd, "node_modules"), { force: true });
7168
}
72-
const install = cp.spawnSync(`npm`, ["i", "--ignore-scripts"], { cwd, timeout: timeout / 2, shell: true, stdio }); // NPM shouldn't take the entire timeout - if it takes a long time, it should be terminated and we should log the failure
73-
if (install.status !== 0) throw new Error(`NPM Install for ${directoryName} failed: ${install.stderr.toString()}`);
69+
exec("npm", ["i", "--ignore-scripts"], { cwd, timeout: timeout / 2 }); // NPM shouldn't take the entire timeout - if it takes a long time, it should be terminated and we should log the failure
7470
}
7571
const args = [path.join(Harness.IO.getWorkspaceRoot(), "built/local/tsc.js")];
7672
if (types) {
7773
args.push("--types", types.join(","));
7874
// Also actually install those types (for, eg, the js projects which need node)
79-
const install = cp.spawnSync(`npm`, ["i", ...types.map(t => `@types/${t}`), "--no-save", "--ignore-scripts"], { cwd: originalCwd, timeout: timeout / 2, shell: true, stdio }); // NPM shouldn't take the entire timeout - if it takes a long time, it should be terminated and we should log the failure
80-
if (install.status !== 0) throw new Error(`NPM Install types for ${directoryName} failed: ${install.stderr.toString()}`);
75+
exec("npm", ["i", ...types.map(t => `@types/${t}`), "--no-save", "--ignore-scripts"], { cwd: originalCwd, timeout: timeout / 2 }); // NPM shouldn't take the entire timeout - if it takes a long time, it should be terminated and we should log the failure
8176
}
8277
args.push("--noEmit");
8378
Harness.Baseline.runBaseline(`${cls.kind()}/${directoryName}.log`, cls.report(cp.spawnSync(`node`, args, { cwd, timeout, shell: true }), cwd));
79+
80+
function exec(command: string, args: string[], options: { cwd: string, timeout?: number }): void {
81+
const res = cp.spawnSync(command, args, { timeout, shell: true, stdio, ...options });
82+
if (res.status !== 0) {
83+
throw new Error(`${command} ${args.join(" ")} for ${directoryName} failed: ${res.stderr && res.stderr.toString()}`);
84+
}
85+
}
8486
});
8587
});
8688
}

0 commit comments

Comments
 (0)