Skip to content

fix(rust): resolve use krate::module::{self} and module-path imports to the module node (#5398) - #5400

Open
Deepjyoti-Sarmah wants to merge 10 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5398-rust-use-self-group
Open

Deepjyoti-Sarmah wants to merge 10 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5398-rust-use-self-group

Conversation

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5398. In Rust, use krate::dev::{self, SystemRunner}; adds an
imports_from edge to the bare name dev instead of the module node
actions_src_dev. Nothing resolves that target, so the edge dangles, and the
graph build later turns it into a source-less concept stub.

On the issue's four files:

v8 (3671f311)  DANGLING actions_src_bin_b -> dev imports_from
this branch    (none)

The group parser already expands {self} into a binding for the enclosing path
(baba_actions::dev). The import emitter then keeps only the last segment,
dev, and the stub rewire can't reach a module from there. It matches
declaration labels, and a module's file node is labelled dev.rs. Items in the
same group (SystemRunner, Console) resolve because they are declarations.

Plain use crate::util; and use crate::util::*; have the same gap. A test in
tests/test_rust_use_trees.py requires a group to import exactly what its plain
use list does, so this fixes all three forms in one place and keeps that
rule.

 extract_rust
+  every module-path binding records its path segments (_rust_use_module_path)
 _rewire_unique_stub_nodes
+  resolve the path to the module's file node:
+    crate::/self::/super::, own Cargo package, or a workspace crate
+        -> <crate src>/a/dev.rs or <crate src>/a/dev/mod.rs, full path
+    no Cargo.toml
+        -> only a module declared (`mod dev;`) in the lib.rs that contains the importer
+    any other first segment -> external, left alone
+  exactly one candidate, or the edge is unchanged

The rule

A wrong local link is worse than a dangling edge: it merges an external module
into local code and gives it a degree it doesn't have. So every uncertain case
leaves the edge as it was on v8:

case result
serde::de::{self}, crate myapp has its own src/de.rs external, not linked
crate::a::dev, only src/other/dev.rs exists not linked (full path, not last segment)
no Cargo.toml, dev.rs exists but lib.rs doesn't declare mod dev; not linked
two unrelated crates both declare dev not guessed
package baba-actions baba_actions:: matches (hyphen to underscore)

Ten commits

c38629f1  fix(5398): resolve grouped Rust self module imports
037c143f  fix(5398): require declared modules without Cargo metadata
4ce7163e  fix(5398): recognize Rust mod.rs module files
b250dbe3  fix(5398): resolve full Rust module paths
a019c5a7  fix(5398): cache Rust module declarations per file
b8eb0f17  fix(5398): document Rust module cache schema bump
ede0d616  fix(5398): resolve unmanifested relative self imports
77c0ddfb  fix(5398): resolve Rust module imports for plain and grouped use
119eeebd  test(5398): pin Rust module-path import resolution
42e9231e  test(5398): expect the module node for a Rust wildcard use

Each fix commit closes one case the previous one missed. a019c5a7 parses each
lib.rs once per pass instead of once per edge.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

The invariant: a use of a local module points at that module's file node, and
a path that can't be proven local stays exactly as v8 emits it.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Merge Danger

Door: two-way, with one persisted change. _AST_CACHE_SCHEMA goes from 51
to 52, because cached Rust extractions lack the new path metadata. Every cached
Rust file re-extracts once after upgrade.

Blast radius: Rust only.

  • Item imports (use a::Thing) are untouched. Only bindings that name a module
    change.
  • A resolved edge moves from a dangling bare id to a node that already exists.
    No new nodes are created.
  • Anything ambiguous or external keeps its v8 target, per the table above.

Limitations

  • Without a Cargo.toml, a file can't tell its own crate name from an external
    one. So use serde::de::{self} inside a crate whose lib.rs declares
    mod de; links to the local de.rs. With a Cargo.toml this case is handled.
  • The issue's second local target, console (27 edges on the reporter's
    project), isn't reproduced. In the small fixture, bins that mix
    dev::{self, ...} with ports::{Console, ...} resolve console correctly on
    both v8 and here. @grosjeanbaptiste, if you can share a file that shows it,
    I'll follow up.
  • Inline modules (mod dev { ... }) have no file node, so their imports stay as
    they are.

