graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Lua require() dependencies do not appear as file-level edges in the final graph

Open call-me-sean opened this issue 6 months ago • 3 comments

Summary

It looks like Lua require() dependencies are not consistently preserved as file-level edges in the final graph.

Because of this, Lua files that are referenced via require() may not show the expected inbound dependency relationships.

Expected behavior

When Lua source contains imports such as:

local mod = require("foo.bar")
require("foo.baz")

the final graph should include a file-level dependency edge between the importing file and the imported Lua file.

Actual behavior

The final graph appears to keep other relationships such as structural containment and call edges, but Lua file-to-file dependency edges from require() are missing.

Possible cause

From a quick look at the current Lua extraction path, this may be related to how require() targets are extracted and resolved before graph construction.

In particular, it seems possible that:

  • only some require() forms are detected,
  • and/or the extracted target does not always match the corresponding file node in the graph.

If so, the dependency edge may be dropped during graph build.

Minimal example

a.lua

local b = require("pkg.b")
return b

pkg/b.lua

local M = {}
return M

Expected result:

  • a file-level dependency edge from a.lua to pkg/b.lua

Actual result:

  • the file-level dependency edge is missing

Suggested direction

It may help to:

  • support more Lua require() forms,
  • resolve Lua module paths to actual project file nodes,
  • and preserve those relationships as file-level edges in the final graph.

Thanks for taking a look.

call-me-sean avatar May 29 '26 10:05 call-me-sean

Opened a fix PR for this:

  • PR #1076: https://github.com/safishamsi/graphify/pull/1076

It includes the Lua require() file-level edge fix, follow-up path consistency fixes, and targeted validation notes.

call-me-sean avatar May 29 '26 11:05 call-me-sean

Fixed in 5642c1b — _import_lua was taking only the last dot-segment of the module path and using the raw string as the edge target, which never matched the file node ID. Replaced with _resolve_lua_import_target that converts dots to path separators, probes the filesystem (up 6 parent dirs, tries .lua/.luau/init.lua variants), and returns the correct _make_id(str(resolved_path)). Graceful fallback on unresolved imports.

safishamsi avatar May 29 '26 12:05 safishamsi

Thanks for the quick fix, and for linking it here.

I re-tested this on our Lua corpus and on a smaller repro. The core issue in #1075 does look fixed: require("pkg.mod") is now resolved to a real file node instead of using only the last dot segment.

That said, I still see a couple of gaps compared with the behavior we were validating in PR #1076:

  1. Bare require("pkg.bare") calls still appear to be missed in some cases, while assigned / nested / chained forms are picked up.
  2. In my local tests, the upstream fix emits imports edges, but not the imports_from file-level edges we were expecting for cross-file dependency analysis.

For example, on our Nearby corpus I currently see:

  • upstream fix: imports = 481, imports_from = 0
  • PR #1076 branch: imports = 481, imports_from = 775

So from my side, the core target-ID bug is fixed, but there are still some remaining coverage / edge-semantics differences.

PR #1076 is still open if helpful. It includes:

  • broader Lua require() coverage
  • file-level imports_from resolution
  • path consistency follow-up fixes
  • targeted validation notes

Happy to help narrow this further or split the remaining gaps into a smaller follow-up if that makes review easier.

call-me-sean avatar Jun 03 '26 07:06 call-me-sean