JS/TS: calls through namespace imports and whole-module CommonJS require lose call edges
Summary
On clean upstream v0.9.105, calls to exported functions through a module binding do not produce a calls edge. This occurs with a JavaScript CommonJS whole-module require and with ES module namespace imports in both JavaScript and TypeScript. Equivalent calls through a destructured CommonJS import or a named ES import do produce the edge.
Both the caller and the target function nodes exist. Missing call edges can make an imported function appear to have no callers.
Reproduction
Run the following with Python and the dependencies installed from a clean checkout of v0.9.105 (for example, install the checkout with python -m pip install .). The script creates its own files and runs a full extraction for each case:
from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
from pathlib import Path
from tempfile import TemporaryDirectory
from graphify.extract import extract
cases = {
'commonjs-member': ('.js', 'function format(x) { return x; }\nmodule.exports = { format };\n', "const helpers = require('./helpers');\nfunction go(x) { return helpers.format(x); }\n"),
'commonjs-destructured': ('.js', 'function format(x) { return x; }\nmodule.exports = { format };\n', "const { format } = require('./helpers');\nfunction go(x) { return format(x); }\n"),
'esm-namespace-js': ('.js', 'export function format(x) { return x; }\n', "import * as helpers from './helpers.js';\nexport function go(x) { return helpers.format(x); }\n"),
'esm-named-js': ('.js', 'export function format(x) { return x; }\n', "import { format } from './helpers.js';\nexport function go(x) { return format(x); }\n"),
'esm-namespace-ts': ('.ts', 'export function format(x: string) { return x; }\n', "import * as helpers from './helpers';\nexport function go(x: string) { return helpers.format(x); }\n"),
'esm-named-ts': ('.ts', 'export function format(x: string) { return x; }\n', "import { format } from './helpers';\nexport function go(x: string) { return format(x); }\n"),
}
for name, (ext, module, consumer) in cases.items():
with TemporaryDirectory() as tmp:
root = Path(tmp)
helper, user = root / ('helpers' + ext), root / ('user' + ext)
helper.write_text(module, encoding='utf-8')
user.write_text(consumer, encoding='utf-8')
with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
graph = extract([helper, user], root=root, cache_root=root / 'cache', parallel=False)
nodes = {n['id']: n for n in graph['nodes']}
assert any(n.get('label') == 'go()' for n in nodes.values())
assert any(n.get('label') == 'format()' for n in nodes.values())
hits = [(nodes[e['source']]['label'], nodes[e['target']]['label'], Path(nodes[e['target']]['source_file']).name)
for e in graph['edges'] if e['relation'] == 'calls'
and e['source'] in nodes and e['target'] in nodes
and nodes[e['source']]['label'] == 'go()' and nodes[e['target']]['label'] == 'format()']
print(name, sorted(hits))
Observed:
commonjs-member []
commonjs-destructured [('go()', 'format()', 'helpers.js')]
esm-namespace-js []
esm-named-js [('go()', 'format()', 'helpers.js')]
esm-namespace-ts []
esm-named-ts [('go()', 'format()', 'helpers.ts')]
Expected behavior
All six cases should contain a calls edge from go() to format() in the corresponding helpers file.
Related issues
- #3346 reported calls through namespace imports and was closed as completed. The namespace-import case above still reproduces on
v0.9.105; the aliased named-import case from that report is not assessed here. - #5034 concerned a destructured CommonJS constructor call through a barrel. Its original reproduction is a different case; the whole-module member call above does not require a barrel.
- #2041 discusses missing member-call edges in a broader context.
Environment
Clean upstream tag v0.9.105, Python 3.12, Windows. No operating-system dependence has been established.
Thanks for opening this issue, @sub4biz. A maintainer will take a look soon.
If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.
To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.
Fixed in 0.9.106 (now on PyPI) via #5129: calls through an ESM namespace import, a CommonJS whole-module require, or a TS import = require now resolve to the real declaration, fail-closed on unknown/ambiguous modules.