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): read generated files as unindexed users (#3639)

Swati Ahuja087a557870c3
diff --git a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
index ea80829e4..cacb52b5a 100644
--- a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
@@ -135,17 +135,13 @@ def _analyze_live(repo_path: Path, config: dict, notices: Any) -> DeadCodeReport
# parsed_files powers the source-scan rescues (dynamic-import markers,
# bundler resolve.alias targets, export-alias maps).
- stats = graph_builder.traversal_stats
analyzer = DeadCodeAnalyzer(
graph_builder.graph(),
git_meta_map,
parsed_files=graph_builder._parsed_files,
source_map=source_map,
repo_root=repo_path,
- unindexed_source_files=[
- (skipped.path, skipped.reason)
- for skipped in (*stats.skipped_source_files, *stats.unknown_language_files)
- ],
+ unindexed_source_files=graph_builder.traversal_stats.unindexed_source_files(),
dotnet_index=getattr(graph_builder, "dotnet_index", None),
)
return analyzer.analyze(config)
diff --git a/packages/core/src/repowise/core/ingestion/traverser.py b/packages/core/src/repowise/core/ingestion/traverser.py
index c51beb3d0..b1bc1974b 100644
--- a/packages/core/src/repowise/core/ingestion/traverser.py
+++ b/packages/core/src/repowise/core/ingestion/traverser.py
@@ -162,6 +162,20 @@ class TraversalStats:
workers would name a different subset on every run.
"""
+ def unindexed_source_files(self) -> list[tuple[str, str]]:
+ """``(path, reason)`` for each dropped file the dead-code analyzer reads.
+
+ Without them it cannot tell "nothing uses this" from "its user was
+ never read" (#1237). Generated files count too: they are skipped for
+ being generated, not for naming nothing, so a header function called
+ only from a jnigen ``.c`` is used. Sorted, so a read budget that runs
+ out skips the same generated files every run.
+ """
+ return [
+ *((s.path, s.reason) for s in (*self.skipped_source_files, *self.unknown_language_files)),
+ *((path, "generated") for path in sorted(self.generated_files)),
+ ]
+
log = structlog.get_logger(__name__)
diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.py
index 4bb392a4a..d52d8b6ac 100644
--- a/packages/core/src/repowise/core/pipeline/incremental.py
+++ b/packages/core/src/repowise/core/pipeline/incremental.py
@@ -869,7 +869,7 @@ def run_partial_analysis(
# files they cover; the stored rows carry every other file, which is
# what init's analyzer had and this path did not.
_dead_code_git_meta = {**(stored_git_meta or {}), **git_meta_map}
- # Files the walk dropped on size or for having no language spec. This
+ # Files the walk dropped (size, no language spec, generated). This
# report is persisted repo-wide (see below), so without them an update
# would re-derive the verdicts init had clamped and write them back
# unclamped — the #1237 cascade would return, looking like the fix had
@@ -881,13 +881,9 @@ def run_partial_analysis(
parsed_files=graph_builder._parsed_files,
source_map=source_map,
repo_root=repo_path,
- unindexed_source_files=[
- (skipped.path, skipped.reason)
- for skipped in (
- *getattr(_traversal_stats, "skipped_source_files", []),
- *getattr(_traversal_stats, "unknown_language_files", []),
- )
- ],
+ unindexed_source_files=(
+ _traversal_stats.unindexed_source_files() if _traversal_stats is not None else []
+ ),
dotnet_index=getattr(graph_builder, "dotnet_index", None),
)
# Repo-wide, and persisted repo-wide. The detectors were always
diff --git a/packages/core/src/repowise/core/pipeline/phases/analysis.py b/packages/core/src/repowise/core/pipeline/phases/analysis.py
index bf070ddb6..f6aeeb623 100644
--- a/packages/core/src/repowise/core/pipeline/phases/analysis.py
+++ b/packages/core/src/repowise/core/pipeline/phases/analysis.py
@@ -64,17 +64,10 @@ async def _run_dead_code_analysis(
if progress:
progress.on_phase_start("dead_code", dead_code_steps)
- # Files the traverser dropped on size, plus those it dropped for having
- # no language spec. Without them the analyzer cannot tell "nothing
- # imports this" from "the importer was never read", which is the
- # cascade in #1237.
- unindexed_source_files = [
- (skipped.path, skipped.reason)
- for skipped in (
- *getattr(traversal_stats, "skipped_source_files", []),
- *getattr(traversal_stats, "unknown_language_files", []),
- )
- ]
+ # Files the traverser dropped; see TraversalStats.unindexed_source_files.
+ unindexed_source_files = (
+ traversal_stats.unindexed_source_files() if traversal_stats is not None else []
+ )
analyzer = DeadCodeAnalyzer(
graph_builder.graph(),
diff --git a/tests/unit/dead_code/test_cpp_name_uses.py b/tests/unit/dead_code/test_cpp_name_uses.py
index 04ca4c310..430a39eb5 100644
--- a/tests/unit/dead_code/test_cpp_name_uses.py
+++ b/tests/unit/dead_code/test_cpp_name_uses.py
@@ -11,6 +11,8 @@ from __future__ import annotations
from pathlib import Path
+import pytest
+
from repowise.core.analysis.dead_code import DeadCodeAnalyzer
from repowise.core.analysis.dead_code.c_name_uses import (
declared_names,
@@ -189,6 +191,37 @@ def test_analyzer_drops_the_typedef_tag_end_to_end(tmp_path: Path):
assert {f.symbol_name for f in report.findings} == {"Offset"}
+_DARTJNI_H = b"static inline void load_method(void)\n{\n}\n"
+
+
[email protected](("generated", "expected"), [(True, set()), (False, {"load_method"})])
+def test_header_helper_called_only_from_generated_code(tmp_path: Path, generated, expected):
+ # Flutter FFI: a jnigen ``.c`` (skipped at ingestion as generated) is the
+ # only caller of a ``static inline`` in the plugin header.
+ (tmp_path / "bindings.c").write_text(
+ "// Autogenerated by jnigen. DO NOT EDIT!\nvoid f(void) { load_method(); }\n"
+ )
+ symbol = {
+ "name": "load_method",
+ "kind": "function",
+ "visibility": "private",
+ "decorators": [],
+ "start_line": 1,
+ "end_line": 3,
+ "language": "c",
+ }
+ graph = _build_graph({"dartjni.h": {"language": "c", "symbols": [symbol]}})
+ report = DeadCodeAnalyzer(
+ graph,
+ source_map={"dartjni.h": _DARTJNI_H},
+ repo_root=tmp_path,
+ unindexed_source_files=[("bindings.c", "generated")] if generated else [],
+ ).analyze(
+ {"detect_unreachable_files": False, "detect_zombie_packages": False, "min_confidence": 0.0}
+ )
+ assert {f.symbol_name for f in report.findings} == expected
+
+
def test_header_prototype_and_comment_banner_are_not_uses():
# ZoomIt's ``ConvertToUnicode`` and powerrename's ``SetRegBoolean``: the
# only other mentions are a banner comment and the header prototype.
diff --git a/tests/unit/ingestion/test_traverser.py b/tests/unit/ingestion/test_traverser.py
index 8753c476b..d80d8235b 100644
--- a/tests/unit/ingestion/test_traverser.py
+++ b/tests/unit/ingestion/test_traverser.py
@@ -1390,6 +1390,20 @@ class TestIsGenerated:
assert traverser.stats.skipped_generated == 1
assert traverser.stats.generated_files == ["gen.go"]
+ def test_generated_files_reach_the_dead_code_unindexed_list(self, tmp_path: Path) -> None:
+ # A jnigen binding is the only caller of its plugin header's helpers.
+ (tmp_path / "bindings.c").write_text(
+ '// Autogenerated by jnigen. DO NOT EDIT!\n#include "dartjni.h"\n'
+ )
+ (tmp_path / "dartjni.h").write_text("static inline void load_method(void) {}\n")
+ (tmp_path / "api.def").write_text("EXPORTS\n load_method\n")
+ traverser = FileTraverser(tmp_path)
+ assert [f.path for f in traverser.traverse()] == ["dartjni.h"]
+ assert traverser.stats.unindexed_source_files() == [
+ ("api.def", "unknown_language"),
+ ("bindings.c", "generated"),
+ ]
+
class TestConcurrentLazyInit:
"""The lazy scans behind ``_build_file_info`` run once, not once per worker.