perf(update): reuse shared chunk scans during candidate builds (#158296)

This commit is contained in:
Peter Steinberger 2026-09-25 13:12:33 -07:00 • committed by GitHub
parent a496098eef
commit 37b7e78134
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 16 deletions

View file

@ -243,6 +243,7 @@ function resolveBuiltChunkPath(importerPath: string, specifier: string): string
function collectBuiltModuleStaticDependencies(
entryPath: string,
parser: NativeTypeScriptParser,
moduleSpecifiers: Map<string, string[]>,
): Map<string, string> {
const dependencies = new Map<string, string>();
const visited = new Set<string>();
@ -253,13 +254,18 @@ function collectBuiltModuleStaticDependencies(
continue;
}
visited.add(filePath);
let source: string;
try {
source = fs.readFileSync(filePath, "utf8");
} catch {
continue;
let specifiers = moduleSpecifiers.get(filePath);
if (!specifiers) {
let source: string;
try {
source = fs.readFileSync(filePath, "utf8");
} catch {
continue;
}
specifiers = parseStaticModuleSpecifiers(parser.parseSourceFile(filePath, source));
moduleSpecifiers.set(filePath, specifiers);
}
for (const reference of parseStaticModuleSpecifiers(parser.parseSourceFile(filePath, source))) {
for (const reference of specifiers) {
if (reference.startsWith(".") || reference.startsWith("/")) {
const resolved = resolveBuiltChunkPath(filePath, reference);
if (resolved) {
@ -283,10 +289,13 @@ export function collectBuiltDoctorContractClosureViolations(
const rootDir = path.resolve(params.rootDir ?? ROOT);
using parser = createNativeTypeScriptParser({ cwd: rootDir });
const violations: BuiltDoctorContractClosureViolation[] = [];
// Shared chunks are parsed once per check; each contract retains its own traversal and attribution.
const moduleSpecifiers = new Map<string, string[]>();
for (const module of modules.filter((candidate) => candidate.kind === "doctor-contract")) {
const dependencies = collectBuiltModuleStaticDependencies(
path.join(rootDir, module.relativePath),
parser,
moduleSpecifiers,
);
for (const dependency of FORBIDDEN_DOCTOR_CONTRACT_DEPENDENCIES) {
const importer = dependencies.get(dependency);

View file

@ -199,7 +199,7 @@ try {
describe("built doctor contract closures", () => {
it.each([".js", ".cjs"])(
"follows %s chunk edges to a forbidden runtime dependency",
"checks shared and cyclic %s chunks once while reporting each doctor contract",
(extension) => {
const rootDir = makeRoot(extension);
write(
@ -216,20 +216,28 @@ describe("built doctor contract closures", () => {
? 'module.exports = require("./exec-chunk.cjs");'
: 'export * from "./exec-chunk.js";',
);
write(
rootDir,
`dist/extensions/other/doctor-contract-api${extension}`,
extension === ".cjs"
? 'module.exports = require("../../token-chunk.cjs");'
: 'export * from "../../token-chunk.js";',
);
write(
rootDir,
`dist/exec-chunk${extension}`,
extension === ".cjs"
? 'const exec = require("execa"); exports.rule = exec;'
: 'import "execa"; export const rule = 1;',
? 'require("./token-chunk.cjs"); const exec = require("execa"); exports.rule = exec;'
: 'import "./token-chunk.js"; import "execa"; export const rule = 1;',
);
expect(
collectBuiltDoctorContractClosureViolations(
listBuiltPluginControlPlaneModules({ rootDir }),
{ rootDir },
),
).toEqual([
// Both formats use the same closure check; select the synthetic second plugin explicitly.
const modules = ["demo", "other"].map((pluginId) => ({
pluginId,
kind: "doctor-contract",
relativePath: `dist/extensions/${pluginId}/doctor-contract-api${extension}`,
}));
const readFile = vi.spyOn(fs, "readFileSync");
expect(collectBuiltDoctorContractClosureViolations(modules, { rootDir })).toEqual([
{
pluginId: "demo",
kind: "doctor-contract",
@ -237,7 +245,23 @@ describe("built doctor contract closures", () => {
dependency: "execa",
importerPath: `dist/exec-chunk${extension}`,
},
{
pluginId: "other",
kind: "doctor-contract",
relativePath: `dist/extensions/other/doctor-contract-api${extension}`,
dependency: "execa",
importerPath: `dist/exec-chunk${extension}`,
},
]);
for (const chunk of ["token-chunk", "exec-chunk"]) {
expect(
readFile.mock.calls.filter(
([file]) => file === path.join(rootDir, `dist/${chunk}${extension}`),
),
).toHaveLength(1);
}
write(rootDir, `dist/exec-chunk${extension}`, "");
expect(collectBuiltDoctorContractClosureViolations(modules, { rootDir })).toEqual([]);
},
);