Skip to content

Commit 7488f35

Browse files
committed
fix(core): skip the V8 flag restart for stdin scripts
A script piped into node (`node -`, or node without a script argument) is consumed before the plugin loads. The restarted process then read an empty stdin and exited successfully without running any benchmark. Keep the process and its missing-flags warning in that case. `-e`/`-p` code is part of execArgv and still restarts.
1 parent 32b5c3c commit 7488f35

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎packages/core/src/introspection.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ export const tryIntrospect = () => {
9494
}
9595
};
9696

97+
const EVAL_FLAGS = ["-e", "--eval", "-p", "--print"];
98+
99+
// `node -` or `node` without a script reads the script from stdin (or opens the
100+
// REPL). The input is consumed by then, so the restarted process would get none.
101+
const readsScriptFromStdin = () => {
102+
const scriptPath = process.argv[1];
103+
if (scriptPath === "-") {
104+
return true;
105+
}
106+
if (scriptPath !== undefined) {
107+
return false;
108+
}
109+
return !process.execArgv.some((arg) =>
110+
EVAL_FLAGS.some((flag) => arg === flag || arg.startsWith(`${flag}=`)),
111+
);
112+
};
113+
97114
/**
98115
* Restarts the process with the V8 flags the runner mode requires when the
99116
* launcher did not pass them (e.g. Bazel executes node by absolute path, so the
@@ -121,6 +138,10 @@ export const ensureV8Flags = () => {
121138
return;
122139
}
123140

141+
if (readsScriptFromStdin()) {
142+
return;
143+
}
144+
124145
// The restart did not help (its launcher stripped the flags again): do not
125146
// restart in a loop.
126147
if (process.env[RESTARTED_PID_ENV] === String(process.pid)) {

‎packages/core/tests/ensureV8Flags.integ.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import path from "path";
66

77
export {}; // Make this a module
88

9+
enum ScriptEntry {
10+
File,
11+
Stdin,
12+
Eval,
13+
}
14+
915
interface BenchmarkProcess {
1016
spawnedPid: number | undefined;
1117
pid: number;
@@ -17,6 +23,7 @@ const runBenchmarkProcess = (
1723
env: NodeJS.ProcessEnv,
1824
nodeArgs: string[] = [],
1925
prelude = "",
26+
entry = ScriptEntry.File,
2027
): BenchmarkProcess => {
2128
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "codspeed-ensure-v8-"));
2229
const script = path.join(dir, "bench.js");
@@ -34,9 +41,15 @@ process.stdout.write(
3441
const baseEnv = { ...process.env };
3542
delete baseEnv.CODSPEED_ENV;
3643
delete baseEnv.CODSPEED_RUNNER_MODE;
37-
const result = spawnSync(process.execPath, [...nodeArgs, script], {
44+
const entryArgs = {
45+
[ScriptEntry.File]: [script],
46+
[ScriptEntry.Stdin]: ["-"],
47+
[ScriptEntry.Eval]: ["-e", `require(${JSON.stringify(script)})`],
48+
}[entry];
49+
const result = spawnSync(process.execPath, [...nodeArgs, ...entryArgs], {
3850
env: { ...baseEnv, ...env },
3951
encoding: "utf8",
52+
input: entry === ScriptEntry.Stdin ? fs.readFileSync(script) : undefined,
4053
});
4154
if (result.status !== 0) {
4255
throw new Error(`child failed: ${result.stderr}`);
@@ -64,6 +77,31 @@ describe("ensureV8Flags", () => {
6477
expect(execArgv).toContain("--predictable");
6578
});
6679

80+
it("restarts a process started with -e", () => {
81+
const { spawnedPid, pid, execArgv } = runBenchmarkProcess(
82+
{ CODSPEED_ENV: "runner", CODSPEED_RUNNER_MODE: "instrumentation" },
83+
[],
84+
"",
85+
ScriptEntry.Eval,
86+
);
87+
88+
expect(pid).toBe(spawnedPid);
89+
expect(execArgv).toContain("--no-opt");
90+
});
91+
92+
it("keeps a process that read its script from stdin", () => {
93+
// Parsing the output proves the script ran instead of an empty restart.
94+
const { spawnedPid, pid, execArgv } = runBenchmarkProcess(
95+
{ CODSPEED_ENV: "runner", CODSPEED_RUNNER_MODE: "instrumentation" },
96+
[],
97+
"",
98+
ScriptEntry.Stdin,
99+
);
100+
101+
expect(pid).toBe(spawnedPid);
102+
expect(execArgv).not.toContain("--no-opt");
103+
});
104+
67105
it("adds the flags before the user's so user flags win", () => {
68106
const { execArgv } = runBenchmarkProcess(
69107
{ CODSPEED_ENV: "runner", CODSPEED_RUNNER_MODE: "instrumentation" },

0 commit comments

Comments
 (0)