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(dead-code): a co-change edge is not an importer in JVM, Go and C++ reachability (#3658)

Swati Ahuja9b33bbceaaf0
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
index 47673ce47..5921c6b5b 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -68,6 +68,7 @@ from .entry_shape import (
is_program,
)
from .file_reachability import (
+ _NON_DEPENDENCY_EDGES,
CSHARP_SUFFIX,
PackageFileMap,
ReachabilityRescues,
@@ -2224,7 +2225,10 @@ class DeadCodeAnalyzer:
def _has_cross_package_importer(self, pkg: str, files: list[str]) -> bool:
"""Whether anything outside *pkg* depends on a file inside it."""
for f in files:
- for pred in self.graph.predecessors(f):
+ for pred, _f, edge_data in self.graph.in_edges(f, data=True):
+ # Committed together is history, not a dependency.
+ if edge_data.get("edge_type") in _NON_DEPENDENCY_EDGES:
+ continue
pred_str = str(pred)
if pred_str.startswith("external:"):
# Third-party imports don't count as cross-package
diff --git a/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
index 265d92ec3..6072be801 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
@@ -172,7 +172,10 @@ def is_cpp_file_reachable(
generic skips (``is_entry_point``, ``is_test``, ``is_never_flag``,
fixture / barrel filters). Rules below short-circuit.
"""
- if graph.in_degree(node) > 0:
+ # Called, not imported at module level: file_reachability imports this module.
+ from .file_reachability import has_dependency_importer
+
+ if has_dependency_importer(graph, node):
return True
# ``int main`` / ``WinMain`` / fuzzer entry — the file IS the binary
@@ -198,7 +201,7 @@ def is_cpp_file_reachable(
if sibling == node:
continue
sib_data = graph.nodes.get(sibling, {})
- sib_has_importer = graph.in_degree(sibling) > 0
+ sib_has_importer = has_dependency_importer(graph, sibling)
if is_reachability_root(sib_data):
has_sibling_entry = True
break
diff --git a/packages/core/src/repowise/core/analysis/dead_code/go_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/go_reachability.py
index 8ba3e9ee2..e7ec0bf06 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/go_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/go_reachability.py
@@ -64,13 +64,16 @@ def is_go_file_reachable(
package is an entry ``package main``). See the module docstring for the
``func init`` rationale.
"""
- if graph.in_degree(node) > 0:
+ # Called, not imported at module level: file_reachability imports this module.
+ from .file_reachability import has_dependency_importer
+
+ if has_dependency_importer(graph, node):
return True
for sibling in package_files.get(_pkg_dir(node), ()):
if sibling == node:
continue
- if graph.in_degree(sibling) > 0:
+ if has_dependency_importer(graph, sibling):
return True
if is_reachability_root(graph.nodes.get(sibling, {})):
return True
diff --git a/packages/core/src/repowise/core/analysis/dead_code/jvm_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/jvm_reachability.py
index d39343acc..f4e275cdf 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/jvm_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/jvm_reachability.py
@@ -206,7 +206,10 @@ def is_jvm_file_reachable(
(Spring/Jakarta/Quarkus/Micronaut/JPA);
- it defines a ``main`` method.
"""
- if graph.in_degree(node) > 0:
+ # Called, not imported at module level: file_reachability imports this module.
+ from .file_reachability import has_dependency_importer
+
+ if has_dependency_importer(graph, node):
return True
if _file_defines_entry_class(graph, node):
@@ -216,7 +219,7 @@ def is_jvm_file_reachable(
if sibling == node:
continue
sib_data = graph.nodes.get(sibling, {})
- if graph.in_degree(sibling) > 0:
+ if has_dependency_importer(graph, sibling):
return True
if is_reachability_root(sib_data):
return True
diff --git a/tests/unit/analysis/test_cpp_reachability.py b/tests/unit/analysis/test_cpp_reachability.py
index c36e147e9..d9ed138ac 100644
--- a/tests/unit/analysis/test_cpp_reachability.py
+++ b/tests/unit/analysis/test_cpp_reachability.py
@@ -87,6 +87,23 @@ class TestCppFileReachable:
pkgs = build_cpp_package_files(graph)
assert is_cpp_file_reachable("a/foo.h", graph, pkgs) is True
+ def test_co_change_is_not_an_importer(self) -> None:
+ # Committed together is history: no include reaches the file or its sibling.
+ graph = _build_graph(
+ {
+ "a/foo.cc": _cpp_file(),
+ "a/foo_impl.cc": _cpp_file(),
+ "b/bar.cc": _cpp_file(),
+ },
+ edges=[
+ ("b/bar.cc", "a/foo.cc", {"edge_type": "co_changes"}),
+ ("b/bar.cc", "a/foo_impl.cc", {"edge_type": "co_changes"}),
+ ],
+ )
+ pkgs = build_cpp_package_files(graph)
+ assert is_cpp_file_reachable("a/foo.cc", graph, pkgs) is False
+ assert is_cpp_file_reachable("a/foo_impl.cc", graph, pkgs) is False
+
def test_main_carrier_is_reachable(self) -> None:
graph = _build_graph(
{
diff --git a/tests/unit/analysis/test_go_reachability.py b/tests/unit/analysis/test_go_reachability.py
index 1664f7b74..9dbf9b4f0 100644
--- a/tests/unit/analysis/test_go_reachability.py
+++ b/tests/unit/analysis/test_go_reachability.py
@@ -86,6 +86,23 @@ class TestGoReachabilityHelper:
# pruner.go itself has in_degree 0 but cache.go (its sibling) is imported.
assert is_go_file_reachable("pkg/cache/pruner.go", g, pkgs)
+ def test_co_change_is_not_an_importer(self):
+ """A package only ever committed alongside another is not imported."""
+ g = _build_graph(
+ nodes={
+ "pkg/cache/cache.go": _go_file(),
+ "pkg/cache/pruner.go": _go_file(),
+ "pkg/user/user.go": _go_file(),
+ },
+ edges=[
+ ("pkg/user/user.go", "pkg/cache/cache.go", {"edge_type": "co_changes"}),
+ ("pkg/user/user.go", "pkg/cache/pruner.go", {"edge_type": "co_changes"}),
+ ],
+ )
+ pkgs = build_go_package_files(g)
+ assert not is_go_file_reachable("pkg/cache/cache.go", g, pkgs)
+ assert not is_go_file_reachable("pkg/cache/pruner.go", g, pkgs)
+
def test_orphaned_package_is_unreachable(self):
"""A package nobody imports — no entry sibling — stays unreachable."""
g = _build_graph(
diff --git a/tests/unit/analysis/test_jvm_reachability.py b/tests/unit/analysis/test_jvm_reachability.py
index bf3e41e3d..88478a645 100644
--- a/tests/unit/analysis/test_jvm_reachability.py
+++ b/tests/unit/analysis/test_jvm_reachability.py
@@ -111,6 +111,23 @@ class TestJvmFileReachable:
# Helper.java has no direct importer but its sibling User.java does.
assert is_jvm_file_reachable("app/svc/Helper.java", graph, pkgs) is True
+ def test_co_change_is_not_an_importer(self) -> None:
+ # Committed together is history: neither the file nor its sibling is imported.
+ graph = _build_graph(
+ {
+ "app/svc/User.java": _java_file(),
+ "app/svc/Helper.java": _java_file(),
+ "app/web/Controller.java": _java_file(),
+ },
+ edges=[
+ ("app/web/Controller.java", "app/svc/User.java", {"edge_type": "co_changes"}),
+ ("app/web/Controller.java", "app/svc/Helper.java", {"edge_type": "co_changes"}),
+ ],
+ )
+ pkgs = build_jvm_package_files(graph)
+ assert is_jvm_file_reachable("app/svc/User.java", graph, pkgs) is False
+ assert is_jvm_file_reachable("app/svc/Helper.java", graph, pkgs) is False
+
def test_stereotype_annotated_class_is_reachable(self) -> None:
graph = _build_graph(
{
diff --git a/tests/unit/dead_code/test_zombie_packages.py b/tests/unit/dead_code/test_zombie_packages.py
index a20253ff3..badbbd878 100644
--- a/tests/unit/dead_code/test_zombie_packages.py
+++ b/tests/unit/dead_code/test_zombie_packages.py
@@ -155,6 +155,28 @@ def _run_zombie_only(git_meta: dict) -> list:
]
+def test_co_change_is_not_a_cross_package_importer():
+ """A file only ever committed alongside pkgA does not keep pkgA alive."""
+
+ def zombies(g) -> set[str]:
+ report = DeadCodeAnalyzer(g, git_meta_map={}).analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_unused_exports": False,
+ "detect_unused_internals": False,
+ "min_confidence": 0.0,
+ }
+ )
+ return {f.file_path for f in report.findings if f.kind == DeadCodeKind.ZOMBIE_PACKAGE}
+
+ g = _zombie_graph()
+ g.add_edge("pkgB/sentinel.py", "pkgA/alpha.py", edge_type="co_changes")
+ assert "pkgA" in zombies(g)
+
+ g.add_edge("pkgB/sentinel.py", "pkgA/beta.py", edge_type="imports")
+ assert "pkgA" not in zombies(g)
+
+
def test_zombie_package_primary_owner_from_git_meta():
"""primary_owner on a zombie finding is taken from git_meta_map.