fix(resolution): an import naming the emitted .js extension resolves to its .ts source - #1706
fix(resolution): an import naming the emitted .js extension resolves to its .ts source#1706bompus wants to merge 1 commit into
Conversation
…to its .ts source Under moduleResolution node16/nodenext/bundler the specifier carries the emitted extension and no file of that name exists, so resolveRelativeImport and the aliased path returned null and every imported name fell through to bare-name matching: a method wrapping the same-named helper it imports resolved to itself. Retry with the source extensions TypeScript compiles from when the emitted file is absent; a real .js beside the .ts still wins.
Real-repo check: one edge gained, zero lostRan this against vitejs/vite (1,635 files, 9,354 nodes, 27,499 edges) — index the same checkout with each build, then diff the full edge set joined back to symbol names and call-site lines. Indexing is deterministic, so the diff is this change and nothing else. 27,499 → 27,500 edges. One gained, none lost. The one: which is the fixture line What I found interesting is the shape of the gap. That fixture imports five sibling modules through their emitted extension, and
So the only broken variant was the plainest and by far the most common one — One caveat on scope. This checkout has no kernel binary staged, so extraction ran the wasm path in both arms. Resolution is shared TS and sits downstream of extraction, so the result holds either way, but I have not re-run it against a kernel-extracted graph. |
Fixes #1705.
What
resolveRelativeImportand the aliased path'stryWithExtnow fall back tofindSourceForEmittedSpecifier: when a specifier ends in.js/.jsx/.mjs/.cjsand no file of that exact name exists, the source extensions TypeScript compiles from are tried (.ts,.tsx,.d.ts/.tsx/.mts,.d.mts/.cts,.d.cts). Only for the TS/JS-family languages (typescript,tsx,javascript,jsx,vue,svelte,astro,arkts); a real.jsbeside the.tsstill wins because the existing as-is check runs first.Why
Under
moduleResolution: node16 | nodenext | bundlerthe emitted extension is what TypeScript requires in the specifier, so every import in such a project was invisible to the import resolver and each imported name fell through to bare-name matching. The clearest symptom is a method that wraps the same-named function it imports resolving to itself (acallsself-edge at confidence 0.4); the wider one is that cross-module edges in these projects were name guesses instead of import-backed.Measured
Real repo, 582 files, 92
.tsfiles with.jsspecifiers, indexed with and without this change on the same build:calls/importsedgesresolvedBy: importresolvedBy: exact-matchresolvedBy: fuzzycallsedgesTests
__tests__/import-emitted-specifier.test.ts: unit cases throughresolveImportPathfor.js,.jsx,.mjs,.cjs, the real-.js-wins case, an aliased@/lib/util.js, a missing target, and a non-TS language; plus an end-to-endCodeGraph.initon the wrapper-method repro asserting the edge lands on the imported function and not on the method. Full suite: no failure outside the host's known EPERM/EBUSY teardown set.