Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(rust): carry the rest of the path through a crate-root re-export (#2412)

Raghav Chamadiya03ef6d7b5ae1
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/rust.py b/packages/core/src/repowise/core/ingestion/resolvers/rust.py
index 4a46e4733..7c9c3d50e 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/rust.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/rust.py
@@ -160,6 +160,14 @@ def _follow_crate_root_reexport(
and resolve that ``pub use``'s own module path instead. Depth is
capped at one hop: a chain of re-exporting hubs resolves to the next
hub, whose own ``pub use`` edges keep the graph connected.
+
+ Segments after the matched one are carried through the hop. A
+ re-exported *module* can be named on the way to a submodule, as in
+ ``use crate_x::outer::inner::Type`` against a root that re-exports
+ ``outer``, and dropping the tail resolves the import to the parent
+ module's file instead. ``_probe_rust_path`` probes longest-first and
+ walks down, so a matched name that is a type rather than a module
+ still falls back to the module file for free.
"""
if not remaining_parts:
return None
@@ -174,6 +182,7 @@ def _follow_crate_root_reexport(
return None
name = remaining_parts[0]
+ rest = remaining_parts[1:]
for imp in getattr(parsed_files[root_path], "imports", []) or []:
if not getattr(imp, "is_reexport", False):
continue
@@ -186,12 +195,18 @@ def _follow_crate_root_reexport(
# carries the selected names.
if name not in names:
continue
- target_mp = "::".join([*segments[:-1], name])
+ target_mp = "::".join([*segments[:-1], name, *rest])
elif last == "*":
# Glob re-export: `pub use crate::module::*` — resolve the module.
+ # `name` names a member of it rather than a path segment, so
+ # neither it nor anything after it is appended.
target_mp = "::".join(segments[:-1])
elif last == name or name in names:
- target_mp = mp
+ # `name` either ends `mp` or is an alias for it: a renamed
+ # re-export carries the alias in `imported_names`, never a
+ # segment below `mp`. Either way it names `mp` itself, so only
+ # the tail is appended.
+ target_mp = "::".join([mp, *rest]) if rest else mp
else:
continue
resolved = resolve_rust_import(target_mp, root_path, ctx, _reexport_depth=1)
diff --git a/tests/unit/ingestion/test_rust_resolver.py b/tests/unit/ingestion/test_rust_resolver.py
index b7d3e1d5f..68d5be4a0 100644
--- a/tests/unit/ingestion/test_rust_resolver.py
+++ b/tests/unit/ingestion/test_rust_resolver.py
@@ -701,3 +701,97 @@ class TestPubUseReexportFollowing:
}
got = resolve_rust_import("crate::Ghost", "src/app.rs", ctx)
assert got is None
+
+ def test_submodule_of_reexported_module(self, tmp_path: Path) -> None:
+ # THE control for carrying the path tail. lib.rs re-exports the module
+ # `domain`; the import names a SUBMODULE of it. Matching only the first
+ # segment resolves to domain.rs -- the parent -- and every symbol the
+ # importer then calls binds against the wrong file.
+ paths = [
+ "src/lib.rs",
+ "src/outer.rs",
+ "src/outer/domain.rs",
+ "src/outer/domain/inner.rs",
+ "src/app.rs",
+ ]
+ ctx = _ctx(tmp_path, paths)
+ ctx.parsed_files = {
+ "src/lib.rs": _parsed_with_imports(
+ [("crate::outer::{domain}", True, ["domain"])]
+ ),
+ "src/outer.rs": _parsed_with_imports([]),
+ "src/outer/domain.rs": _parsed_with_imports([]),
+ "src/outer/domain/inner.rs": _parsed_with_imports([]),
+ "src/app.rs": _parsed_with_imports([]),
+ }
+ got = resolve_rust_import("crate::domain::inner::Thing", "src/app.rs", ctx)
+ assert got == "src/outer/domain/inner.rs"
+
+ def test_type_under_reexported_module_falls_back_to_module(
+ self, tmp_path: Path
+ ) -> None:
+ # Same shape, but the tail names a TYPE rather than a submodule. There
+ # is no inner file to find, so probing must walk back down to the
+ # module file. This is what makes carrying the tail safe.
+ paths = ["src/lib.rs", "src/outer.rs", "src/outer/domain.rs", "src/app.rs"]
+ ctx = _ctx(tmp_path, paths)
+ ctx.parsed_files = {
+ "src/lib.rs": _parsed_with_imports(
+ [("crate::outer::{domain}", True, ["domain"])]
+ ),
+ "src/outer.rs": _parsed_with_imports([]),
+ "src/outer/domain.rs": _parsed_with_imports([]),
+ "src/app.rs": _parsed_with_imports([]),
+ }
+ got = resolve_rust_import("crate::domain::Thing", "src/app.rs", ctx)
+ assert got == "src/outer/domain.rs"
+
+ def test_direct_reexport_carries_submodule_tail(self, tmp_path: Path) -> None:
+ # The single-name branch, not the brace group: `pub use crate::a::b`
+ # re-exports module `b`, and the import reaches through it.
+ paths = ["src/lib.rs", "src/a.rs", "src/a/b.rs", "src/a/b/c.rs", "src/app.rs"]
+ ctx = _ctx(tmp_path, paths)
+ ctx.parsed_files = {
+ "src/lib.rs": _parsed_with_imports([("crate::a::b", True, ["b"])]),
+ "src/a.rs": _parsed_with_imports([]),
+ "src/a/b.rs": _parsed_with_imports([]),
+ "src/a/b/c.rs": _parsed_with_imports([]),
+ "src/app.rs": _parsed_with_imports([]),
+ }
+ got = resolve_rust_import("crate::b::c::Thing", "src/app.rs", ctx)
+ assert got == "src/a/b/c.rs"
+
+ def test_glob_reexport_still_resolves_to_the_globbed_module(
+ self, tmp_path: Path
+ ) -> None:
+ # A glob re-export names no member, so the matched segment is a member
+ # of the globbed module rather than a path segment. Neither it nor the
+ # tail is appended, and a multi-segment import still lands on the
+ # module itself.
+ paths = ["src/lib.rs", "src/prelude.rs", "src/app.rs"]
+ ctx = _ctx(tmp_path, paths)
+ ctx.parsed_files = {
+ "src/lib.rs": _parsed_with_imports([("crate::prelude::*", True, ["*"])]),
+ "src/prelude.rs": _parsed_with_imports([]),
+ "src/app.rs": _parsed_with_imports([]),
+ }
+ got = resolve_rust_import("crate::Anything::deeper", "src/app.rs", ctx)
+ assert got == "src/prelude.rs"
+
+ def test_renamed_reexport_carries_tail_without_the_alias(
+ self, tmp_path: Path
+ ) -> None:
+ # `pub use crate::a::b as c` puts the ALIAS in imported_names, and an
+ # alias is another name for the module rather than a segment beneath
+ # it. Appending it would probe a path nobody wrote.
+ paths = ["src/lib.rs", "src/a.rs", "src/a/b.rs", "src/a/b/inner.rs", "src/app.rs"]
+ ctx = _ctx(tmp_path, paths)
+ ctx.parsed_files = {
+ "src/lib.rs": _parsed_with_imports([("crate::a::b as c", True, ["c"])]),
+ "src/a.rs": _parsed_with_imports([]),
+ "src/a/b.rs": _parsed_with_imports([]),
+ "src/a/b/inner.rs": _parsed_with_imports([]),
+ "src/app.rs": _parsed_with_imports([]),
+ }
+ got = resolve_rust_import("crate::c::inner::Thing", "src/app.rs", ctx)
+ assert got == "src/a/b/inner.rs"