Skip to content

Commit 78a7a07

Browse files
committed
chore(gates): name the added modules and their import paths when an eager closure grows
The no-growth diagnostic in scripts/__tests__/eager-closure-budgets.ts only named the FIRST newly evaluated module and always advised a dynamic import. On #2423 that sent five reviewers toward the wrong fix when the growth was a small new module that belonged in a module every affected entry already evaluated -- the dynamic-import advice was never coherent for a brand-new module with no old edge to defer. - describeClosureGrowth now lists every added module (bounded to 10), each with the shortest static import route from the entry to it. - describeSharedGrowthHomes runs once after every entry is evaluated: when two or more entries grew by the same added module, it names the modules they already evaluate at the merge-base under that module's own package -- candidate homes, not a verdict. - classifyGrowth's closing advice now states the two common causes (a new static edge, or something that used to load lazily) and the two remedies (give the symbol a home in a module already in the closure, or make the new edge lazy) instead of prescribing one fix. The verdict logic (when an entry is flagged as having grown) is unchanged.
1 parent 6d08de4 commit 78a7a07

2 files changed

Lines changed: 311 additions & 28 deletions

File tree

‎scripts/__tests__/eager-closure-budgets.test.ts‎

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import {
1111
renamedSince,
1212
} from './committed-source-tree.ts';
1313
import {
14+
addedModules,
1415
APPROVED_OVER_CEILING,
1516
classifyGrowth,
1617
classifyNewEntry,
1718
describeClosureGrowth,
1819
describeClosurePressure,
1920
describePlatformOffenders,
21+
describeSharedGrowthHomes,
2022
discoverFacadeEntryFiles,
2123
eagerClosureEntries,
2224
entryCategoryOf,
@@ -71,6 +73,40 @@ test('no-growth fails growth with both counts and passes an equal or smaller clo
7173
expect(classifyGrowth('x.ts', 42, 43)).toMatch(/evaluates 43 modules.*merge-base evaluated 42/);
7274
});
7375

