Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

Merge branch 'main' into fix/import-resolver-nodenext-rewrite

Tirth Kanani143b4e492f7b
diff --cc tests/skill/understand/test_extract_import_map.test.mjs
index e25a644f,0f172774..8a7e0198
--- a/tests/skill/understand/test_extract_import_map.test.mjs
+++ b/tests/skill/understand/test_extract_import_map.test.mjs
@@@ -238,149 -238,110 +238,254 @@@ describe('extract-import-map.mjs — Ty
);
});
+ // ── Issue #214: tsconfig path-alias targets with leading "./" ───────────
+ // create-next-app ships `"@/*": ["./*"]` as the default. With a root
+ // tsconfig the candidate would stay as "./lib/thing" while ctx.fileSet
+ // stores normalized "lib/thing", silently dropping every cross-module
+ // import edge. Three originally broken cases plus one regression guard
+ // for the already working `["*"]` form.
+
+ it('resolves tsconfig paths with leading "./" target and no baseUrl (#214)', () => {
+ projectRoot = setupTree({
+ 'tsconfig.json': JSON.stringify({
+ compilerOptions: {
+ paths: { '@/*': ['./*'] },
+ },
+ }),
+ 'src/app.ts': `import { x } from '@/lib/thing';\nconst _ = x;\n`,
+ 'lib/thing.ts': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'tsconfig.json', language: 'json', fileCategory: 'config' },
+ { path: 'src/app.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'lib/thing.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/app.ts']).toContain('lib/thing.ts');
+ });
+
+ it('resolves tsconfig paths with leading "./" target and baseUrl "." (#214)', () => {
+ projectRoot = setupTree({
+ 'tsconfig.json': JSON.stringify({
+ compilerOptions: {
+ baseUrl: '.',
+ paths: { '@/*': ['./*'] },
+ },
+ }),
+ 'src/app.ts': `import { x } from '@/lib/thing';\nconst _ = x;\n`,
+ 'lib/thing.ts': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'tsconfig.json', language: 'json', fileCategory: 'config' },
+ { path: 'src/app.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'lib/thing.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/app.ts']).toContain('lib/thing.ts');
+ });
+
+ it('resolves tsconfig paths with leading "./" target and baseUrl "src" (#214)', () => {
+ projectRoot = setupTree({
+ 'tsconfig.json': JSON.stringify({
+ compilerOptions: {
+ baseUrl: 'src',
+ paths: { '@/*': ['./*'] },
+ },
+ }),
+ 'src/app.ts': `import { x } from '@/thing';\nconst _ = x;\n`,
+ 'src/thing.ts': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'tsconfig.json', language: 'json', fileCategory: 'config' },
+ { path: 'src/app.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/thing.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/app.ts']).toContain('src/thing.ts');
+ });
+
+ it('keeps resolving tsconfig paths with bare "*" target (#214 regression guard)', () => {
+ projectRoot = setupTree({
+ 'tsconfig.json': JSON.stringify({
+ compilerOptions: {
+ paths: { '@/*': ['*'] },
+ },
+ }),
+ 'src/app.ts': `import { x } from '@/lib/thing';\nconst _ = x;\n`,
+ 'lib/thing.ts': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'tsconfig.json', language: 'json', fileCategory: 'config' },
+ { path: 'src/app.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'lib/thing.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/app.ts']).toContain('lib/thing.ts');
+ });
++
+ // ── #294: NodeNext / ESM TypeScript `.js → .ts` rewrite ────────────────
+ //
+ // Under `moduleResolution: NodeNext`, TypeScript does NOT rewrite import
+ // specifiers during compilation — what you write in the .ts source is
+ // emitted verbatim. Because Node's ESM loader requires explicit file
+ // extensions at runtime, the TS source must already spell the import with
+ // the `.js` extension that will only be correct AFTER compilation:
+ //
+ // import { x } from './config.js'; // on disk: config.ts
+ //
+ // Before the fix, every such import resolved to null, leaving ESM-TS
+ // projects with a near-edgeless knowledge graph.
+
+ it('resolves NodeNext .js → .ts relative imports (the main #294 case)', () => {
+ projectRoot = setupTree({
+ 'src/index.ts': `import { resolveBackend } from './llm-backend-selector.js';\nimport { loadConfig } from './config.js';\n`,
+ 'src/llm-backend-selector.ts': `export function resolveBackend() {}\n`,
+ 'src/config.ts': `export function loadConfig() {}\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/llm-backend-selector.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/config.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.ts']).toEqual([
+ 'src/config.ts',
+ 'src/llm-backend-selector.ts',
+ ]);
+ });
+
+ it('resolves NodeNext .jsx → .tsx and .mjs → .mts rewrites', () => {
+ projectRoot = setupTree({
+ 'src/index.ts': `import Comp from './Comp.jsx';\nimport { fn } from './worker.mjs';\n`,
+ 'src/Comp.tsx': `export default function Comp() {}\n`,
+ 'src/worker.mts': `export function fn() {}\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/Comp.tsx', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/worker.mts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.ts']).toEqual([
+ 'src/Comp.tsx',
+ 'src/worker.mts',
+ ]);
+ });
+
+ it('resolves to the .js when both .ts and .js exist on disk', () => {
+ // Rare but possible during a partial migration: both `config.ts` and
+ // `config.js` exist. An `import './config.js'` is an exact-disk match
+ // and should resolve to that exact file — the NodeNext rewrite only
+ // kicks in when the .js *doesn't* exist on disk. We assert this to
+ // pin the disambiguation and avoid future regressions where the rewrite
+ // accidentally prefers `.ts` over an existing `.js`.
+ projectRoot = setupTree({
+ 'src/index.ts': `import { x } from './config.js';\n`,
+ 'src/config.ts': `export const x = 1;\n`,
+ 'src/config.js': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/config.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/config.js', language: 'javascript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.ts']).toEqual(['src/config.js']);
+ });
+
+ it('still resolves traditional .js → .js imports unchanged', () => {
+ // The rewrite must not break the case where `.js` IS the real file on
+ // disk (pure JavaScript projects, untyped libraries).
+ projectRoot = setupTree({
+ 'src/index.js': `import { x } from './util.js';\n`,
+ 'src/util.js': `export const x = 1;\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.js', language: 'javascript', fileCategory: 'code' },
+ { path: 'src/util.js', language: 'javascript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.js']).toEqual(['src/util.js']);
+ });
+
+ it('leaves the historical "no extension" probe behaviour intact', () => {
+ // An import like `./utils` (no extension) must still go through the
+ // append-extensions loop and resolve to `./utils.ts` — the new rewrite
+ // path is only triggered when the import already ends with a compiled
+ // extension.
+ projectRoot = setupTree({
+ 'src/index.ts': `import { foo } from './utils';\n`,
+ 'src/utils.ts': `export function foo() {}\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/utils.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.ts']).toEqual(['src/utils.ts']);
+ });
+
+ it('returns null (no resolution) for a .js import whose .ts source is missing', () => {
+ // The rewrite must NOT silently invent a target when neither the .js nor
+ // the .ts file exists. The old behaviour would also return null for this
+ // case — we're verifying the rewrite path doesn't regress it.
+ projectRoot = setupTree({
+ 'src/index.ts': `import './completely-missing.js';\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [{ path: 'src/index.ts', language: 'typescript', fileCategory: 'code' }],
+ });
+
+ expect(result.status).toBe(0);
+ expect(result.output.importMap['src/index.ts']).toEqual([]);
+ });
});
describe('extract-import-map.mjs — Python resolver', () => {