Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions graphify/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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", "")))
Expand Down Expand Up @@ -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]
Expand Down
67 changes: 67 additions & 0 deletions tests/test_rust_crate_use_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiError> { 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<ApiError> { 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") == []
Loading