Skip to content

fix(extract,affected): edge import('…') in plain JS/TS, and travers… - #2574

Closed
phudayyy wants to merge 1 commit into
Graphify-Labs:v8from
phudayyy:fix/dynamic-import-edges-js-ts
Closed

phudayyy wants to merge 1 commit into
Graphify-Labs:v8from
phudayyy:fix/dynamic-import-edges-js-ts

Conversation

@phudayyy

@phudayyy phudayyy commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2575

tree-sitter models await import('x') as a call_expression, not an import_statement, so the specifier never reaches the import walk. The Svelte/Astro/Vue extractors already compensate with a regex pass — the docstring in extract_vue says so outright ("A regex pass then recovers import('…') dynamic imports the AST does not edge"). Plain .ts/.js was left out, on the reasonable-sounding basis that its AST pass works. It works for STATIC imports; the dynamic ones fall through silently.

Two independent gaps, and fixing either alone changes nothing:

  1. extract.py — no dynamic-import edge is produced for plain JS/TS at all.
  2. affected.py — dynamic_import was missing from DEFAULT_AFFECTED_RELATIONS, so even the dynamic edges the Svelte/Astro/Vue passes DO emit were invisible to graphify affected.

Measured on a 546-file TypeScript app (graphify 0.9.36/0.9.37, 2026-08-09): 31 real import('…') edges missing from graph.json. The loss compounds with depth because dynamic imports cluster under hub modules — codebases use them precisely to break require cycles, so the missing edges are the load-bearing ones. On that app, affected <file> --depth 3 returned 10 of 30 truly-affected files (recall 0.27); ground truth came from an independently written import resolver, not from graphify. Precision stayed 1.00 throughout: the graph under-reports, it never invents.

With this change, on the same corpus: 31 missing edges -> 0, and recall goes to 1.00 at every depth on all three targets tested, precision still 1.00.

