Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions scripts/checks/source-architecture.mts
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,13 @@ export function analyzeSourceArchitecture(
repoRoot = REPO_ROOT,
options: AnalyzeOptions = {},
): SourceArchitectureReport {
const canonicalRepoRoot = realpathSync(repoRoot);
const scanRoots = options.scanRoots ?? DEFAULT_SCAN_ROOTS;
const rootFileDirectories = options.rootFileDirectories ?? [];
const absoluteFiles = [
...new Set(scanRoots.flatMap((root) => [...walkSourceFiles(path.join(repoRoot, root))])),
...new Set(
scanRoots.flatMap((root) => [...walkSourceFiles(path.join(canonicalRepoRoot, root))]),
),
].sort();
const sourceFiles = new Set(absoluteFiles);
const edges = new Map<string, Set<string>>();
Expand All @@ -304,21 +307,27 @@ export function analyzeSourceArchitecture(
}
}

const files = absoluteFiles.map((file) => toRepoPath(repoRoot, file));
const files = absoluteFiles.map((file) => toRepoPath(canonicalRepoRoot, file));
const fanIn = Object.fromEntries(
absoluteFiles.map((file) => [toRepoPath(repoRoot, file), fanInByAbsolutePath.get(file) ?? 0]),
absoluteFiles.map((file) => [
toRepoPath(canonicalRepoRoot, file),
fanInByAbsolutePath.get(file) ?? 0,
]),
);
const fanOut = Object.fromEntries(
absoluteFiles.map((file) => [toRepoPath(repoRoot, file), edges.get(file)?.size ?? 0]),
absoluteFiles.map((file) => [toRepoPath(canonicalRepoRoot, file), edges.get(file)?.size ?? 0]),
);
const repoEdges = new Map(
absoluteFiles.map((file) => [
toRepoPath(repoRoot, file),
new Set([...(edges.get(file) ?? [])].map((target) => toRepoPath(repoRoot, target))),
toRepoPath(canonicalRepoRoot, file),
new Set([...(edges.get(file) ?? [])].map((target) => toRepoPath(canonicalRepoRoot, target))),
]),
);
const rootFiles = Object.fromEntries(
rootFileDirectories.map((directory) => [directory, countRootFiles(repoRoot, directory)]),
rootFileDirectories.map((directory) => [
directory,
countRootFiles(canonicalRepoRoot, directory),
]),
);

return {
Expand Down
25 changes: 25 additions & 0 deletions test/source-architecture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ describe("source architecture budget (#7692)", () => {
expect(report.fanIn).toMatchObject({ "bin/b.js": 1, "src/b.ts": 1 });
});

test("reports repository-relative paths through an aliased repository root (#7692)", ({
resources,
}) => {
const root = resources.temporaryDirectory("nemoclaw-architecture-canonical-root-");
const aliasRoot = path.join(
resources.temporaryDirectory("nemoclaw-architecture-root-alias-"),
"repo",
);
writeModule(root, "src/a.ts", 'import "./b";\n');
writeModule(root, "src/b.ts", "export {};\n");
fs.symlinkSync(root, aliasRoot, process.platform === "win32" ? "junction" : "dir");

const report = analyzeSourceArchitecture(aliasRoot, {
scanRoots: ["src"],
rootFileDirectories: ["src"],
});

expect(report).toMatchObject({
files: ["src/a.ts", "src/b.ts"],
fanIn: { "src/a.ts": 0, "src/b.ts": 1 },
fanOut: { "src/a.ts": 1, "src/b.ts": 0 },
rootFiles: { src: 2 },
});
});

test("ignores type-only dependency cycles", ({ resources }) => {
const root = resources.temporaryDirectory("nemoclaw-architecture-types-");
writeModule(root, "src/a.ts", 'import type { B } from "./b";\nexport type A = B;\n');
Expand Down
Loading