Skip to content

Commit 3a2b428

Browse files
committed
chore(gates): parse the workspace manifests once for the package-isolation walks
The test-kit shape check and the shared isolation walk each parsed every workspace manifest. The walk now takes the parsed map, so the caller parses once at the boundary.
1 parent 54e0f03 commit 3a2b428

5 files changed

Lines changed: 38 additions & 21 deletions

File tree

‎scripts/layering/check.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ import { sourceExecutionCompatibilityViolations } from './source-execution-polic
161161
import { sessionResourceOwnershipViolations } from './session-resource-ownership.ts';
162162
import { applicationLifecycleOwnershipViolations } from './application-lifecycle-policy.ts';
163163
import { iosSnapshotEngineOwnershipViolations } from './ios-snapshot-engine-policy.ts';
164-
import { checkPackageIsolation, PLUGIN_FRAMEWORK_ISOLATION } from './package-isolation.ts';
164+
import {
165+
checkPackageIsolation,
166+
parseManifests,
167+
PLUGIN_FRAMEWORK_ISOLATION,
168+
} from './package-isolation.ts';
165169
import { providerSnapshotPresentationViolations } from './provider-snapshot-presentation-policy.ts';
166170
import { snapshotAssemblyPresentationViolations } from './snapshot-assembly-presentation-policy.ts';
167171
import { RETIRED_PATH_RULES, retiredPathRuleViolations } from './retired-paths-policy.ts';
@@ -620,11 +624,14 @@ export const LAYERING_RULES: Readonly<Record<LayeringRuleId, LayeringRule>> = {
620624
context.ratchets.testOnlyExports,
621625
context.reference.testOnlyExports,
622626
),
623-
...checkTestKitIsolation(readTrackedPackageManifests(repoRoot), context.allTypeScriptSources),
627+
...checkTestKitIsolation(
628+
parseManifests(readTrackedPackageManifests(repoRoot)),
629+
context.allTypeScriptSources,
630+
),
624631
],
625632
'plugin-framework-independence': (context) =>
626633
checkPackageIsolation(
627-
readTrackedPackageManifests(repoRoot),
634+
parseManifests(readTrackedPackageManifests(repoRoot)),
628635
context.allTypeScriptSources,
629636
PLUGIN_FRAMEWORK_ISOLATION,
630637
),

‎scripts/layering/package-isolation.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from 'node:path';
88
import { test } from 'node:test';
99
import {
1010
checkPackageIsolation,
11+
parseManifests,
1112
PLUGIN_FRAMEWORK_INDEPENDENCE_RULE,
1213
PLUGIN_FRAMEWORK_ISOLATION,
1314
TEST_KIT_ISOLATION,
@@ -69,7 +70,11 @@ function check(
6970
overrides: Record<string, Manifest> = {},
7071
sources: Record<string, string> = {},
7172
) {
72-
return checkPackageIsolation(workspace(overrides), new Map(Object.entries(sources)), policy);
73+
return checkPackageIsolation(
74+
parseManifests(workspace(overrides)),
75+
new Map(Object.entries(sources)),
76+
policy,
77+
);
7378
}
7479

7580
function source(policy: PackageIsolationPolicy, text: string) {
@@ -83,15 +88,19 @@ test('the real repository holds every isolation policy', () => {
8388
fs.readFileSync(path.join(repoRoot, file), 'utf8'),
8489
]),
8590
);
86-
const manifests = readTrackedPackageManifests(repoRoot);
91+
const manifests = parseManifests(readTrackedPackageManifests(repoRoot));
8792
for (const policy of POLICIES) {
8893
assert.deepEqual(checkPackageIsolation(manifests, sources, policy), [], policy.name);
8994
}
9095
});
9196

9297
test('a repo without the isolated package has nothing to isolate', () => {
9398
for (const policy of POLICIES) {
94-
assert.deepEqual(checkPackageIsolation(new Map(), new Map(), policy), [], policy.name);
99+
assert.deepEqual(
100+
checkPackageIsolation(parseManifests(new Map()), new Map(), policy),
101+
[],
102+
policy.name,
103+
);
95104
}
96105
});
97106

‎scripts/layering/package-isolation.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const PLUGIN_FRAMEWORK_ISOLATION: PackageIsolationPolicy = {
5858
remedy: 'Provider knowledge reaches the loader as a policy from the composing root.',
5959
};
6060

