Skip to content

Commit 6e8168a

Browse files
committed
perf(scripts): list ignored check inputs without walking node_modules
1 parent 6577c6b commit 6e8168a

2 files changed

Lines changed: 82 additions & 14 deletions

File tree

‎scripts/check-changed.test.ts‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
defaultConcurrency,
2121
docReaderTests,
2222
gitLines,
23+
ignoredCheckInputs,
2324
runSteps,
2425
splitTests,
2526
type Step,
@@ -271,16 +272,17 @@ function fixture(remotes: Record<string, "A" | "B">) {
271272
};
272273
}
273274

274-
const compare = (root: string, ref: Option.Option<string>) =>
275+
const inFixture = <A, E>(effect: Effect.Effect<A, E, ChildProcessSpawner.ChildProcessSpawner>) =>
275276
Effect.gen(function* () {
276277
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
277-
return yield* comparisonBase(root, ref).pipe(
278+
return yield* effect.pipe(
278279
Effect.provideService(
279280
ChildProcessSpawner.ChildProcessSpawner,
280281
ChildProcessSpawner.make((command) => spawner.spawn(ChildProcess.setEnv(command, env))),
281282
),
282283
);
283284
}).pipe(Effect.provide(NodeServices.layer), Effect.runPromise);
285+
const compare = (root: string, ref: Option.Option<string>) => inFixture(comparisonBase(root, ref));
284286

285287
it.each(["upstream", "team/upstream"])(
286288
"compares against %s/main when the fork's origin/main lags it",
@@ -299,6 +301,44 @@ it("compares against an explicit ref instead of the closest remote main", async
299301
assert.strictEqual(result.ref, "origin/main");
300302
});
301303

304+
it("lists the ignored inputs that main's pathspecs list, without entering node_modules", async () => {
305+
using project = fixture({});
306+
const git = (...args: Array<string>) =>
307+
NodeChildProcess.execFileSync("git", args, { cwd: project.root, encoding: "utf8", env });
308+
const write = (file: string, content = "") => {
309+
NodeFS.mkdirSync(NodePath.dirname(NodePath.join(project.root, file)), { recursive: true });
310+
NodeFS.writeFileSync(NodePath.join(project.root, file), content);
311+
};
312+
write(".gitignore", "node_modules/\ndist/\n.generated/\n.env*\n*.gen.d.ts\nreplaced/\nskipped/");
313+
for (const file of ["replaced", "skipped"]) write(file);
314+
git("add", ".");
315+
git("commit", "-m", "files that directories replace");
316+
git("update-index", "--skip-worktree", "skipped");
317+
for (const file of ["replaced", "skipped"]) NodeFS.rmSync(NodePath.join(project.root, file));
318+
for (const file of [
319+
".env.local",
320+
"src/types.gen.d.ts",
321+
"src/.generated/routes.ts",
322+
"dist/index.d.ts",
323+
"dist/node_modules/pkg/index.d.ts",
324+
"node_modules/pkg/index.d.ts",
325+
// A package folder left behind by a branch switch.
326+
"old/dist/a.d.ts",
327+
"old/node_modules/pkg/index.d.ts",
328+
"replaced/sub/a.d.ts",
329+
"skipped/a.d.ts",
330+
])
331+
write(file);
332+
const command =
333+
"ls-files -o -i --exclude-standard -- :(glob)**/.generated/** :(glob)**/.astro/** :(glob)**/*.d.ts :(glob)**/.env* :(exclude,glob)**/node_modules/**";
334+
const old = git(...command.split(" "))
335+
.split("\n")
336+
.filter(Boolean)
337+
.sort();
338+
assert.includeMembers(old, ["old/dist/a.d.ts", "replaced/sub/a.d.ts", "skipped/a.d.ts"]);
339+
assert.deepStrictEqual(await inFixture(ignoredCheckInputs(project.root)), old);
340+
});
341+
302342
function launcherFixture() {
303343
const root = NodeFS.realpathSync(
304344
NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 checks & (fixture) ")),

‎scripts/check-changed.ts‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ export const git = Effect.fn("checkChanged.git")(function* (
150150
export const gitLines = (cwd: string, args: ReadonlyArray<string>) =>
151151
git(cwd, args).pipe(Effect.map((stdout) => stdout.toString().split("\n").filter(Boolean)));
152152

153+
// Ignored files that checks read: generated sources, declarations, and env files.
154+
const checkInput = /(^|\/)\.(generated|astro)\/|\.d\.ts$|(^|\/)\.env[^/]*$/;
155+
const inNodeModules = /(^|\/)node_modules\//;
156+
157+
// Pathspecs don't stop git from walking an ignored directory, and on Windows it follows pnpm's
158+
// junctions in node_modules until MAX_PATH. `--directory` lists one without entering it.
159+
export const ignoredCheckInputs = Effect.fn("checkChanged.ignoredCheckInputs")(function* (
160+
root: string,
161+
) {
162+
const list = (args: ReadonlyArray<string>) =>
163+
git(root, ["ls-files", "-z", ...args]).pipe(
164+
Effect.map((stdout) => stdout.toString().split("\0").filter(Boolean)),
165+
);
166+
const ignored = ["--others", "--ignored", "--exclude-standard"];
167+
// `--directory` hides a directory where the index has a file; `-k` lists those.
168+
const [listed, replaced] = yield* Effect.all(
169+
[list([...ignored, "--directory"]), list(["-k", "--directory"])],
170+
{ concurrency: 2 },
171+
);
172+
const entries = [...new Set([...listed, ...replaced])].sort();
173+
// git lists an untracked directory whose contents are all ignored together with them;
174+
// listing it again would walk its node_modules.
175+
const directories = entries.filter(
176+
(entry, index) =>
177+
entry.endsWith("/") && !inNodeModules.test(entry) && !entries[index + 1]?.startsWith(entry),
178+
);
179+
const files = entries.filter((entry) => !entry.endsWith("/"));
180+
// 100 MAX_PATH paths fit a Windows command line. `.git/` is never listed; it keeps the
181+
// pathspecs' common prefix out of a nested repository, where git would list nothing.
182+
for (let start = 0; start < directories.length; start += 100) {
183+
const batch = directories.slice(start, start + 100).map((dir) => `:(literal)${dir}`);
184+
files.push(...(yield* list([...ignored, "--", ":(literal).git/", ...batch])));
185+
}
186+
// A nested repository is listed by its directory, as the old pathspecs listed it.
187+
return files
188+
.filter((file) => !inNodeModules.test(file) && (file.endsWith("/") || checkInput.test(file)))
189+
.sort();
190+
});
191+
153192
export const comparisonBase = Effect.fn("checkChanged.comparisonBase")(function* (
154193
root: string,
155194
explicitRef: Option.Option<string>,
@@ -274,18 +313,7 @@ const checkChanged = Effect.fn("checkChanged")(function* (input: {
274313
"--",
275314
]),
276315
);
277-
const ignored = yield* lines(
278-
"ls-files",
279-
"--others",
280-
"--ignored",
281-
"--exclude-standard",
282-
"--",
283-
":(glob)**/.generated/**",
284-
":(glob)**/.astro/**",
285-
":(glob)**/*.d.ts",
286-
":(glob)**/.env*",
287-
":(exclude,glob)**/node_modules/**",
288-
);
316+
const ignored = yield* ignoredCheckInputs(root);
289317
for (const file of [
290318
// Raw bytes of changed files too: .gitattributes normalizes line endings in
291319
// the diff, but the formatter reads the file as it is on disk.

0 commit comments

Comments
 (0)