Skip to content

fix(js): retain lazy CommonJS require dependencies - #2749

Closed
rajanpanth wants to merge 1 commit into
Graphify-Labs:v8from
rajanpanth:fix/js-lazy-require-edges
Closed

rajanpanth wants to merge 1 commit into
Graphify-Labs:v8from
rajanpanth:fix/js-lazy-require-edges

Conversation

@rajanpanth

Copy link
Copy Markdown
Contributor

Summary

  • detect CommonJS
    equire()\ declarations while traversing JavaScript and TypeScript function bodies
  • emit the resolved \imports_from\ and symbol import edges from the enclosing callable
  • preserve existing file-level edges for module-scope requires

Fixes #2700.

Validation

  • uv run pytest tests/test_extract.py -q
  • uv run pytest tests/test_extract.py::test_extract_js_function_scoped_require_emits_import_edge tests/test_extract.py::test_extract_js_destructured_require_imports_from tests/test_extract.py::test_extract_js_destructured_require_named_symbols tests/test_languages.py -q
  • uv run ruff check graphify/extractors/engine.py tests/test_extract.py
  • uv run graphify update .

@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).

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

This PR modifies the JavaScript/TypeScript extraction logic in graphify/extractors/engine.py to detect CommonJS require() imports that appear inside function bodies (not just at the module top level) and attribute those import edges to the enclosing callable rather than dropping them. It renames the file_nid parameter to importer_nid in _require_imports_js to reflect that the import source may now be a function rather than a file, and adds a call to this helper during the function-body walk in _extract_generic. A new test (test_extract_js_function_scoped_require_emits_import_edge) is added to exercise the lazy/function-scoped require path, asserting the resulting import edge is sourced from the enclosing function. The many other changed symbols in the diff summary appear to be incidental (e.g., line-number-shifted rationale/test identifiers) rather than direct edits.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 897 functions depend on the 534 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: walk_calls() — 1 callers, 16 callees
  • worse: _require_imports_js() — 3 callers, 5 callees

Verification — 897 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: 838 function(s) in the blast radius were not formally verified this run

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

Could not verify: Could not verify \_require\_imports\_js.

The verifier did not have enough to check \_require\_imports\_js, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: not verifiable: all 200 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

· 1 grounded finding(s) anchored inline below; 1 more finding(s) on lines outside this diff (see the check run).

return None

def _require_imports_js(node, source: bytes, file_nid: str, stem: str, edges: list, str_path: str) -> bool:
def _require_imports_js(node, source: bytes, importer_nid: str, stem: str, edges: list, str_path: str) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Health regression — _require_imports_js()

high coupling complexity (Ca·Ce = 15).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.44, just published to PyPI. I cherry-picked this onto v8 with your authorship preserved in the commit, so both the change and the credit are in the history. Thanks @rajanpanth for the fix. Closing since it is now released.

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: CommonJS require() inside a function body produces no edge at all — cycle-breaking lazy requires are invisible (0.9.37)

2 participants