From 996de9206ceb4899421d62c1445d5b873296c290 Mon Sep 17 00:00:00 2001 From: Max Loffgren Date: Sun, 15 Nov 2026 21:43:15 -0600 Subject: [PATCH] fix(rust): keep a `use crate::` import inside its own crate (#5240) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In a Cargo workspace, every crate's `use crate::…::X` targets the same bare stub id. The rewire scopes each stub to its own file's crate (#4699), but it kept one answer per id and applied it to every edge. One file's stub decided where every crate's import landed. On ripgrep, the `Error` in globset's `use crate::{Candidate, Error, ErrorKind, new_regex}` reached the regex crate's `Error`. Resolve per file instead. Each importing file gets its own `use` stub, and each Rust edge uses its own file's answer. A file whose crate has no unique match leaves its import unbound rather than borrowing another crate's answer. Fixes #5240 Co-Authored-By: Claude Opus 5.5 --- graphify/extract.py | 38 +++++++++++++---- tests/test_rust_crate_use_scope.py | 67 ++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/graphify/extract.py b/graphify/extract.py index 17f97dd349..564fd7021a 100644 --- a/graphify/extract.py +++ b/graphify/extract.py @@ -4880,6 +4880,10 @@ def keep(crates: set[str]) -> list[dict]: supertype_stub_ids.add(str(nid)) remap: dict[str, str] = {} + # #5240: every crate's `use crate::…::X` points at the same bare stub id, but each + # stub resolves within its own file's crate. A Rust edge takes its own file's + # answer, so one crate's X is never handed to another crate. + file_remap: dict[tuple[str, str], str | None] = {} for stub in stubs: stub_id = str(stub.get("id", "")) if not stub_id: @@ -4915,11 +4919,18 @@ def keep(crates: set[str]) -> list[dict]: cand_fam = _lang_family(fcands[0].get("source_file")) if not fams or cand_fam is None or cand_fam in fams: candidates = fcands - if len(candidates) != 1: - continue - target_id = candidates[0].get("id") - if isinstance(target_id, str) and target_id and target_id != stub_id: - remap[stub_id] = target_id + resolved: str | None = None + if len(candidates) == 1: + target_id = candidates[0].get("id") + if isinstance(target_id, str) and target_id and target_id != stub_id: + resolved = target_id + origin = str(stub.get("origin_file", "")) + if origin.endswith(".rs"): + # Two stubs of one file that disagree leave that file's edges unbound. + key = (stub_id, origin) + file_remap[key] = resolved if file_remap.get(key, resolved) == resolved else None + if resolved is not None: + remap[stub_id] = resolved # SQL: a table no file defines is one table however many files read it, so the # per-file stubs of one name fold into a single node. @@ -5017,8 +5028,12 @@ def _scoped_by_a_use(stub: dict) -> bool: ) and not _cross_language_candidate(edge, remapped_source): edge["source"] = remapped_source target = edge.get("target") - if target in remap: - remapped_target = remap[str(target)] + own_key = (str(target), str(edge.get("source_file", ""))) + if own_key in file_remap: + remapped_target = file_remap[own_key] # None: this file's stub found no match + else: + remapped_target = remap.get(str(target)) + if remapped_target is not None: if not ( is_csharp_scoped_edge and _is_cs_file(str(by_id.get(remapped_target, {}).get("source_file", ""))) @@ -12720,14 +12735,19 @@ def _learn(e: dict) -> None: # #4439: a Rust `use` reached its declaration only when a type annotation had left # a sourceless node under the same id. Lend the rewire one for each imported name # that has none; they are not kept, so an unresolved import dangles as before. + # One per importing file, not per name (#5240). Another file's stub resolves + # within that file's crate, so it cannot answer for this one. + _real_ids = {n.get("id") for n in all_nodes if n.get("source_file")} + _stubbed = {(n.get("id"), n.get("origin_file")) for n in all_nodes if not n.get("source_file")} _use_stubs = { - e["target"]: { + (e["target"], e.get("source_file")): { "id": e["target"], "label": e["_rust_use_name"], "file_type": "code", "source_file": "", "origin_file": e.get("source_file"), "_rust_use_roots": ["crate"], } for e in all_edges - if e.get("_rust_use_name", "").isidentifier() and e["target"] not in _fresh_ids + if e.get("_rust_use_name", "").isidentifier() and e["target"] not in _real_ids + and (e["target"], e.get("source_file")) not in _stubbed } _rewire_view = all_nodes + list(_use_stubs.values()) + [ n for n in resolution_context_nodes or [] if n.get("id") not in _fresh_ids] diff --git a/tests/test_rust_crate_use_scope.py b/tests/test_rust_crate_use_scope.py index 22d784fba4..1223792cd3 100644 --- a/tests/test_rust_crate_use_scope.py +++ b/tests/test_rust_crate_use_scope.py @@ -227,3 +227,70 @@ def test_a_use_of_a_scanned_crate_keeps_the_binding_extracted(tmp_path): edges = _edges_between(result, tmp_path, "plugin", "corelib") assert edges assert {edge["confidence"] for edge in edges} == {"EXTRACTED"} + + +# #5240: both crates import their own `ApiError` with `use crate::…`, so every import +# targets the same bare stub id. One crate's answer must not stand in for the other's. +CLIENT_API = "pub enum ApiError { Network }\n" +SERVER_ERROR = "pub enum ApiError { NotFound }\n" +CLIENT_IMPORT = "use crate::api::ApiError;\n" +SERVER_IMPORT = "use crate::error::ApiError;\n\npub fn check() -> Option { None }\n" + + +def _imports_between(result: dict, root: Path, src_crate: str, dst_crate: str) -> list[dict]: + return [e for e in _edges_between(result, root, src_crate, dst_crate) + if e["relation"] == "imports_from"] + + +def test_a_crate_use_ignores_another_crates_test_module_import(tmp_path): + # The issue's fixture, where the server's nested `mod tests` import decided + # where the client's `use crate::api::ApiError` landed. + result = _run(tmp_path, { + "client/src/api.rs": CLIENT_API, + "client/src/calls.rs": CLIENT_IMPORT, + "server/src/error.rs": SERVER_ERROR, + "server/src/dpop.rs": ( + "pub fn check() {}\n\n#[cfg(test)]\nmod tests {\n" + " use crate::error::ApiError;\n\n fn t() -> Option { None }\n}\n" + ), + }) + assert _imports_between(result, tmp_path, "client", "client") != [] + assert _edges_between(result, tmp_path, "client", "server") == [] + + +def test_two_crates_importing_the_same_name_each_reach_their_own(tmp_path): + result = _run(tmp_path, { + "client/src/api.rs": CLIENT_API, + "client/src/calls.rs": CLIENT_IMPORT, + "server/src/error.rs": SERVER_ERROR, + "server/src/dpop.rs": SERVER_IMPORT, + }) + assert _imports_between(result, tmp_path, "client", "client") != [] + assert _imports_between(result, tmp_path, "server", "server") != [] + assert _edges_between(result, tmp_path, "client", "server") == [] + assert _edges_between(result, tmp_path, "server", "client") == [] + + +def test_two_crates_with_only_use_imports_each_reach_their_own(tmp_path): + # No type annotation in either crate, so each `use` gets its own lent stub. + result = _run(tmp_path, { + "client/src/api.rs": CLIENT_API, + "client/src/calls.rs": CLIENT_IMPORT, + "server/src/error.rs": SERVER_ERROR, + "server/src/dpop.rs": "use crate::error::ApiError;\n", + }) + assert _imports_between(result, tmp_path, "client", "client") != [] + assert _imports_between(result, tmp_path, "server", "server") != [] + assert _edges_between(result, tmp_path, "client", "server") == [] + + +def test_a_crate_use_with_no_match_in_its_crate_stays_unbound(tmp_path): + # The client imports `crate::api::ApiError` but defines no `ApiError`. The + # server's is not the client's, so the import stays unresolved. + result = _run(tmp_path, { + "client/src/calls.rs": CLIENT_IMPORT, + "server/src/error.rs": SERVER_ERROR, + "server/src/dpop.rs": SERVER_IMPORT, + }) + assert _imports_between(result, tmp_path, "server", "server") != [] + assert _edges_between(result, tmp_path, "client", "server") == []