Skip to content

Commit 99754dc

Browse files
authored
fix(layering): resolve relative imports inside workspace packages referencing #1781 (#1872)
* fix(layering): resolve relative imports inside workspace packages resolveTargetFile() dropped any relative import whose resolved path didn't start with src/. Since #1490 W0 added packages/*/src/** to the source set, every intra-package relative import (e.g. a facade re-exporting a sibling file) was silently invisible to the layering graph — R4 value-cycle rejection and depgraph reverse reachability both stopped at the package facade. Resolve relative specifiers that land under packages/<name>/src/ too, while still refusing anything outside src/ and packages/*/src/. Verified against the real tree: 448 previously-invisible value edges and 436 type-only edges now resolve, but none of them close a new R4 value cycle or grow the R9 type-cycle SCC, so the R6/R9 baselines are unchanged. Refs #1781 * style: apply oxfmt to regression test
1 parent 8c06965 commit 99754dc

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎scripts/layering/model.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ test('neutral ownership zones reject value imports into higher layers', () => {
111111
});
112112
});
113113

114+
test('a relative import resolves within its own workspace package src/, but not past it', () => {
115+
const edges = resolveImportEdges(
116+
new Map([
117+
['packages/contracts/src/facades/device.ts', "import '../device.ts';"],
118+
['packages/contracts/src/device.ts', 'export const device = true;'],
119+
// A value cycle closed entirely inside one package must be as visible to R4 as
120+
// one closed inside src/ — this is what #1781 A9-2 found invisible: `resolved`
121+
// landed under `packages/<name>/src/`, so the old `resolved.startsWith('src/')`
122+
// check dropped both edges and the reverse-reachability graph stopped at the
123+
// package facade.
124+
['packages/contracts/src/cycle-a.ts', "import '../src/cycle-b.ts';"],
125+
['packages/contracts/src/cycle-b.ts', "import '../src/cycle-a.ts';"],
126+
// A relative path that climbs out of any package's src/ (landing under a bare
127+
// `packages/<name>/` with no `src/` segment) must still be refused by the
128+
// model — R11 owns rejecting that import outright, but the graph itself must
129+
// not silently resolve a target outside src/ and packages/*/src/.
130+
['packages/contracts/src/escape.ts', "import '../../outside-src.ts';"],
131+
['packages/outside-src.ts', 'export const outsideSrc = true;'],
132+
]),
133+
);
134+
135+
assert.deepEqual(edges.map(({ file, target }) => `${file} -> ${target}`).sort(), [
136+
'packages/contracts/src/cycle-a.ts -> packages/contracts/src/cycle-b.ts',
137+
'packages/contracts/src/cycle-b.ts -> packages/contracts/src/cycle-a.ts',
138+
'packages/contracts/src/facades/device.ts -> packages/contracts/src/device.ts',
139+
]);
140+
assert.deepEqual(findValueImportCycles(edges), [
141+
[
142+
'packages/contracts/src/cycle-a.ts',
143+
'packages/contracts/src/cycle-b.ts',
144+
'packages/contracts/src/cycle-a.ts',
145+
],
146+
]);
147+
});
148+
114149
test('type-only edges are ranked by R6 and ignored by R5, and vice versa', () => {
115150
const edges = resolveImportEdges(
116151
new Map([

‎scripts/layering/model.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ export function unclassifiedZones(files: readonly string[]): string[] {
198198
return [...collectZones(files)].filter((zone) => classifyZone(zone) === 'unclassified').sort();
199199
}
200200

201+
// A relative specifier resolves only within a source root: root `src/`, or a workspace
202+
// package's own `src/` (#1490 W0 added `packages/*/src/**` to the source set, but a bare
203+
// `src/`-prefix check left every intra-package relative import — e.g. a facade re-exporting
204+
// a sibling file — unresolved and invisible to the value-cycle and reverse-reachability graphs).
205+
const PACKAGE_SRC_PREFIX = /^packages\/[^/]+\/src\//;
206+
201207
function resolveTargetFile(
202208
fromFile: string,
203209
spec: string,
@@ -225,7 +231,7 @@ function resolveTargetFile(
225231
}
226232
if (!spec.startsWith('.')) return null;
227233
const resolved = path.posix.normalize(path.posix.join(path.posix.dirname(fromFile), spec));
228-
if (!resolved.startsWith('src/')) return null;
234+
if (!resolved.startsWith('src/') && !PACKAGE_SRC_PREFIX.test(resolved)) return null;
229235
const candidates = [
230236
resolved,
231237
resolved.replace(/\.js$/, '.ts'),

0 commit comments

Comments
 (0)