Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- In JavaScript, TypeScript, Python, Go, Rust, Java and Kotlin, a function named in the value of a top-level constant or variable, or of a class field, like `const routes = { home }`, `HANDLERS = {"home": home}`, a Go `map[string]func()` table, a Rust `static` list of functions, `static handlers = { click: onClick }`, a Java `List.of(Registry::home)` field or a Kotlin `listOf(::home)` property, is now referenced once, by the constant, variable, field or property that holds it. The file or class around it no longer also shows up as a second reference from the same place, so a function's callers and impact list each table once.
- `codegraph sync` and the file watcher now follow an edit that changes which declaration a module exports as its default, like `export default A` becoming `export default B` while `A` stays, or the module dropping its default. References through `import Y from './x'`, `{ default as Y }`, `require('./x').default`, a barrel that passes the default on or a route that renders it as a layout, and calls on a value typed by it, now link to the new default, or stay unlinked, as a fresh index has them. Before, they kept pointing at the old declaration until the importing file changed or the project was indexed again.
- `codegraph sync` and the file watcher now relink a named import that a name match had linked to a declaration elsewhere while its module didn't export the name, once an edit makes the module export it, like `import { EmptyState } from './layouts/shared'` after the barrel gains `export { EmptyState } from './EmptyState'`. An import a name match had linked to a class the module exported only as its default comes unlinked once the module stops exporting it. Before, the import, the references through it and calls on values typed by it kept the old link until the importing file changed or the project was indexed again.
- In JavaScript, calling a member a required module doesn't export, like `utils.missing()` when `utils` has no `missing`, no longer links to an unrelated class's method of the same name, so it stays unresolved; a module that exports a class, or an instance of one, still links to that class's methods. (#3272)

## [1.6.2] - 2026-10-03

Expand Down
207 changes: 207 additions & 0 deletions __tests__/require-missing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { CodeGraph } from '../src';
import type { Edge } from '../src/types';

let root = '';
let cg: CodeGraph;

beforeEach(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), `cg-require-missing-`));
});

afterEach(() => {
cg?.close();
if (root) fs.rmSync(root, { recursive: true, force: true });
});

const node = (file: string, name: string) => {
const found = cg.getNodesInFile(file).find((n) => n.name === name && n.kind !== 'import');
expect(found, `${file}: ${name}`).toBeDefined();
return found!;
};

/** The edges into `target`, as `kind source-file resolvedBy`. */
const into = (target: { id: string }, kinds: Edge['kind'][]) =>
cg
.getIncomingEdges(target.id)
.filter((e) => kinds.includes(e.kind))
.map((e) => `${e.kind} ${cg.getNode(e.source)!.filePath} ${(e.metadata as { resolvedBy?: string }).resolvedBy}`)
.sort();

/** The call and instantiation edges out of a file's nodes, as `kind target-file:target-name`. */
const outOf = (file: string) =>
cg
.getOutgoingEdgesFrom(cg.getNodesInFile(file).map((n) => n.id))
.filter((e) => e.kind === 'calls' || e.kind === 'instantiates')
.map((e) => {
const target = cg.getNode(e.target)!;
return `${e.kind} ${target.filePath}:${target.name}`;
})
.sort();

describe('require() missing member on a known local module stays unresolved', () => {
// Main case
it('main case: require(\'./utils\').missing should stay unresolved', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `exports.known = function known() { return 1; };`,
'decoy.js': `export class Decoy { missing() { return 42; } }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual([]);
expect(into(node('utils.js', 'known'), ['calls'])).toEqual([]);
});

// Variant 1: no decoy.js
it('variant 1: no decoy.js', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `exports.known = function known() { return 1; };`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual([]);
});

// Variant 2: Decoy is a standalone export function missing()
it('variant 2: Decoy is a standalone export function missing()', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `exports.known = function known() { return 1; };`,
'decoy.js': `export function missing() { return 42; }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual([]);
});

// Variant 3: utils.js also has exports.missing = function missing() {}
it('variant 3: utils.js also has exports.missing', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `exports.known = function known() { return 1; };
exports.missing = function missing() { return 2; }`,
'decoy.js': `export class Decoy { missing() { return 42; } }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual(['calls utils.js:missing']);
});

// Variant 4: ESM import * as utils from ./utils, no missing export
it('variant 4: ESM import * as utils from ./utils, no missing export', async () => {
const files: Record<string, string> = {
'main.js': `import * as utils from './utils';
function run() { return utils.missing(); }
export { run };`,
'utils.js': `exports.known = function known() { return 1; };`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual([]);
});

// Variant 5: ESM import { default } as utils from ./utils, no missing
it('variant 5: ESM import { default } as utils from ./utils, no missing', async () => {
const files: Record<string, string> = {
'main.js': `import { default as utils } from './utils';
function run() { return utils.missing(); }
export { run };`,
'utils.js': `exports.known = function known() { return 1; };`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual([]);
});

// Variant 6: module.exports = new Widget() — a call on the instance is legitimate
it('variant 6: module.exports = new Widget()', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `class Widget { missing() { return 42; } }
module.exports = new Widget();`,
'decoy.js': `export class Decoy { missing() { return 42; } }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual(['calls utils.js:missing']);
});

// Variant 7: module.exports = class Widget { missing() {} }
it('variant 7: module.exports = class Widget with an instance method', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `module.exports = class Widget { missing() { return 42; } };`,
'decoy.js': `export class Decoy { missing() { return 42; } }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual(['calls utils.js:missing']);
});

// Variant 8: module.exports = class Widget { static missing() {} }
it('variant 8: module.exports = class Widget with a static method', async () => {
const files: Record<string, string> = {
'main.js': `const utils = require('./utils');
function run() { return utils.missing(); }
module.exports = run;`,
'utils.js': `module.exports = class Widget { static missing() { return 42; } };`,
'decoy.js': `export class Decoy { missing() { return 42; } }`,
'package.json': JSON.stringify({ name: 'test-3272', devDependencies: {} }),
};
for (const [rel, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
cg = await CodeGraph.init(root, { index: true });
expect(outOf('main.js')).toEqual(['calls utils.js:missing']);
});
});
Loading