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(ingestion): mark Cargo target-table paths as reachable (#2940)

Aman4e2636e4e573
diff --git a/packages/core/src/repowise/core/ingestion/graph_warmups.py b/packages/core/src/repowise/core/ingestion/graph_warmups.py
index faa583e9f..cb5f7174c 100644
--- a/packages/core/src/repowise/core/ingestion/graph_warmups.py
+++ b/packages/core/src/repowise/core/ingestion/graph_warmups.py
@@ -306,6 +306,34 @@ def _warmup_dotnet(ctx: ResolverContext) -> None:
get_or_build_index(ctx)
+def _warmup_rust(ctx: ResolverContext) -> None:
+ """Stamp Cargo ``[[bin]]``/``[[test]]``/``[[bench]]``/``[[example]]``
+ targets that name an explicit ``path`` as live, so a file Cargo discovers
+ only through the manifest (not convention) is not reported as dead (#2936).
+
+ ``[[bin]]`` targets are entry points: cargo runs them directly. The other
+ three are runner-loaded the same way a test file is, so they become
+ reachability roots instead — the same "roots, not entry points" split the
+ TypeScript warmup above makes for its own non-manifest entry paths.
+ """
+ from .resolvers.rust_workspace import get_or_build_cargo_workspace_index
+
+ index = get_or_build_cargo_workspace_index(ctx)
+ if index is None:
+ return
+ graph = getattr(ctx, "graph", None)
+ if graph is None:
+ return
+ parsed = getattr(ctx, "parsed_files", None) or {}
+ for crate in index.crates:
+ for path in crate.bin_paths:
+ _stamp_entry(graph, parsed, path)
+ for path in crate.reachability_root_paths:
+ node = graph.nodes.get(path)
+ if node is not None:
+ node["is_reachability_root"] = True
+
+
def _warmup_go(ctx: ResolverContext) -> None:
"""Build the Go package index and stamp ``is_entry_point`` on every
``package main`` file declaring ``func main()``. Go's entry convention
@@ -517,6 +545,7 @@ _WARMUPS: dict[str, tuple[str, Warmup]] = {
"kotlin": ("graph.jvm_index", _warmup_jvm),
"csharp": ("graph.dotnet_index", _warmup_dotnet),
"go": ("graph.go_index", _warmup_go),
+ "rust": ("graph.rust_targets", _warmup_rust),
"typescript": ("graph.ts_index", _warmup_typescript),
"javascript": ("graph.ts_index", _warmup_typescript),
"cpp": ("graph.cpp_index", _warmup_cpp),
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/rust_workspace.py b/packages/core/src/repowise/core/ingestion/resolvers/rust_workspace.py
index 98200eeb1..0cd71fc71 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/rust_workspace.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/rust_workspace.py
@@ -44,6 +44,9 @@ class CargoCrate:
dependencies: tuple[CargoDep, ...] = ()
is_proc_macro: bool = False # True if [lib] proc-macro = true
bin_paths: tuple[str, ...] = () # repo-relative POSIX paths to [[bin]] entry points
+ # repo-relative POSIX paths to [[test]]/[[bench]]/[[example]] entries that
+ # name an explicit ``path`` — runner-loaded, not entry points (#2936).
+ reachability_root_paths: tuple[str, ...] = ()
@dataclass(frozen=True)
@@ -139,13 +142,27 @@ def _parse_deps(
return tuple(deps)
-def _parse_bin_targets(cargo_data: dict, member_rel: str, repo: Path) -> tuple[str, ...]:
- """Extract ``[[bin]]`` entry-point paths from Cargo.toml data."""
- bins = cargo_data.get("bin", [])
- if not isinstance(bins, list):
+# Tables, other than [[bin]], whose explicit-``path`` entries are
+# runner-loaded rather than entry points (#2936).
+_ROOT_TARGET_TABLES: tuple[str, ...] = ("test", "bench", "example")
+
+
+def _parse_target_table(
+ cargo_data: dict, table: str, member_rel: str, repo: Path
+) -> tuple[str, ...]:
+ """Extract explicit-``path`` entries from one Cargo target table.
+
+ Only entries naming a ``path`` carry any signal here: Cargo already
+ discovers ``src/bin/*.rs``, ``tests/*.rs``, ``benches/*.rs`` and
+ ``examples/*.rs`` by convention, and those stay covered by the existing
+ never-flag globs — adding them again would just duplicate a root that
+ already exists.
+ """
+ entries = cargo_data.get(table, [])
+ if not isinstance(entries, list):
return ()
paths: list[str] = []
- for entry in bins:
+ for entry in entries:
if not isinstance(entry, dict):
continue
path_str = entry.get("path")
@@ -155,6 +172,19 @@ def _parse_bin_targets(cargo_data: dict, member_rel: str, repo: Path) -> tuple[s
return tuple(paths)
+def _parse_entry_targets(
+ cargo_data: dict, member_rel: str, repo: Path
+) -> tuple[tuple[str, ...], tuple[str, ...]]:
+ """``(bin_paths, reachability_root_paths)`` for one manifest's target tables."""
+ bin_paths = _parse_target_table(cargo_data, "bin", member_rel, repo)
+ root_paths = tuple(
+ path
+ for table in _ROOT_TARGET_TABLES
+ for path in _parse_target_table(cargo_data, table, member_rel, repo)
+ )
+ return bin_paths, root_paths
+
+
def _build_cargo_workspace_index(ctx) -> CargoWorkspaceIndex | None:
repo_path = getattr(ctx, "repo_path", None)
if not repo_path:
@@ -192,10 +222,11 @@ def _build_cargo_workspace_index(ctx) -> CargoWorkspaceIndex | None:
repo,
ws_deps=ws_deps_raw,
)
- root_bins = _parse_bin_targets(root_data, "", repo)
+ root_bins, root_roots = _parse_entry_targets(root_data, "", repo)
crates.append(CargoCrate(
name=str(root_pkg["name"]), src_dir="src",
dependencies=root_deps, bin_paths=root_bins,
+ reachability_root_paths=root_roots,
))
# Parse workspace-level shared dependencies
@@ -256,16 +287,75 @@ def _build_cargo_workspace_index(ctx) -> CargoWorkspaceIndex | None:
repo,
ws_deps=ws_deps_raw,
)
- bin_paths = _parse_bin_targets(member_data, member_rel, repo)
+ bin_paths, root_paths = _parse_entry_targets(member_data, member_rel, repo)
crates.append(CargoCrate(
name=str(name),
src_dir=src_dir,
dependencies=member_deps,
is_proc_macro=is_proc_macro,
bin_paths=bin_paths,
+ reachability_root_paths=root_paths,
))
+ crates.extend(_discover_isolated_crates(ctx, repo, tuple(crates)))
+
if not crates:
return None
log.debug("Built Cargo workspace index", crate_count=len(crates))
return CargoWorkspaceIndex(crates=tuple(crates), workspace_dependencies=ws_deps)
+
+
+def _discover_isolated_crates(
+ ctx, repo: Path, existing: tuple[CargoCrate, ...]
+) -> list[CargoCrate]:
+ """Manifests the workspace walk above never reads, for their targets only.
+
+ A crate with its own ``[workspace]`` or listed under the root's
+ ``exclude`` (ripgrep's ``fuzz/`` is both) is invisible to the member walk
+ entirely — fixing target parsing does nothing if the manifest is never
+ opened. For every parsed ``.rs`` file no existing crate already owns, walk
+ up to the nearest ``Cargo.toml`` and read its target tables.
+
+ Dependencies are not parsed here: nothing in this pass needs the crate
+ resolvable as a ``use``-able sibling, only whether its explicit-path
+ targets keep a file alive (#2936).
+ """
+ path_set = getattr(ctx, "path_set", None) or ()
+ probe = CargoWorkspaceIndex(crates=existing)
+ manifest_dirs: set[Path] = set()
+ for path in path_set:
+ if not path.endswith(".rs") or probe.lookup_crate_for_file(path) is not None:
+ continue
+ current = (repo / path).parent
+ while True:
+ if (current / "Cargo.toml").is_file():
+ manifest_dirs.add(current)
+ break
+ if current == repo or current.parent == current:
+ break
+ current = current.parent
+
+ crates: list[CargoCrate] = []
+ for manifest_dir in sorted(manifest_dirs):
+ try:
+ with open(manifest_dir / "Cargo.toml", "rb") as f:
+ data = tomllib.load(f)
+ except (OSError, tomllib.TOMLDecodeError):
+ continue
+ try:
+ member_rel = manifest_dir.relative_to(repo).as_posix()
+ except ValueError:
+ continue
+ if member_rel == ".":
+ member_rel = ""
+ bin_paths, root_paths = _parse_entry_targets(data, member_rel, repo)
+ if not bin_paths and not root_paths:
+ continue
+ pkg_name = (data.get("package") or {}).get("name") or member_rel or "isolated"
+ crates.append(CargoCrate(
+ name=str(pkg_name),
+ src_dir=f"{member_rel}/src" if member_rel else "src",
+ bin_paths=bin_paths,
+ reachability_root_paths=root_paths,
+ ))
+ return crates
diff --git a/tests/unit/ingestion/test_rust_cargo_targets.py b/tests/unit/ingestion/test_rust_cargo_targets.py
new file mode 100644
index 000000000..9d45a7777
--- /dev/null
+++ b/tests/unit/ingestion/test_rust_cargo_targets.py
@@ -0,0 +1,170 @@
+"""Cargo `[[bin]]`/`[[test]]`/`[[bench]]`/`[[example]]` targets with an explicit path.
+
+Cargo discovers src/bin/*.rs, tests/*.rs, benches/*.rs and examples/*.rs by
+convention; a target naming a non-default `path` is exactly the file Cargo
+cannot find any other way, so it is the one case with no signal besides the
+manifest itself (#2936).
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from pathlib import Path
+from typing import ClassVar
+
+import networkx as nx
+
+from repowise.core.analysis.dead_code import DeadCodeAnalyzer, DeadCodeKind
+from repowise.core.ingestion.graph import GraphBuilder
+from repowise.core.ingestion.models import FileInfo
+from repowise.core.ingestion.parser import ASTParser
+
+_PARSER = ASTParser()
+
+
+def _build(repo: Path, sources: dict[str, str]) -> nx.DiGraph:
+ builder = GraphBuilder(repo_path=repo)
+ for rel, body in sources.items():
+ p = repo / rel
+ p.parent.mkdir(parents=True, exist_ok=True)
+ p.write_text(body, encoding="utf-8")
+ for rel in sources:
+ if rel.endswith(".rs"):
+ abs_path = repo / rel
+ file_info = FileInfo(
+ path=rel,
+ abs_path=str(abs_path.resolve()),
+ language="rust",
+ size_bytes=100,
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ builder.add_file(_PARSER.parse_file(file_info, abs_path.read_bytes()))
+ return builder.build()
+
+
+def _unused(graph: nx.DiGraph) -> set[str]:
+ report = DeadCodeAnalyzer(graph, git_meta_map={}).analyze(
+ {
+ "detect_zombie_packages": False,
+ "detect_unused_internals": False,
+ "min_confidence": 0.0,
+ }
+ )
+ return {
+ f"{f.file_path}::{f.symbol_name}" if f.symbol_name else f.file_path
+ for f in report.findings
+ if f.kind in (DeadCodeKind.UNUSED_EXPORT, DeadCodeKind.UNREACHABLE_FILE)
+ }
+
+
+class TestWorkspaceMemberTargets:
+ """A target table on a normally-walked workspace member."""
+
+ _SOURCES: ClassVar[dict[str, str]] = {
+ "Cargo.toml": (
+ '[workspace]\nmembers = ["crates/net"]\n'
+ ),
+ "crates/net/Cargo.toml": (
+ '[package]\nname = "net"\nversion = "0.1.0"\n\n'
+ '[[bin]]\nname = "net_cli"\npath = "cli/main.rs"\n\n'
+ '[[test]]\nname = "wire"\npath = "harness/wire.rs"\n'
+ ),
+ "crates/net/src/lib.rs": "pub fn connect() {}\n",
+ "crates/net/cli/main.rs": "fn main() {}\n",
+ "crates/net/harness/wire.rs": "pub fn target() {}\n",
+ }
+
+ def test_bin_with_an_explicit_path_is_an_entry_point(self, tmp_path: Path) -> None:
+ graph = _build(tmp_path, self._SOURCES)
+ node = graph.nodes["crates/net/cli/main.rs"]
+ assert node.get("is_entry_point") is True
+
+ def test_test_with_an_explicit_path_is_a_reachability_root_not_an_entry(
+ self, tmp_path: Path
+ ) -> None:
+ graph = _build(tmp_path, self._SOURCES)
+ node = graph.nodes["crates/net/harness/wire.rs"]
+ assert node.get("is_reachability_root") is True
+ assert not node.get("is_entry_point")
+
+ def test_neither_target_file_is_reported_dead(self, tmp_path: Path) -> None:
+ unused = _unused(_build(tmp_path, self._SOURCES))
+ assert "crates/net/cli/main.rs" not in unused
+ assert "crates/net/harness/wire.rs" not in unused
+ assert "crates/net/harness/wire.rs::target" not in unused
+
+
+class TestBenchAndExampleTargets:
+ _SOURCES: ClassVar[dict[str, str]] = {
+ "Cargo.toml": (
+ '[package]\nname = "lib1"\nversion = "0.1.0"\n\n'
+ '[[bench]]\nname = "throughput"\npath = "perf/throughput.rs"\n\n'
+ '[[example]]\nname = "demo"\npath = "showcase/demo.rs"\n'
+ ),
+ "src/lib.rs": "pub fn work() {}\n",
+ "perf/throughput.rs": "pub fn target() {}\n",
+ "showcase/demo.rs": "fn main() {}\n",
+ }
+
+ def test_bench_and_example_targets_are_reachability_roots(self, tmp_path: Path) -> None:
+ graph = _build(tmp_path, self._SOURCES)
+ assert graph.nodes["perf/throughput.rs"].get("is_reachability_root") is True
+ assert graph.nodes["showcase/demo.rs"].get("is_reachability_root") is True
+
+ def test_neither_target_file_is_reported_dead(self, tmp_path: Path) -> None:
+ unused = _unused(_build(tmp_path, self._SOURCES))
+ assert "perf/throughput.rs" not in unused
+ assert "showcase/demo.rs" not in unused
+
+
+class TestTargetWithNoExplicitPathAddsNoRoot:
+ """A bare ``[[bin]]`` (no ``path``) names nothing new; convention covers it."""
+
+ _SOURCES: ClassVar[dict[str, str]] = {
+ "Cargo.toml": '[package]\nname = "lib1"\nversion = "0.1.0"\n\n[[bin]]\nname = "lib1"\n',
+ "src/lib.rs": "pub fn work() {}\n",
+ "src/main.rs": "fn main() {}\n",
+ }
+
+ def test_build_does_not_crash_and_adds_no_bogus_root(self, tmp_path: Path) -> None:
+ graph = _build(tmp_path, self._SOURCES)
+ # The conventional src/main.rs is unaffected either way; the point is
+ # a path-less entry must not be parsed into a (wrong) empty-string path.
+ assert "" not in graph.nodes
+
+
+class TestIsolatedCrateOutsideTheWorkspaceWalk:
+ """The ripgrep ``fuzz/`` shape: a sub-crate with its own ``[workspace]``,
+ excluded from the root's member list, naming a target by an explicit path.
+ """
+
+ _SOURCES: ClassVar[dict[str, str]] = {
+ "Cargo.toml": (
+ '[package]\nname = "ripgrep"\nversion = "0.1.0"\n\n'
+ '[workspace]\nmembers = ["."]\nexclude = ["fuzz"]\n'
+ ),
+ "src/lib.rs": "pub fn run() {}\n",
+ "fuzz/Cargo.toml": (
+ '[package]\nname = "rg-fuzz"\nversion = "0.0.0"\n\n'
+ "[workspace]\n\n"
+ '[[bin]]\nname = "fuzz_glob"\npath = "fuzz_targets/fuzz_glob.rs"\n'
+ "test = false\ndoc = false\n"
+ ),
+ "fuzz/fuzz_targets/fuzz_glob.rs": "pub fn target() {}\n",
+ }
+
+ def test_the_isolated_crates_bin_target_is_not_reported_dead(self, tmp_path: Path) -> None:
+ unused = _unused(_build(tmp_path, self._SOURCES))
+ assert "fuzz/fuzz_targets/fuzz_glob.rs" not in unused
+ assert "fuzz/fuzz_targets/fuzz_glob.rs::target" not in unused
+
+ def test_the_isolated_crates_bin_target_is_stamped_an_entry_point(
+ self, tmp_path: Path
+ ) -> None:
+ graph = _build(tmp_path, self._SOURCES)
+ assert graph.nodes["fuzz/fuzz_targets/fuzz_glob.rs"].get("is_entry_point") is True