76+
test('growth advice names both causes and both remedies, not one prescribed fix', () => {
77+
// #2423's review: a message that only ever says "move it behind a dynamic import" is wrong
78+
// advice when the growth is a new module that belongs in an existing one. This pins that the
79+
// verdict states both common causes and leaves the remedy to the reader.
80+
const finding = classifyGrowth('x.ts', 42, 43) ?? '';
81+
expect(finding).toMatch(/new static edge/);
82+
expect(finding).toMatch(/used to load on demand/);
83+
expect(finding).toMatch(/home in a module the closure already evaluates/);
84+
expect(finding).toMatch(/function-scoped `await import`/);
85+
});
86+
87+
test('growth against the merge-base lists every added module, each with its shortest route, bounded', () => {
88+
// The hole: the old diagnostic named only the FIRST added module, which hid the pattern when a
89+
// whole PR's growth was really "everyone reached one new module" (#2423 review) rather than one
90+
// entry's own new edge. This pins that every added module is named (bounded), not just the
91+
// first, and that the route shown is the whole chain, not just the leaf.
92+
const entry = '/repo/entry.ts';
93+
const graph = new Map<string, string | null>([[entry, null]]);
94+
graph.set('/repo/old.ts', entry); // present at the merge-base too: not "added"
95+
graph.set('/repo/new-a.ts', entry);
96+
graph.set('/repo/new-b.ts', '/repo/new-a.ts');
97+
for (let index = 0; index < 12; index += 1) graph.set(`/repo/new-filler-${index}.ts`, entry);
98+
const base = new Set(['/repo/entry.ts', '/repo/old.ts']);
99+
100+
const described = describeClosureGrowth(graph, base, entry, '/repo');
101+
expect(described, 'the route must show the whole chain, not just the leaf').toContain(
102+
'new-a.ts → new-b.ts',
103+
);
104+
expect(described, 'output must be capped and say how much it omitted').toMatch(
105+
/\(\+\d+ more newly evaluated module\(s\)\)/,
106+
);
107+
expect(described.split('\n').length).toBeLessThan(15);
108+
});
109+
74110
test('a first-introduced entry fits its category ceiling or carries an approval', () => {
75111
expect(classifyNewEntry('x.ts', 'vocabulary-facade', 4, false)).toBeNull();
76112
expect(classifyNewEntry('x.ts', 'vocabulary-facade', 5, false)).toMatch(
@@ -179,6 +215,55 @@ test('an entry that evaluates only itself is described without pretending to an
179215
expect(describeClosurePressure(graph, '/repo/solo.ts', '/repo')).toContain('only itself');
180216
});
181217

218+
test('growth shared by two entries names the merge-base modules they already have in common', () => {
219+
// The aggregation #2423's review asked for: two entries independently grow by the SAME new
220+
// module. Per-entry diagnostics would each show that same chain with no hint that a shared
221+
// existing home was available -- this names the modules BOTH entries already evaluate, scoped
222+
// to the added module's own package, once.
223+
const addedFile = '/repo/packages/demo/src/new-thing.ts';
224+
const sharedHome = '/repo/packages/demo/src/shared-home.ts';
225+
const otherPkgFile = '/repo/packages/other/src/not-a-home.ts'; // different package: excluded
226+
const onlyInOne = '/repo/packages/demo/src/only-in-a.ts'; // not common to both: excluded
227+
228+
const graphA = new Map<string, string | null>([
229+
['/repo/entry-a.ts', null],
230+
[sharedHome, '/repo/entry-a.ts'],
231+
[onlyInOne, '/repo/entry-a.ts'],
232+
[otherPkgFile, '/repo/entry-a.ts'],
233+
]);
234+
const graphB = new Map<string, string | null>([
235+
['/repo/entry-b.ts', null],
236+
[sharedHome, '/repo/entry-b.ts'],
237+
[otherPkgFile, '/repo/entry-b.ts'],
238+
]);
239+
240+
const shared = describeSharedGrowthHomes(
241+
[
242+
{ id: 'entry-a.ts', added: [addedFile], baseGraph: graphA },
243+
{ id: 'entry-b.ts', added: [addedFile], baseGraph: graphB },
244+
],
245+
'/repo',
246+
);
247+
248+
expect(shared, 'labeled neutrally, not as a prescribed fix').toMatch(/possible homes/i);
249+
expect(shared).toContain('packages/demo/src/shared-home.ts');
250+
expect(shared, 'only in one entry: not a shared home').not.toContain('only-in-a.ts');
251+
expect(shared, 'a different package than the added module: not a candidate home').not.toContain(
252+
'not-a-home.ts',
253+
);
254+
});
255+
256+
test('growth that no other entry shares produces no shared-homes note', () => {
257+
const graphA = new Map<string, string | null>([['/repo/entry-a.ts', null]]);
258+
expect(
259+
describeSharedGrowthHomes(
260+
[{ id: 'entry-a.ts', added: ['/repo/new.ts'], baseGraph: graphA }],
261+
'/repo',
262+
),
263+
'nothing to aggregate when only one entry grew by this module',
264+
).toBeNull();
265+
});
266+
182267
/**
183268
* A committed fixture repository: one workspace package with a manifest export target, a
184269
* top-level façade, a nested façade, and a test source. Everything here is TRACKED, so anything a
@@ -350,14 +435,14 @@ function basePathOf(entryFile: string): string | null {
350435
return baseTree.isFile(absolute(file)) ? file : null;
351436
}
352437

353-
const baseClosures = new Map<string, ReadonlySet<string>>();
354-
function baseClosureOf(baseFile: string): ReadonlySet<string> {
355-
let closure = baseClosures.get(baseFile);
356-
if (!closure) {
357-
closure = new Set(eagerClosureGraphOf(absolute(baseFile), baseTree).keys());
358-
baseClosures.set(baseFile, closure);
438+
const baseGraphs = new Map<string, ReadonlyMap<string, string | null>>();
439+
function baseGraphOf(baseFile: string): ReadonlyMap<string, string | null> {
440+
let graph = baseGraphs.get(baseFile);
441+
if (!graph) {
442+
graph = eagerClosureGraphOf(absolute(baseFile), baseTree);
443+
baseGraphs.set(baseFile, graph);
359444
}
360-
return closure;
445+
return graph;
361446
}
362447

363448
const platformFacades = entries.filter((entry) => entry.category === 'platform-facade');
@@ -368,6 +453,19 @@ const carried = others.flatMap((entry) => {
368453
});
369454
const introduced = others.filter((entry) => basePathOf(entry.entryFile) === null);
370455

456+
/**
457+
* Every carried entry's growth data, computed once so the per-entry NO-GROWTH test below and the
458+
* cross-entry shared-homes note after it read the same graphs instead of walking each closure
459+
* twice.
460+
*/
461+
const carriedGrowth = carried.map((entry) => {
462+
const entryPath = absolute(entry.entryFile);
463+
const graph = eagerClosureGraphOf(entryPath);
464+
const baseGraph = baseGraphOf(entry.baseFile);
465+
const base = new Set(baseGraph.keys());
466+
return { entry, entryPath, graph, baseGraph, base, added: addedModules(graph, base, entryPath) };
467+
});
468+
371469
test('every hub exists and is not also a discovered façade', () => {
372470
const discovered = new Set(discoverFacadeEntryFiles(repoRoot));
373471
expect(HUB_ENTRY_FILES.filter((file) => !fs.existsSync(absolute(file)))).toEqual([]);
@@ -402,20 +500,37 @@ test.for(platformFacades)('$id evaluates exactly one module: itself', (entry) =>
402500
).toBe(PLATFORM_FACADE_CLOSURE);
403501
});
404502

405-
test.for(carried)('$id evaluates no more modules than at the merge-base', (entry) => {
406-
const entryPath = absolute(entry.entryFile);
407-
const graph = eagerClosureGraphOf(entryPath);
408-
const base = baseClosureOf(entry.baseFile);
503+
test.for(carriedGrowth)('$entry.id evaluates no more modules than at the merge-base', (growth) => {
504+
const { entry, entryPath, graph, base } = growth;
409505
const finding = classifyGrowth(entry.id, base.size, graph.size);
410506
expect(
411507
finding,
412508
finding === null
413509
? ''
414-
: `${finding}\n\nFirst newly evaluated module, by shortest import route:\n` +
510+
: `${finding}\n\nNewly evaluated module(s), each by shortest import route from the entry:\n` +
415511
describeClosureGrowth(graph, base, entryPath, repoRoot),
416512
).toBeNull();
417513
});
418514

515+
test('entries that grow by the same new module report shared merge-base homes once', () => {
516+
// #2423's review: when several entries grow because they all reached the same new module, the
517+
// per-entry test above already fails once per entry -- this adds ONE more diagnostic naming the
518+
// modules they already have in common, instead of leaving a reader to notice the repetition
519+
// across several separate failures by hand. It can only fail alongside at least two already-red
520+
// entries above, so it never changes the gate's own pass/fail verdict.
521+
const growths = carriedGrowth
522+
.filter((growth) => growth.added.length > 0)
523+
.map((growth) => ({ id: growth.entry.id, added: growth.added, baseGraph: growth.baseGraph }));
524+
const shared = describeSharedGrowthHomes(growths, repoRoot);
525+
expect(
526+
shared,
527+
shared === null
528+
? ''
529+
: 'More than one entry grew by the same new module -- see the shared homes below instead ' +
530+
`of chasing each entry's own diagnostic separately:\n\n${shared}`,
531+
).toBeNull();
532+
});
533+
419534
test.for(introduced)(
420535
'$id is first-introduced and fits the $category ceiling or carries an approval',
421536
(entry) => {

0 commit comments

Comments
 (0)