How was this tested?

tests/test_rust_use_self_module.py is new and has 11 tests. They check the
extract() output for dangling edges, which is the issue's own check. They don't
use graph.json, because the build turns every dangling target into a stub node,
so a check there always passes. The positive cases also assert that the built
graph has no dev concept stub.

  • 7 regressions: the issue's four files (the ports/Intent edges still
    resolve), the no-Cargo declared module, the hyphenated package name,
    {self as runtime}, crate::/super::, dev/mod.rs, and a nested
    crate::a::dev path.
  • 4 guards: the undeclared module, serde::de with a local de.rs, unrelated
    crates, and a nested path that must not match src/other/dev.rs.

The 7 fail on v8 and the 4 pass there. All 11 pass here.

Two lines of test_each_binding_gets_the_plain_use_target expected the
dangling util target, for use crate::util::{self, ...} (L1) and
use crate::util::* (L6). Both now expect src_util, util.rs's module node.

All with PYTHONPATH set to the worktree:

.venv/bin/python -m pytest -q tests/test_rust_use_self_module.py
    v8 (3671f311)  -> 7 failed, 4 passed
    this branch    -> 11 passed
.venv/bin/python -m pytest -q tests -k rust
    v8 (3671f311)  -> 218 passed
    this branch    -> 229 passed
.venv/bin/python -m pytest -q tests
    v8 (3671f311)  -> 11833 passed, 122 skipped, 1 xfailed
    this branch    -> 11844 passed, 122 skipped, 1 xfailed
.venv/bin/python -m ruff check graphify/ tests/test_rust_use_self_module.py tests/test_rust_use_trees.py
    -> All checks passed

Not tested: Windows. The resolver compares resolved Path objects, and
nothing here builds path strings by hand.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables). (The no-Cargo case reads the scanned tree's own lib.rs; no environment is read.)
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages. (No trailer in the commit messages.)

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 22, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Deepjyoti-Sarmah. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@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 review View in Graphify

Review before merging · Merge confidence 4/5

1 bug · 4 possible issues to check · High impact

The new resolver maps Rust use paths through module declarations and source files.

P2  super imports in inline modules can resolve to the wrong module · extract.py:5017

4 possible issues we couldn't confirm

They don't change the score.

What this review covered
  • Some lookups asked for files or lines that do not exist; the review was told and carried on.
  • 5 of 5 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 5587 functions depend on this change
  • 10 functions became more coupled. Largest: extract() (1337 callers), _rewire_unique_stub_nodes() (14 callers), walk() (1 callers).
  • 392 of 614 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 42e9231 · Full report in the Graphify check

Comment thread graphify/extract.py
Comment on lines +5014 to +5017
source = _source_path(origin)
if crate_lib is None or source == crate_lib:
return None
if source.parent == crate_lib.parent:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  super imports in inline modules can resolve to the wrong module

An import inside an inline module can point to a crate-root module instead of its parent module.

Resolve super::... from the containing inline module’s parent in _super_parent_file.

Evidence · checked against the code

Expected: The import resolves relative to the inline module's parent.
Actually: The resolver treats lib.rs as the parent and can select the crate-root module instead.

  • Independent source review: The existing test for super::dev uses an external module file, src/nested.rs, and does not cover an inline module in src/a.rs; it does not prevent the reported wrong target. The failure requires a root-level matching module as well as the intended parent module's module declaration.
  • Also reported as: use super::item from a mod.rs file resolves against the module itself instead of its parent
  • Also reported as: super imports from mod.rs look in the wrong parent module
Prompt to fix with AI
In graphify/extract.py, _super_parent_file (lines 5014-5017):
Problem: `super` imports in inline modules can resolve to the wrong module
Expected: The import resolves relative to the inline module's parent.
Actually: The resolver treats lib.rs as the parent and can select the crate-root module instead.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

This branch has not been deployed

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Rust: use krate::module::{self, Item} leaves a dangling bare-name imports_from edge (0.9.115)

1 participant