Repository navigation
fix(rust): resolve use krate::module::{self} and module-path imports to the module node (#5398) - #5400
fix(rust): resolve use krate::module::{self} and module-path imports to the module node (#5398)#5400Deepjyoti-Sarmah wants to merge 10 commits into
use krate::module::{self} and module-path imports to the module node (#5398)#5400Conversation
`use crate::util::*;` now resolves to util.rs's module node instead of the dangling bare `util` target.
|
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. |
There was a problem hiding this comment.
Graphify review 
1 bug · 4 possible issues to check · High impact
The new resolver maps Rust use paths through module declarations and source files.
super imports in inline modules can resolve to the wrong module · extract.py:5017
4 possible issues we couldn't confirm
- Four-file Rust import test pools targets from lines 1-3, so an import attached to the wrong line still passes ·
tests/test_rust_use_self_module.py:348 - Dangling-edge check reads only
edges, so a result that useslinksfails with a KeyError ·tests/test_rust_use_self_module.py:77 _node_idraises an opaque StopIteration when no node matches the source suffix ·tests/test_rust_use_self_module.py:54- +1 more in Graphify
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
| source = _source_path(origin) | ||
| if crate_lib is None or source == crate_lib: | ||
| return None | ||
| if source.parent == crate_lib.parent: |
There was a problem hiding this comment.
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::devuses 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::itemfrom a mod.rs file resolves against the module itself instead of its parent - Also reported as:
superimports frommod.rslook 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
What does this PR do?
Fixes #5398. In Rust,
use krate::dev::{self, SystemRunner};adds animports_fromedge to the bare namedevinstead of the module nodeactions_src_dev. Nothing resolves that target, so the edge dangles, and thegraph build later turns it into a source-less
conceptstub.On the issue's four files:
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 matchesdeclaration labels, and a module's file node is labelled
dev.rs. Items in thesame group (
SystemRunner,Console) resolve because they are declarations.Plain
use crate::util;anduse crate::util::*;have the same gap. A test intests/test_rust_use_trees.pyrequires a group to import exactly what its plainuselist does, so this fixes all three forms in one place and keeps thatrule.
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:
serde::de::{self}, cratemyapphas its ownsrc/de.rscrate::a::dev, onlysrc/other/dev.rsexistsdev.rsexists butlib.rsdoesn't declaremod dev;devbaba-actionsbaba_actions::matches (hyphen to underscore)Ten commits
Each fix commit closes one case the previous one missed.
a019c5a7parses eachlib.rsonce per pass instead of once per edge.Type of change
Verification & Invariants
The invariant: a
useof a local module points at that module's file node, anda path that can't be proven local stays exactly as v8 emits it.
Merge Danger
Door: two-way, with one persisted change.
_AST_CACHE_SCHEMAgoes from 51to 52, because cached Rust extractions lack the new path metadata. Every cached
Rust file re-extracts once after upgrade.
Blast radius: Rust only.
use a::Thing) are untouched. Only bindings that name a modulechange.
No new nodes are created.
Limitations
one. So
use serde::de::{self}inside a crate whoselib.rsdeclaresmod de;links to the localde.rs. With a Cargo.toml this case is handled.console(27 edges on the reporter'sproject), isn't reproduced. In the small fixture, bins that mix
dev::{self, ...}withports::{Console, ...}resolveconsolecorrectly onboth v8 and here. @grosjeanbaptiste, if you can share a file that shows it,
I'll follow up.
mod dev { ... }) have no file node, so their imports stay asthey are.
How was this tested?
tests/test_rust_use_self_module.pyis new and has 11 tests. They check theextract()output for dangling edges, which is the issue's own check. They don'tuse
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
devconcept stub.ports/Intentedges stillresolve), the no-Cargo declared module, the hyphenated package name,
{self as runtime},crate::/super::,dev/mod.rs, and a nestedcrate::a::devpath.serde::dewith a localde.rs, unrelatedcrates, 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_targetexpected thedangling
utiltarget, foruse crate::util::{self, ...}(L1) anduse crate::util::*(L6). Both now expectsrc_util, util.rs's module node.All with
PYTHONPATHset to the worktree:Not tested: Windows. The resolver compares resolved
Pathobjects, andnothing here builds path strings by hand.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)lib.rs; no environment is read.)