Repository navigation
feat(graph): resolve TypeScript tsconfig path aliases - #867
nguyenduyhung1989 wants to merge 1 commit into
Conversation
resolveImport only followed relative specifiers, so an import through a tsconfig alias (`@core/x`, `@/x`, a baseUrl-relative `core/x`) stayed an external string and no call or reference edge was drawn to the real file. In a repo that uses aliases, `callers`, `blast`, hub ranking in `map` and the coupling-based ranking in `ask` silently missed almost every caller. Read every tsconfig.json, tsconfig.*.json and jsconfig.json once per build (JSONC, relative `extends` chains, `baseUrl`, `paths` with an exact key or one `*`, longest-prefix-wins, several targets), scope each config to the importers under its directory, and resolve through the same candidate order relative imports use. Only JS/TS-family importers are affected; an alias that does not land on an indexed file stays unresolved, so no edge is ever invented. Editing a tsconfig alone does not mark the graph stale, the same limitation `go.mod` has today: the fingerprint tracks source files only. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
🌱 graft blast radius1 area changed → 3 areas can be affected. 10 dependent symbols, depth 2. flowchart TB
A0(("Graph Refresh<br/>5 symbols"))
A1(("Pull Request Review<br/>4 symbols"))
A2(("Engine Core<br/>1 symbol"))
classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
class A0,A1,A2 reached;
Who knows this code — 2 people across 4 areas
Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no All 10 dependent symbols, grouped by areaGraph Refresh — 5 symbols in 3 files
Pull Request Review — 4 symbols in 4 files
Engine Core — 1 symbol in 1 file
Test signal per changed area — 1 ✓Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.
53 test suites also reference this code66 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.
Open the interactive graph → — click an area to see its dependent symbols at file:line. |
| const configs: TsPathConfig[] = []; | ||
| const configFiles = repoFiles | ||
| .map(toPosixPath) | ||
| .filter((file) => /(?:^|\/)(?:tsconfig(?:\..+)?|jsconfig)\.json$/.test(file)) |
Problem
resolveImportonly follows specifiers that start with.. Anything else is treated as an external package. In a TypeScript repo that imports throughtsconfigaliases (@/x,@core/x, or abaseUrl-relativecore/x), every such import stays an unresolved string, so no call or reference edge is drawn to the real file.The failure is silent.
graft callersandgraft blastreturn a short, confident list, the hub ranking ingraft mapfavours code that happens to use relative imports, and the coupling-based ranking ingraft askloses the edges that matter most.Change
src/graph/ts-paths.ts(new): reads everytsconfig.json,tsconfig.*.jsonandjsconfig.jsononce per build from the samerepoFileswalk the rest of the build uses. Handles JSONC (comments, trailing commas), relativeextendschains (also arrays),baseUrl, andpathswith an exact key or a single*(longest prefix wins, several targets tried in order). A config applies only to importers under its own directory; nearest directory first, thentsconfig.json, then the other configs by name.includeandexcludeare deliberately ignored, graph membership still comes from graft's file walk.src/graph/build.ts: passes the compiled aliases toresolveEdges, the same wayreadGoModulesfeedsgoModules.src/graph/resolve.ts: non-relative specifiers from JS/TS-family importers (.ts .tsx .mts .cts .js .jsx .mjs .cjs .vue .svelte .astro) go through the aliases. The candidate order (exact, extension swap,/index.*) is now one helper shared with relative imports, and relative behaviour is unchanged. Python importers are untouched.An alias that does not land on an indexed file stays unresolved, exactly as today, so the change cannot invent an edge.
Evidence
Measured on a 2,100-file TypeScript repo (backend and frontend
tsconfigboth definepaths). For 7 exported symbols, the "truth" is every file that mentions the symbol on a non-comment line, excluding the defining file (410 files in total).graft callers <symbol> --depth 1:build --lsp(typescript-language-server)The remaining misses are barrel files (
export { x } from './y'), which graft records as an import but not as a reference to the symbol. That is a separate question and this PR does not touch it.For six plain-language "where is X" questions, the expected file was ranked first for 6 of 6 (5 of 6 before). A full build took the same time as before (about 27 s on this repo);
--lsptook 2 min 54 s.Tests
test/graph-resolve-ts-paths.test.ts(11 tests): wildcard alias to a directory index, exact andprefix*suffixaliases, longest prefix wins,baseUrlonly (an unknown bare specifier such asreactstays external),pathswithoutbaseUrl, anextendschain with a child overridingpaths, JSONC, monorepo scoping with two configs defining@/*differently, several targets (first existing wins), Python importers and relative specifiers unchanged, an alias to a missing file stays unresolved, and an end-to-endbuildGraphcheck that incomingcallsedges appear for an aliased import. Written red first (8 of the new tests failed before the change).tsc --noEmitis clean, and alltest/graph-*.test.tsplusgeneric-extract.test.tspass (487 run, 0 failed, 4 skipped).Known limitation
Editing a
tsconfigon its own does not mark the graph stale, because the fingerprint tracks source files only.go.modhas the same limitation today. The next source change orgraft buildpicks the new aliases up.🤖 Generated with Claude Code