Skip to content

fix(lua): emit an imports edge for bare require() statements (#3320) - #3519

Closed
A-Levin wants to merge 3 commits into
Graphify-Labs:v8from
A-Levin:pr/lua-bare-require
Closed

A-Levin wants to merge 3 commits into
Graphify-Labs:v8from
A-Levin:pr/lua-bare-require

Conversation

@A-Levin

@A-Levin A-Levin commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #3320.

Problem

A Lua require("mod") written as a bare statement produces no imports edge. The assigned form does:

# bare
(root/"init.lua").write_text('require("config.lazy")\n')
extract([...])["edges"]  # []

# assigned
(root/"init.lua").write_text('local ok = require("config.lazy")\n')
extract([...])["edges"]  # [{'source': 'init', 'target': 'config_lazy', 'relation': 'imports', ...}]

The bare form is the dominant idiom in Neovim configuration, where init.lua is typically nothing but a run of require("config.lazy") lines with no assignment. On a real LazyVim-style tree (init.lua + lua/config/*.lua + lua/plugins/*.lua) v8 today produces zero import edges; every file lands in the graph disconnected.

The cause is dispatch, not parsing. _LUA_CONFIG.import_types is frozenset({"variable_declaration"}) (graphify/extract.py:1207), and walk() only invokes import_handler for a node whose type is in that set. A bare require(...) parses as a function_call directly under chunk/block, so _import_lua is never called for it at all.

Fix

graphify/extractors/engine.py: a new _lua_is_require_call() helper plus a narrow branch at the top of walk() that fires only for tree_sitter_lua, only on a function_call, and only when the call's name field is an identifier reading exactly require. It invokes the existing _import_lua handler unchanged.

The obvious one-line alternative — adding function_call to _LUA_CONFIG.import_types — is deliberately not what this does. walk() returns after an import_types match, and function_call is the node type for every Lua call, so that version would stop recursion into ordinary calls and silently drop whatever is declared inside their callbacks. Verified: under that behaviour vim.keymap.set("n","x", function() local function inner() end end) loses the init_inner node entirely, and vim.defer_fn(function() local function deferred() end end, 10) loses init_deferred. The shipped branch leaves both intact.

No double counting: local x = require("mod") matches variable_declaration first and returns before the walker descends to the nested call, so it never reaches the new branch. Confirmed with a spy on _import_lua — one handler invocation and one edge for both forms.

Method-style calls are rejected: foo.require("x") has a dot_index_expression name and bar:require("x") a method_index_expression, so neither produces an edge.

Test

tests/test_lua_import.py, 8 tests. Five pin the defect (bare require in the flattened and lua/config/ layouts, several bare requires in one file, and the no-parens require 'm' / require "m" forms); one is the assigned-form control; two guard the rationale above — that a non-require call still recurses into its callback, and that a method-style require fabricates nothing.

Each guard was checked by mutation rather than assumed:

Mutation Tests killed
Delete the walk() branch 5 (every bare-require case)
_lua_is_require_call → return "requires" comparison 5
_lua_is_require_call → return True 2 (callback recursion + method-style)
Drop the ts_module == "tree_sitter_lua" guard 0 — equivalent mutant: tree_sitter_lua is the only configured grammar with a node kind named function_call. Kept as defensive scoping.
Drop the name_node.type != "identifier" check 0 — equivalent mutant: the string compare already rejects foo.require. Kept for clarity.

End to end on a LazyVim-shaped tree: 0 import edges before, 3 after (init → lua_config_lazy, init → lua_config_options, lua_config_lazy → lua_plugins_tele).

Local run of the CI jobs on this branch:

  • skillgen --check / --audit-coverage / --schema-singleton / --monolith-roundtrip / --always-on-roundtrip: all OK
  • pytest tests/ -q (after uv sync --all-extras --frozen): 5600 passed, 14 skipped, 2 failed
  • ruff check graphify tests: clean
  • bandit -r graphify -ll: unchanged from baseline

The 2 failures are both test_hooks.py::test_rebuild_bodies_survive_a_graphify_root_symlink_loop, and they fail identically on pristine v8 (522ea96) with this branch not applied — 2 failed, 111 passed in tests/test_hooks.py there. Environment-specific to my machine, unrelated to Lua.

Open question

Only literal require("string") / require 'string' is handled. A dynamic require(var) still produces no edge — unchanged from the assigned form's existing behaviour, since _import_lua has only ever matched string literals. Not addressed here; happy to file it separately if you want it covered.

A bare `require("mod")` statement with no assignment produces no imports
edge, while `local x = require("mod")` does. Cover the bare form in the
flattened and lua/config layouts, with and without parentheses, plus
guards for the callback recursion a fix must preserve and for method-style
calls that must not fabricate an edge.
…abs#3320)

A bare require("mod") with no assignment - the dominant idiom in
Neovim configs - never reached the import handler: only
variable_declaration was in the Lua import dispatch set, so
require(...) used as a standalone statement produced no edge at
all. Detect the require call ahead of the generic import_types
check instead of widening it to function_call, which would have
swallowed every plain call and skipped the walker's recursion into
its arguments (e.g. a require() nested inside a callback passed to
vim.keymap.set).

@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

Emits an imports edge for a bare Lua require("mod") statement (parenthesized or quote-only, single or double), matching the assigned local x = require(...) form so Neovim/LazyVim configs graph as connected files instead of orphans. The new _lua_is_require_call gate fires only for calls to the global require identifier and runs ahead of the normal import dispatch, so method-style calls like foo.require(...) are ignored and non-require calls still get walked into (a local function inside a vim.keymap.set callback is still discovered). Adds tests/test_lua_import.py covering the bare/assigned/multi-require, directory-layout resolution, no-parens, callback-recursion, and method-style cases.

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

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 880 functions depend on the 439 functions this change touches.

Health — this change adds coupling hotspots:

  • new: _extract_generic() — 18 callers, 26 callees
  • new: extract_js() — 85 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: extract_objc() — 27 callers, 9 callees
  • new: extract_julia() — 17 callers, 7 callees
  • new: extract_cpp() — 27 callers, 3 callees
  • new: extract_vue() — 10 callers, 7 callees
  • new: walk() — 1 callers, 60 callees
  • …and 8 more — each is listed as a finding

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

Test selection

Test selection

274 of 274 test file(s) selected (100%) via static blast radius.

Escalated to a full run for safety — the selection is not trustworthy on its own (see below). CI should run the whole suite.

  • tests/test_affected_cli.py — full-run-safety
  • tests/test_affected_member_seed.py — full-run-safety
  • tests/test_agents_platform.py — full-run-safety
  • tests/test_analyze.py — full-run-safety
  • tests/test_anthropic_custom_endpoint.py — full-run-safety
  • tests/test_antigravity_install.py — full-run-safety
  • tests/test_apm_fallback_version.py — full-run-safety
  • tests/test_architecture_doc.py — full-run-safety
  • tests/test_astro_extraction.py — impact, full-run-safety
  • tests/test_astro_import_ids.py — full-run-safety
  • tests/test_atomic_canvas_export.py — full-run-safety
  • tests/test_atomic_version_stamp.py — full-run-safety
  • tests/test_atomic_writes.py — full-run-safety
  • tests/test_backend_env_isolation.py — full-run-safety
  • tests/test_backend_extras.py — full-run-safety
  • tests/test_benchmark.py — full-run-safety
  • tests/test_benchmark_raw_graph.py — full-run-safety
  • tests/test_build.py — impact, full-run-safety
  • tests/test_build_merge_dedup_scope.py — full-run-safety
  • tests/test_build_merge_hyperedges_and_prune.py — full-run-safety
  • tests/test_build_merge_shrink_guard.py — full-run-safety
  • tests/test_builtin_global_type_refs.py — full-run-safety
  • tests/test_cache.py — full-run-safety
  • tests/test_callflow_html.py — full-run-safety
  • tests/test_cargo_introspect.py — full-run-safety
  • tests/test_carried_hyperedge_remap.py — full-run-safety
  • tests/test_case_sensitive_resolution.py — full-run-safety
  • tests/test_charmap_encoding.py — full-run-safety
  • tests/test_chunking.py — full-run-safety
  • tests/test_cjs_module_extension.py — impact, full-run-safety
  • tests/test_claude_cli_backend.py — full-run-safety
  • tests/test_claude_md.py — full-run-safety
  • tests/test_cli_broken_pipe.py — full-run-safety
  • tests/test_cli_export.py — full-run-safety
  • tests/test_cli_help.py — full-run-safety
  • tests/test_cluster.py — full-run-safety
  • tests/test_codebuddy.py — full-run-safety
  • tests/test_community_hub_labels.py — full-run-safety
  • tests/test_community_labels_skill.py — full-run-safety
  • tests/test_confidence.py — full-run-safety
  • tests/test_corrupt_graph_json.py — full-run-safety
  • tests/test_cpp_nested_and_cli.py — impact, full-run-safety
  • tests/test_cpp_objc_cross_file_calls.py — full-run-safety
  • tests/test_cpp_preprocess.py — full-run-safety
  • tests/test_cross_extension_reexport_self_cycle.py — full-run-safety
  • tests/test_cross_language_call_resolution.py — full-run-safety
  • tests/test_cross_repo_member_calls.py — full-run-safety
  • tests/test_cross_repo_shared_types.py — full-run-safety
  • tests/test_csharp_call_site_generic_args.py — full-run-safety
  • tests/test_csharp_enum_members.py — full-run-safety
  • … and 224 more

non-code file(s) changed (CHANGELOG.md) → running the full suite for safety (a code graph can't see config/fixture/data deps)

changed code file(s) with no mapped test (CHANGELOG.md) — a coverage gap or a missing link — running the full suite rather than only the selected tests

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

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

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

return _read_text(name_node, source) == "require"


def _extract_generic(

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 — _extract_generic()

fans out to 26 callees (efferent coupling); 18 callers depend on it (afferent coupling).

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

@@ -3347,6 +3356,20 @@ def ensure_named_node(name: str, line: int) -> str:
def walk(node, parent_class_nid: str | None = None) -> None:

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 — walk()

fans out to 60 callees (efferent coupling).

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

safishamsi added a commit that referenced this pull request Sep 15, 2026
…nto released sections

These belong in the next release and will be re-added at the bump with correct
credit (@HARSHAVARDHAN-RAJU5 for #3529) and wording.
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.62 (now on PyPI: uv tool install graphifyy==0.9.62). Your commits were cherry-picked with authorship preserved, so this shows up under your GitHub contributions. Thanks @A-Levin!

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.

Lua extractor misses bare require() statements (no assignment)

2 participants