Implementation reuses what already exists — _emit_rescued_import, _load_tsconfig_aliases, _load_tsconfig_base_url — so aliased specifiers (@/agent/runner) resolve through the same path as static ones. The regex is guarded with (?<!\w) so fooimport('x') does not match, and the pass bails early when the file contains no import( at all.

tests/test_js_dynamic_imports.py covers relative, multi-level relative and tsconfig-aliased dynamic imports, asserts a static import still edges alongside a dynamic one, guards the (?<!\w) boundary, and asserts dynamic_import is traversable by affected. Verified to fail without this change: 5 of the 6 fail on v8, all 6 pass with it.

Suite: 6 failed / 3931 passed, byte-identical with and without this change — those 6 are pre-existing in my environment (missing ollama, a wheel-build test, an apm manifest dependency).

…e it

tree-sitter models `await import('x')` as a `call_expression`, not an
`import_statement`, so the specifier never reaches the import walk. The
Svelte/Astro/Vue extractors already compensate with a regex pass — the docstring
in `extract_vue` says so outright ("A regex pass then recovers `import('…')`
dynamic imports the AST does not edge"). Plain .ts/.js was left out, on the
reasonable-sounding basis that its AST pass works. It works for STATIC imports;
the dynamic ones fall through silently.

Two independent gaps, and fixing either alone changes nothing:

1. extract.py — no dynamic-import edge is produced for plain JS/TS at all.
2. affected.py — `dynamic_import` was missing from DEFAULT_AFFECTED_RELATIONS,
   so even the dynamic edges the Svelte/Astro/Vue passes DO emit were invisible
   to `graphify affected`.

Measured on a 546-file TypeScript app (graphify 0.9.36/0.9.37, 2026-08-09):
31 real `import('…')` edges missing from graph.json. The loss compounds with
depth because dynamic imports cluster under hub modules — codebases use them
precisely to break require cycles, so the missing edges are the load-bearing
ones. On that app, `affected <file> --depth 3` returned 10 of 30 truly-affected
files (recall 0.27); ground truth came from an independently written import
resolver, not from graphify. Precision stayed 1.00 throughout: the graph
under-reports, it never invents.

With this change, on the same corpus: 31 missing edges -> 0, and recall goes to
1.00 at every depth on all three targets tested, precision still 1.00.

Implementation reuses what already exists — `_emit_rescued_import`,
`_load_tsconfig_aliases`, `_load_tsconfig_base_url` — so aliased specifiers
(`@/agent/runner`) resolve through the same path as static ones. The regex is
guarded with `(?<!\w)` so `fooimport('x')` does not match, and the pass bails
early when the file contains no `import(` at all.

tests/test_js_dynamic_imports.py covers relative, multi-level relative and
tsconfig-aliased dynamic imports, asserts a static import still edges alongside
a dynamic one, guards the `(?<!\w)` boundary, and asserts `dynamic_import` is
traversable by `affected`. Verified to fail without this change: 5 of the 6
fail on v8, all 6 pass with it.

Suite: 6 failed / 3931 passed, byte-identical with and without this change —
those 6 are pre-existing in my environment (missing `ollama`, a wheel-build
test, an apm manifest dependency).

Co-Authored-By: Claude Opus 5 <[email protected]>

@graphify-labs graphify-labs Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).


Graphify review — findings

This PR adds handling for dynamic import('…') specifiers in plain JS/TS extraction. It introduces a new _rescue_js_dynamic_imports regex-based pass in extract.py (invoked from extract_js) that emits dynamic_import edges, mirroring what the Svelte/Astro/Vue extractors already do, and adds "dynamic_import" to DEFAULT_AFFECTED_RELATIONS so those edges are traversed by affected. A new test file covers relative, multi-level, and tsconfig-aliased dynamic imports, coexistence with static imports, a non-matching identifier case, and the presence of the relation in the affected traversal set.

No blocking issues surfaced. 4 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 1368 functions depend on the 224 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: extract() — 387 callers, 39 callees
  • worse: extract_js() — 75 callers, 3 callees
  • worse: _emit_rescued_import() — 4 callers, 4 callees

Verification — 1368 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1235 function(s) in the blast radius were not formally verified this run

· 3 more finding(s) on lines outside this diff (see the check run).

safishamsi added a commit that referenced this pull request Aug 9, 2026
…2568, #2565, #2561, #2577, #2575)

These changes are interleaved across engine.py/extract.py by function, so
they land as one batch.

#2568 (thanks @imagineers-tyler): the 0.9.37 #2552 callback-body fix
unioned sibling closures' local names under the shared declaration, so a
local in one callback suppressed a real indirect_call in a sibling. Locals
are now scoped per body (keyed by body id, via walk_calls' extra_locals).
Restore-only, never fabricates; #2552 capture preserved.

#2565 (thanks @kskchaitanya1993): Kotlin property initializers — class,
top-level, companion, and `by lazy {}` — now seed call extraction, so
`val repo = createRepo()` produces a calls edge; literal initializers
produce none; FQ calls compose with the #2550 resolver.

#2561 (thanks @fakewaffle): Swift receiver typing now handles
`@Environment(Store.self)` (whitelisted; @Query/keypath/dotted skipped to
avoid a wrong edge) and in-corpus factory bindings via a marked concrete
return type; opaque/array/out-of-corpus returns stay unresolved.

#2577 (thanks @wilyan09007, PR #2579): the SQL extractor no longer emits a
reads_from edge to a CTE name. WITH names are scoped per query (a subquery
CTE no longer suppresses an outer real table of the same name), so a CTE
no longer mints a bare stub that binds to an unrelated same-named symbol.

#2575 (thanks @phudayyy, PR #2574): a dynamic `import('…')` inside a nested
function or at module scope now produces an edge, dynamic_import is
included in affected, and calls inside nested named functions are
collected; a dynamic import already captured as a deferred imports_from is
not double-counted.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.38. Your dynamic-import rescue + dynamic_import in DEFAULT_AFFECTED_RELATIONS is what landed for #2575, with a dedupe so an AST-captured deferred import is not double-counted and a companion engine fix so calls in nested named functions resolve too. Credited in the release notes. Thanks @phudayyy!

@safishamsi safishamsi closed this Aug 9, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…raphify-Labs#2568, Graphify-Labs#2565, Graphify-Labs#2561, Graphify-Labs#2577, Graphify-Labs#2575)

These changes are interleaved across engine.py/extract.py by function, so
they land as one batch.

Graphify-Labs#2568 (thanks @imagineers-tyler): the 0.9.37 Graphify-Labs#2552 callback-body fix
unioned sibling closures' local names under the shared declaration, so a
local in one callback suppressed a real indirect_call in a sibling. Locals
are now scoped per body (keyed by body id, via walk_calls' extra_locals).
Restore-only, never fabricates; Graphify-Labs#2552 capture preserved.

Graphify-Labs#2565 (thanks @kskchaitanya1993): Kotlin property initializers — class,
top-level, companion, and `by lazy {}` — now seed call extraction, so
`val repo = createRepo()` produces a calls edge; literal initializers
produce none; FQ calls compose with the Graphify-Labs#2550 resolver.

Graphify-Labs#2561 (thanks @fakewaffle): Swift receiver typing now handles
`@Environment(Store.self)` (whitelisted; @Query/keypath/dotted skipped to
avoid a wrong edge) and in-corpus factory bindings via a marked concrete
return type; opaque/array/out-of-corpus returns stay unresolved.

Graphify-Labs#2577 (thanks @wilyan09007, PR Graphify-Labs#2579): the SQL extractor no longer emits a
reads_from edge to a CTE name. WITH names are scoped per query (a subquery
CTE no longer suppresses an outer real table of the same name), so a CTE
no longer mints a bare stub that binds to an unrelated same-named symbol.

Graphify-Labs#2575 (thanks @phudayyy, PR Graphify-Labs#2574): a dynamic `import('…')` inside a nested
function or at module scope now produces an edge, dynamic_import is
included in affected, and calls inside nested named functions are
collected; a dynamic import already captured as a deferred imports_from is
not double-counted.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS/TS: await import('…') inside a nested function produces no edge — and affected cannot traverse dynamic_import at all

2 participants