61-
type DependencyManifest = {
61+
export type DependencyManifest = {
6262
name?: string;
6363
exports?: unknown;
6464
dependencies?: Record<string, string>;
@@ -67,9 +67,9 @@ type DependencyManifest = {
6767
peerDependencies?: Record<string, string>;
6868
};
6969

70-
export function parseManifests(
71-
manifests: ReadonlyMap<string, string>,
72-
): Map<string, DependencyManifest> {
70+
export type ParsedManifests = ReadonlyMap<string, DependencyManifest>;
71+
72+
export function parseManifests(manifests: ReadonlyMap<string, string>): ParsedManifests {
7373
return new Map(
7474
[...manifests].map(([file, text]) => [file, JSON.parse(text) as DependencyManifest]),
7575
);
@@ -87,10 +87,7 @@ function declaredNames(manifest: DependencyManifest): string[] {
8787

8888
const ROOT_PACKAGE = 'agent-device';
8989

90-
function forbiddenNames(
91-
parsed: ReadonlyMap<string, DependencyManifest>,
92-
policy: PackageIsolationPolicy,
93-
): Set<string> {
90+
function forbiddenNames(parsed: ParsedManifests, policy: PackageIsolationPolicy): Set<string> {
9491
const names = new Set(policy.forbidsRoot ? [ROOT_PACKAGE] : []);
9592
for (const [file, manifest] of parsed) {
9693
if (policy.forbiddenManifests.test(file) && manifest.name) names.add(manifest.name);
@@ -109,7 +106,7 @@ function packageName(specifier: string): string {
109106
* that declares the policy's package back closes a project-reference cycle.
110107
*/
111108
function manifestViolations(
112-
parsed: ReadonlyMap<string, DependencyManifest>,
109+
parsed: ParsedManifests,
113110
policy: PackageIsolationPolicy,
114111
forbidden: ReadonlySet<string>,
115112
): LayeringViolation[] {
@@ -161,11 +158,10 @@ function manifestViolations(
161158
* declaring it back.
162159
*/
163160
export function checkPackageIsolation(
164-
manifests: ReadonlyMap<string, string>,
161+
parsed: ParsedManifests,
165162
sources: ReadonlyMap<string, string>,
166163
policy: PackageIsolationPolicy,
167164
): LayeringViolation[] {
168-
const parsed = parseManifests(manifests);
169165
if (!parsed.has(`${policy.dir}/package.json`)) return [];
170166
const forbidden = forbiddenNames(parsed, policy);
171167
const violations = manifestViolations(parsed, policy, forbidden);

‎scripts/layering/test-kit-isolation.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import assert from 'node:assert/strict';
55
import { test } from 'node:test';
6+
import { parseManifests } from './package-isolation.ts';
67
import { checkTestKitIsolation } from './test-kit-isolation.ts';
78

89
type Manifest = Record<string, unknown>;
@@ -46,7 +47,7 @@ const SOURCE = new Map([
4647
]);
4748

4849
function check(overrides: Record<string, Manifest> = {}, sources = SOURCE) {
49-
return checkTestKitIsolation(workspace(overrides), sources);
50+
return checkTestKitIsolation(parseManifests(workspace(overrides)), sources);
5051
}
5152

5253
test('the test-kit shape holds: one entry, built only on packages below it', () => {

‎scripts/layering/test-kit-isolation.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
import type { LayeringViolation } from './model.ts';
1515
import { PACKAGE_EXPORT_SURFACE_RULE, TEST_KIT_PACKAGE } from './package-export-surface.ts';
16-
import { checkPackageIsolation, parseManifests, TEST_KIT_ISOLATION } from './package-isolation.ts';
16+
import {
17+
checkPackageIsolation,
18+
TEST_KIT_ISOLATION,
19+
type ParsedManifests,
20+
} from './package-isolation.ts';
1721

1822
const TEST_KIT_DIR = TEST_KIT_ISOLATION.dir;
1923
const TEST_KIT_ENTRY = './src/index.ts';
@@ -34,12 +38,12 @@ function violation(file: string, message: string, line = 1): LayeringViolation {
3438
* other packages or by import; no package it reaches declaring it back).
3539
*/
3640
export function checkTestKitIsolation(
37-
manifests: ReadonlyMap<string, string>,
41+
manifests: ParsedManifests,
3842
sources: ReadonlyMap<string, string>,
3943
): LayeringViolation[] {
4044
const violations: LayeringViolation[] = [];
4145
const testKitFile = `${TEST_KIT_DIR}/package.json`;
42-
const testKit = parseManifests(manifests).get(testKitFile);
46+
const testKit = manifests.get(testKitFile);
4347
if (!testKit) return violations;
4448

4549
const exported = (testKit.exports as Record<string, unknown> | undefined) ?? {};

0 commit comments

Comments
 (0)