Skip to content

JS: CommonJS require() inside a function body produces no edge at all — cycle-breaking lazy requires are invisible (0.9.37) #2700

Closed

Description

@BourbonDog

Version: graphify 0.9.37 (also observed on 0.9.32)
Platform: Windows 11, Python 3.14.6, uv tool install "graphifyy[mcp]"

Summary

A CommonJS require() written directly inside a function body (nesting depth 1) produces no edge at all — not a dangling one, not a low-confidence one. The importing file ends up with zero connection to the module it requires. The identical require() at module scope resolves fine, so a codebase can look well-connected while every deferred require is invisible.

This matters disproportionately because lazy require() is the standard CommonJS idiom for breaking circular dependencies. The edges most likely to be dropped are exactly the ones describing a project's most tangled relationships.

There is an irony worth flagging: graphify labels edges EXTRACTED (explicit in source) vs INFERRED (resolved heuristically). A literal require('./target') string is about as EXTRACTED as evidence gets — yet in the repro below the lazy-require file keeps an INFERRED 0.5-confidence edge while its four EXTRACTED 1.0-confidence edges vanish.

Reproduction

Three files, no config, no aliases, no bundler. git init + graphify extract . --code-only.

// target.js
function helper() { return 42; }
module.exports = { helper };
// top_level.js  — require at module scope (CONTROL)
const { helper } = require('./target');
function useIt() { return helper(); }
module.exports = { useIt };
// in_function.js — same require, one level deeper
function useItLazily() {
  const { helper } = require('./target');
  return helper();
}
module.exports = { useItLazily };

Result — 7 nodes, 7 edges

top_level.js → four EXTRACTED edges, confidence 1.0:

{"relation":"imports_from","confidence":"EXTRACTED","confidence_score":1,"source":"top_level","target":"target","source_location":"L1"}
{"relation":"imports","confidence":"EXTRACTED","confidence_score":1,"source":"top_level","target":"target_helper","source_location":"L1"}
{"relation":"calls","confidence":"EXTRACTED","confidence_score":1,"source":"top_level_useit","target":"target_helper","source_location":"L2"}
{"relation":"contains","confidence":"EXTRACTED","confidence_score":1,"source":"top_level","target":"top_level_helper","source_location":"L1"}

in_function.js → one INFERRED edge, confidence 0.5, pointing at its own function:

{"relation":"indirect_call","confidence":"INFERRED","confidence_score":0.5,"source":"in_function","target":"in_function_useitlazily","source_location":"L5"}
file require placement edge to target
top_level.js module scope ✅ imports_from + imports
in_function.js depth-1 function body ❌ none

Also note top_level_helper exists as a node (the destructured binding) while in_function has no equivalent — the binding is not extracted either.

Why this is distinct from existing issues

Real-world impact

Found while indexing BourbonDog/amicus (~841 JS files). Benchmarking graphify affected against plain grep for one module's dependents:

  • graphify found 3 of 6 real require()-backed dependents; grep found all 6 in under a second
  • the 3 missed (headless.js, mcp-server.js, sidecar/session-utils.js) all require the target from inside a function body
  • --depth 4 did not recover them, confirming this is missing edges rather than traversal depth

That codebase has 9 circular-dependency chains that are deliberately broken by lazy require(), each with a source comment saying so (// lazy: avoids a load-time cycle). Every one of those cycle-breaking edges is absent from the graph — so the graph presents an acyclic module structure that does not match the code.

The practical consequence is that affected under-reports blast radius without signalling reduced confidence, which is the dangerous direction for a "what breaks if I change this?" tool.

Suggested direction

Walk call_expression nodes with callee require at any scope depth rather than only at module scope, mirroring whatever the ESM path does for static imports. Two things worth covering in tests, since the amicus corpus exercises both:

  • require() inside try { } within a function (very common for optional/fallback loads)
  • multiple require() calls of the same module in different functions of one file (should dedupe to one edge, not zero)

Happy to test a patch against the amicus corpus — it has ~50 lazy requires across 9 documented cycles, so it is a decent real-world fixture for this.

Activity

  1. safishamsi commented on Aug 15, 2026

    @safishamsi
    Member

    Fixed in v0.9.44 (on PyPI now) via #2749, thanks @rajanpanth. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions