Skip to content

Commit bc57ebf

Browse files
Zacharonmaria-rcks
andauthored
fix(scripts): launch check:changed on Windows without a vp shim (#24164)
Co-authored-by: maria-rcks <[email protected]>
1 parent fcb1d0b commit bc57ebf

4 files changed

Lines changed: 290 additions & 1 deletion

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFS from "node:fs";
4+
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
6+
import { expect, it } from "vite-plus/test";
7+
8+
import { vitePlusCommand } from "./check-changed-command.ts";
9+
10+
function fixture() {
11+
const root = NodeFS.realpathSync(
12+
NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 check & (vp) % ! ")),
13+
);
14+
const write = (file: string, content: string) => {
15+
const path = NodePath.join(root, file);
16+
NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
17+
NodeFS.writeFileSync(path, content);
18+
};
19+
NodeFS.copyFileSync(
20+
NodePath.join(import.meta.dirname, "check-changed-command.ts"),
21+
NodePath.join(root, "command.ts"),
22+
);
23+
write("package.json", JSON.stringify({ type: "module" }));
24+
write(
25+
"node_modules/vite-plus/package.json",
26+
JSON.stringify({ type: "module", exports: { "./bin": "./cli.js" } }),
27+
);
28+
write(
29+
"node_modules/vite-plus/cli.js",
30+
`import fs from 'node:fs';
31+
fs.writeFileSync(process.env.CALL_FILE, JSON.stringify({
32+
args: process.argv.slice(2), cwd: process.cwd(), marker: process.env.ARG_MARKER
33+
}));
34+
process.stdout.write('vp stdout');
35+
process.stderr.write('vp stderr');
36+
process.exit(Number(process.env.EXIT_STATUS));`,
37+
);
38+
write(
39+
"run.mjs",
40+
`import { spawn, spawnSync } from 'node:child_process';
41+
import { vitePlusCommand } from './command.ts';
42+
const [command, args] = vitePlusCommand(process.argv.slice(3), 'win32');
43+
const options = { cwd: process.env.CHILD_CWD, env: process.env };
44+
if (process.argv[2] === 'sync') {
45+
const result = spawnSync(command, args, { ...options, encoding: 'utf8' });
46+
process.stdout.write(result.stdout ?? '');
47+
process.stderr.write(result.stderr ?? '');
48+
if (result.error) console.error(result.error.message);
49+
process.exitCode = result.status ?? 1;
50+
} else {
51+
const child = spawn(command, args, options);
52+
child.stdout.pipe(process.stdout);
53+
child.stderr.pipe(process.stderr);
54+
child.on('error', error => console.error(error.message));
55+
child.on('close', status => { process.exitCode = status ?? 1; });
56+
}`,
57+
);
58+
const cwd = NodePath.join(root, "temporary install");
59+
NodeFS.mkdirSync(cwd);
60+
return {
61+
cwd,
62+
run(mode: string, args: Array<string>, status: number) {
63+
return NodeChildProcess.spawnSync(
64+
process.execPath,
65+
[NodePath.join(root, "run.mjs"), mode, ...args],
66+
{
67+
cwd: NodeOS.tmpdir(),
68+
encoding: "utf8",
69+
env: {
70+
...process.env,
71+
// No vp, vp.cmd or node launcher can be found on PATH.
72+
PATH: root,
73+
Path: root,
74+
CALL_FILE: NodePath.join(root, "call.json"),
75+
CHILD_CWD: cwd,
76+
ARG_MARKER: "inherited",
77+
EXIT_STATUS: String(status),
78+
},
79+
},
80+
);
81+
},
82+
call: () => JSON.parse(NodeFS.readFileSync(NodePath.join(root, "call.json"), "utf8")),
83+
[Symbol.dispose]() {
84+
NodeFS.rmSync(root, { recursive: true, force: true });
85+
},
86+
};
87+
}
88+
89+
it.each(["linux", "darwin"] as const)("keeps the existing %s launcher", (platform) => {
90+
const args = ["fmt", "--check", "a file.ts"];
91+
expect(vitePlusCommand(args, platform)).toEqual(["vp", args]);
92+
});
93+
94+
it.each(["sync", "async"])("launches the Windows CLI without a shim (%s)", (mode) => {
95+
using project = fixture();
96+
const args = [
97+
"fmt",
98+
"--check",
99+
"space & (draft) %PATH% ! ^ 研究.ts",
100+
'embedded"quote',
101+
"trailing\\",
102+
"",
103+
"& echo injected>injected.txt",
104+
];
105+
const result = project.run(mode, args, 0);
106+
expect(result.error).toBeUndefined();
107+
expect(result.status).toBe(0);
108+
expect(result.stdout).toBe("vp stdout");
109+
expect(result.stderr).toBe("vp stderr");
110+
expect(project.call()).toEqual({ args, cwd: project.cwd, marker: "inherited" });
111+
expect(NodeFS.existsSync(NodePath.join(project.cwd, "injected.txt"))).toBe(false);
112+
});
113+
114+
it.each(["sync", "async"])("preserves a failing CLI's output and status (%s)", (mode) => {
115+
using project = fixture();
116+
const args = ["install", "--lockfile-only", "--ignore-scripts"];
117+
const result = project.run(mode, args, 23);
118+
expect(result.status).toBe(23);
119+
expect(result.stdout).toBe("vp stdout");
120+
expect(result.stderr).toBe("vp stderr");
121+
expect(project.call()).toEqual({ args, cwd: project.cwd, marker: "inherited" });
122+
});

‎scripts/check-changed-command.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeURL from "node:url";
3+
4+
export function vitePlusCommand(
5+
args: ReadonlyArray<string>,
6+
// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone Node tooling has no Effect runtime; tests pass the platform explicitly.
7+
platform: NodeJS.Platform = process.platform,
8+
): [command: string, args: Array<string>] {
9+
// Windows cannot spawn the package manager's vp.cmd shim without a shell.
10+
// Use Vite+'s exported JS entry with this Node instead, keeping paths and
11+
// arguments literal. Resolve here, not from the patch check's temporary cwd.
12+
if (platform === "win32") {
13+
return [
14+
process.execPath,
15+
[NodeURL.fileURLToPath(import.meta.resolve("vite-plus/bin")), ...args],
16+
];
17+
}
18+
return ["vp", [...args]];
19+
}

‎scripts/check-changed.test.ts‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeChildProcess from "node:child_process";
33
import * as NodeFS from "node:fs";
44
import * as NodeOS from "node:os";
55
import * as NodePath from "node:path";
6+
import { expect } from "vite-plus/test";
67
import * as NodeServices from "@effect/platform-node/NodeServices";
78
import * as Option from "effect/Option";
89
import { assert, describe, it } from "@effect/vitest";
@@ -264,3 +265,143 @@ it("compares against an explicit ref instead of the closest remote main", async
264265
assert.strictEqual(result.base, project.commits.A);
265266
assert.strictEqual(result.ref, "origin/main");
266267
});
268+
269+
function launcherFixture() {
270+
const root = NodeFS.realpathSync(
271+
NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 checks & (fixture) ")),
272+
);
273+
const write = (file: string, content: string) => {
274+
const path = NodePath.join(root, file);
275+
NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
276+
NodeFS.writeFileSync(path, content);
277+
};
278+
for (const file of [
279+
"scripts/check-changed.ts",
280+
"scripts/check-changed-command.ts",
281+
".github/scripts/ci-changes.cjs",
282+
]) {
283+
write(file, NodeFS.readFileSync(NodePath.join(import.meta.dirname, "..", file), "utf8"));
284+
}
285+
write(".gitignore", "node_modules/\nrecords/\n");
286+
write("pnpm-workspace.yaml", "packages:\n - packages/*\n");
287+
write(
288+
"package.json",
289+
JSON.stringify({ type: "module", scripts: { "knip:check": "knip --include files" } }),
290+
);
291+
write("packages/example/package.json", JSON.stringify({ name: "example" }));
292+
const source = "packages/example/space & (draft) %PATH% ! 研究.ts";
293+
write(source, "export const value = 1;\n");
294+
write(
295+
"node_modules/vite-plus/package.json",
296+
JSON.stringify({ type: "module", exports: { "./bin": "./cli.js" } }),
297+
);
298+
write(
299+
"node_modules/vite-plus/cli.js",
300+
`
301+
import fs from 'node:fs';
302+
import path from 'node:path';
303+
const args = process.argv.slice(2);
304+
fs.writeFileSync(path.join(process.env.RECORDS, process.pid + '.json'), JSON.stringify({ args, cwd: process.cwd() }));
305+
console.log('fixture stdout: ' + args[0]);
306+
console.error('fixture stderr: ' + args[0]);
307+
process.exit(args[0] === process.env.FAIL_STEP ? 23 : 0);
308+
`,
309+
);
310+
// Select the Windows launcher without changing the Effect runtime's host platform.
311+
write(
312+
"scripts/check-changed.ts",
313+
NodeFS.readFileSync(NodePath.join(import.meta.dirname, "check-changed.ts"), "utf8").replace(
314+
"vitePlusCommand(args, platform)",
315+
'vitePlusCommand(args, "win32")',
316+
),
317+
);
318+
for (const name of ["effect", "@effect/platform-node", "@t3tools/shared"]) {
319+
const dest = NodePath.join(root, "node_modules", name);
320+
NodeFS.mkdirSync(NodePath.dirname(dest), { recursive: true });
321+
NodeFS.symlinkSync(
322+
NodeFS.realpathSync(NodePath.join(import.meta.dirname, "node_modules", name)),
323+
dest,
324+
"junction",
325+
);
326+
}
327+
const git = (...args: Array<string>) =>
328+
NodeChildProcess.execFileSync("git", args, { cwd: root, stdio: "pipe", env });
329+
git("init");
330+
git("add", ".");
331+
git(
332+
"-c",
333+
"user.name=Fixture",
334+
"-c",
335+
336+
"-c",
337+
"commit.gpgsign=false",
338+
"-c",
339+
"core.hooksPath=",
340+
"commit",
341+
"-m",
342+
"fixture",
343+
);
344+
const records = NodePath.join(root, "records");
345+
NodeFS.mkdirSync(records);
346+
return {
347+
root,
348+
source,
349+
write,
350+
run(failStep = "") {
351+
return NodeChildProcess.spawnSync(process.execPath, ["scripts/check-changed.ts", "HEAD"], {
352+
cwd: root,
353+
encoding: "utf8",
354+
env: { ...env, TEMP: NodeOS.tmpdir(), RECORDS: records, FAIL_STEP: failStep },
355+
});
356+
},
357+
calls: (): Array<{ args: Array<string>; cwd: string }> =>
358+
NodeFS.readdirSync(records).map((file) =>
359+
JSON.parse(NodeFS.readFileSync(NodePath.join(records, file), "utf8")),
360+
),
361+
[Symbol.dispose]() {
362+
NodeFS.rmSync(root, { recursive: true, force: true });
363+
},
364+
};
365+
}
366+
367+
it("checks changed source paths through the Windows launcher", () => {
368+
using project = launcherFixture();
369+
project.write(project.source, "export const value = 2;\n");
370+
const result = project.run();
371+
expect(result.status, result.stdout + result.stderr).toBe(0);
372+
const calls = project.calls();
373+
expect(calls.find(({ args }) => args[0] === "lint")).toEqual({
374+
args: ["lint", project.source],
375+
cwd: project.root,
376+
});
377+
expect(calls.some(({ args }) => args[0] === "fmt" && args.includes(project.source))).toBe(true);
378+
expect(calls.some(({ args }) => args[0] === "run" && args.includes("typecheck"))).toBe(true);
379+
expect(calls.some(({ args }) => args[0] === "install")).toBe(false);
380+
expect(result.stdout).toContain("fixture stdout: lint");
381+
expect(result.stdout).toContain("fixture stderr: lint");
382+
});
383+
384+
it.each(["install", "lint"])("reports a failing %s step with its output", (step) => {
385+
using project = launcherFixture();
386+
project.write(project.source, "export const value = 2;\n");
387+
project.write(
388+
"packages/example/package.json",
389+
JSON.stringify({ name: "example", version: "1.0.1" }),
390+
);
391+
const result = project.run(step);
392+
expect(result.status, result.stdout + result.stderr).toBe(1);
393+
const install = project.calls().find(({ args }) => args[0] === "install");
394+
expect(install?.args, result.stdout + result.stderr).toEqual([
395+
"install",
396+
"--lockfile-only",
397+
"--ignore-scripts",
398+
]);
399+
expect(install?.cwd).not.toBe(project.root);
400+
expect(result.stdout + result.stderr).toContain(`fixture stdout: ${step}`);
401+
expect(result.stdout + result.stderr).toContain(`fixture stderr: ${step}`);
402+
expect(result.stdout).toContain(step === "install" ? "FAIL unused patches" : "FAIL lint");
403+
if (step === "lint") expect(result.stdout).toContain("pass unused patches");
404+
expect(
405+
NodeFS.existsSync(NodePath.join(project.root, "node_modules/.cache/check-changed.json")),
406+
).toBe(false);
407+
});

‎scripts/check-changed.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { Argument, Command, Flag } from "effect/cli";
3939
import { ChildProcess, ChildProcessSpawner } from "effect/process";
4040

4141
import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
42+
import { vitePlusCommand } from "./check-changed-command.ts";
4243

4344
const GiB = 1024 ** 3;
4445

@@ -390,8 +391,14 @@ const checkChanged = Effect.fn("checkChanged")(function* (input: {
390391
const vp = (args: ReadonlyArray<string>, cwd = root) =>
391392
Effect.scoped(
392393
Effect.gen(function* () {
394+
const [command, commandArgs] = vitePlusCommand(args, platform);
393395
const handle = yield* spawner.spawn(
394-
ChildProcess.make("vp", args, { cwd, env, extendEnv: true, forceKillAfter: "5 seconds" }),
396+
ChildProcess.make(command, commandArgs, {
397+
cwd,
398+
env,
399+
extendEnv: true,
400+
forceKillAfter: "5 seconds",
401+
}),
395402
);
396403
const [output, exitCode] = yield* Effect.all(
397404
[Stream.mkString(Stream.decodeText(handle.all)), handle.exitCode],

0 commit comments

Comments
 (0)