Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(dead-code): Gradle and Android entry roots for convention plugins, test modules and manifests (#3657)
Swati Ahujacd90191e723d
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.pyindex 83e758eef..47673ce47 100644--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py@@ -2177,8 +2177,15 @@ class DeadCodeAnalyzer:# with a top-level ``main`` (Dart ``lib/main.dart``). A root alone is# not enough: a Rust ``lib.rs`` is a root and the library's surface.# One program file is enough, even a stray script: this errs toward- # not calling a package dead.- files = [f for f in files if not self.graph.nodes.get(f, {}).get("is_test")]+ # not calling a package dead. A build-run source set (``is_never_flag``)+ # counts as a test. Files only: a test file's symbols carry no test flag.+ files = [+ f+ for f in files+ if (nd := self.graph.nodes.get(f, {})).get("node_type") != "symbol"+ and not nd.get("is_test")+ and not nd.get("is_never_flag")+ ]if any(is_program(f, self._source_map.get(f, b""))or defines_main(self.graph, f)diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/android_manifest.py b/packages/core/src/repowise/core/ingestion/framework_edges/android_manifest.pyindex 1091f46f2..047567091 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/android_manifest.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/android_manifest.py@@ -6,9 +6,12 @@ manifest is the runtime's hand-off into source — none of those classreferences appear in any ``import`` statement, so the dead-code passwould otherwise flag every Activity that has no in-source caller.-Strategy: parse every ``AndroidManifest.xml`` we can find, pull the-``android:name`` attributes, and emit a synthetic ``framework`` edge-from the manifest file to the named class's source file.+Strategy: read every ``AndroidManifest.xml`` on disk (the traverser does+not index XML), pull the component ``android:name`` attributes, and link+the named class's source file from the ``framework:android`` anchor. A+relative name (``.MainActivity``) is completed with the manifest+``package``, or with the module's Gradle ``namespace`` when the manifest+has none (AGP 7+)."""from __future__ import annotations@@ -18,65 +21,96 @@ from collections.abc import Iteratorfrom pathlib import Pathfrom typing import TYPE_CHECKING, Any+from repowise.core.fs_walk import glob_via++from ..framework_facts import ANDROIDfrom ..resolvers import ResolverContextfrom .base import (DetectionContext,FrameworkHandler,- _add_edge_if_new,+ anchor_edge,)if TYPE_CHECKING:import networkx as nx-_ANDROID_NAME_RE = re.compile(r"android:name=\"([\w.$]+)\"")+# XML allows either quote and spaces around ``=``.+_ANDROID_NAME_RE = re.compile(r"""android:name\s*=\s*['"]([\w.$]+)['"]""")_ANDROID_COMPONENT_TAGS = ("<activity", "<service", "<receiver","<provider", "<application")-def _has_android_manifests(parsed_files: dict[str, Any]) -> bool:- return any(p.endswith("AndroidManifest.xml") for p in parsed_files)+_MANIFEST_PACKAGE_RE = re.compile(r"""<manifest\b[^>]*?\bpackage\s*=\s*['"]([\w.]+)['"]""")+_GRADLE_NAMESPACE_RE = re.compile(r"""\bnamespace\s*=?\s*['"]([\w.]+)['"]""")-def _add_android_edges(- graph: nx.DiGraph,- parsed_files: dict[str, Any],- path_set: set[str],- ctx: ResolverContext,-) -> int:- count = 0+def _has_jvm_sources(parsed_files: dict[str, Any]) -> bool:+ return any(p.endswith((".java", ".kt")) for p in parsed_files)++def _add_android_edges(graph: nx.DiGraph, path_set: set[str], ctx: ResolverContext) -> int:+ if ctx.repo_path is None:+ return 0try:from ..resolvers.jvm_workspace import get_or_build_jvm_indexjvm_index = get_or_build_jvm_index(ctx)except Exception:- jvm_index = None+ return 0- for path in list(path_set):- if not path.endswith("AndroidManifest.xml"):- continue- text = _read_manifest(parsed_files, path)- if text is None or jvm_index is None:- continue- for fqn in _component_class_names(text):- for target in jvm_index.files_for_fqn(fqn):- if target in path_set and _add_edge_if_new(graph, path, target):- count += 1- node = graph.nodes.get(target)- if node is not None:- node["is_entry_point"] = True+ count = 0+ for name in _registered_class_names(ctx.repo_path.resolve(), ctx):+ for target in jvm_index.files_for_fqn(name):+ if target not in path_set:+ continue+ count += anchor_edge(graph, ANDROID, target)+ node = graph.nodes.get(target)+ if node is not None:+ node["is_entry_point"] = Truereturn count-def _read_manifest(parsed_files: dict[str, Any], path: str) -> str | None:- try:- return Path(parsed_files[path].file_info.abs_path).read_text(- encoding="utf-8", errors="ignore"- )- except (OSError, KeyError, AttributeError):- return None+def _registered_class_names(root: Path, ctx: ResolverContext) -> Iterator[str]:+ """Fully-qualified component class names from every manifest under *root*."""+ for manifest in glob_via(+ ctx.walk_snapshot, root, "AndroidManifest.xml", prune_nested_git=ctx.prune_nested_git+ ):+ try:+ text = manifest.read_text(encoding="utf-8", errors="ignore")+ except OSError:+ continue+ package = _manifest_package(manifest, text)+ for name in _component_class_names(text):+ yield _qualify(name, package)+++def _manifest_package(manifest: Path, text: str) -> str:+ """The package relative component names complete against.++ The manifest ``package`` attribute, else the ``namespace`` of the module+ whose ``src/<set>/`` holds the manifest (or of the manifest's own folder,+ the pre-``src`` layout).+ """+ if m := _MANIFEST_PACKAGE_RE.search(text):+ return m.group(1)+ module = manifest.parent.parent.parent if manifest.parent.parent.name == "src" else manifest.parent+ for build_name in ("build.gradle.kts", "build.gradle"):+ try:+ build_text = (module / build_name).read_text(encoding="utf-8", errors="ignore")+ except OSError:+ continue+ if m := _GRADLE_NAMESPACE_RE.search(build_text):+ return m.group(1)+ return ""+++def _qualify(name: str, package: str) -> str:+ """Android's rule: ``.Foo`` and a bare ``Foo`` live in *package*."""+ if name.startswith(".") or "." not in name:+ return f"{package}.{name.lstrip('.')}" if package else name.lstrip(".")+ return namedef _component_class_names(text: str) -> Iterator[str]:@@ -89,14 +123,13 @@ def _component_class_names(text: str) -> Iterator[str]:for chunk in text.split(tag)[1:]:head = chunk.split(">", 1)[0]for m in _ANDROID_NAME_RE.finditer(head):- fqn = m.group(1).lstrip(".")- if fqn:- yield fqn+ if m.group(1).strip("."):+ yield m.group(1)class _AndroidManifestHandler:def detect(self, dctx: DetectionContext) -> bool:- return _has_android_manifests(dctx.parsed_files)+ return _has_jvm_sources(dctx.parsed_files)def add_edges(self,@@ -105,7 +138,7 @@ class _AndroidManifestHandler:ctx: ResolverContext,path_set: set[str],) -> int:- return _add_android_edges(graph, parsed_files, path_set, ctx)+ return _add_android_edges(graph, path_set, ctx)HANDLERS: list[FrameworkHandler] = [_AndroidManifestHandler()]diff --git a/packages/core/src/repowise/core/ingestion/framework_facts.py b/packages/core/src/repowise/core/ingestion/framework_facts.pyindex 27dab75ac..9910c1f21 100644--- a/packages/core/src/repowise/core/ingestion/framework_facts.py+++ b/packages/core/src/repowise/core/ingestion/framework_facts.py@@ -124,6 +124,9 @@ def detect_php_framework(manifest: ComposerManifest) -> FrameworkFacts | None:return next((facts for facts in PHP_FRAMEWORKS if facts.matches(manifest)), None)+#: The runtime that starts the components an ``AndroidManifest.xml`` registers.+ANDROID = FrameworkFacts(name="Android", anchor="framework:android")+ANGULAR = FrameworkFacts(name="Angular", anchor="framework:angular")#: Workspace config naming what an Angular build loads: Angular CLI'sdiff --git a/packages/core/src/repowise/core/ingestion/graph_warmups.py b/packages/core/src/repowise/core/ingestion/graph_warmups.pyindex d9e11838e..6d2561cda 100644--- a/packages/core/src/repowise/core/ingestion/graph_warmups.py+++ b/packages/core/src/repowise/core/ingestion/graph_warmups.py@@ -66,6 +66,31 @@ def _stamp_entry(graph: Any, parsed_files: dict, path: str) -> None:setattr(info, flag, True)+def _is_android_test_project(plugins: frozenset[str]) -> bool:+ """Whether a Gradle module applies ``com.android.test``, directly or through+ a version-catalog alias (``android.test``) or a convention plugin named for+ it (``nowinandroid.android.test``)."""+ return any(p == "android.test" or p.endswith(".android.test") for p in plugins)+++def _default_package_definers(graph: Any, index: Any, names: set[str]) -> set[str]:+ """JVM files outside every package that define a top-level class in *names*."""+ found: set[str] = set()+ for path in list(graph.nodes()):+ if not str(path).endswith((".java", ".kt")) or index.package_for_file(path):+ continue+ for succ in graph.successors(path):+ sym = graph.nodes[succ]+ if (+ sym.get("node_type") == "symbol"+ and sym.get("name") in names+ and not sym.get("parent_name")+ and graph.edges[path, succ].get("edge_type") == "defines"+ ):+ found.add(path)+ return found++def _warmup_jvm(ctx: ResolverContext) -> None:from .resolvers.jvm_workspace import get_or_build_jvm_index@@ -92,11 +117,14 @@ def _warmup_jvm(ctx: ResolverContext) -> None:entry_fqns.update(build_script_class_names(ctx))- for fqn in entry_fqns:- for path in index.files_for_fqn(fqn):- node = graph.nodes.get(path)- if node is not None:- node["is_reachability_root"] = True+ root_paths = {path for fqn in entry_fqns for path in index.files_for_fqn(fqn)}+ # A bare name is a default-package class, which the index does not key.+ if bare := {n for n in entry_fqns if "." not in n}:+ root_paths.update(_default_package_definers(graph, index, bare))+ for path in root_paths:+ node = graph.nodes.get(path)+ if node is not None:+ node["is_reachability_root"] = True# Stamp every JVM source file under a non-``main`` Gradle source-set# (``testFixtures``, ``integrationTest``, ``javaPoet``, ``jcstress``,@@ -107,7 +135,9 @@ def _warmup_jvm(ctx: ResolverContext) -> None:# beyond the hardcoded never-flag globs and picks up arbitrary# repo-defined names (Caffeine's ``javaPoet`` and ``jcstress`` are# not Gradle-builtin conventions). The check uses the workspace- # source-set ``src_dirs`` discovered by ``jvm_gradle.py``.+ # source-set ``src_dirs`` discovered by ``jvm_gradle.py``. Every source+ # set of an Android test module (benchmarks, baseline profiles) counts:+ # the instrumentation runner runs its ``main`` too.try:from .resolvers.jvm_gradle import get_or_build_jvm_gradle_index@@ -119,8 +149,9 @@ def _warmup_jvm(ctx: ResolverContext) -> None:non_main_prefixes: list[str] = []for project in gradle_index.projects.values():base = project.root_dir.rstrip("/")+ android_test = _is_android_test_project(project.plugins)for ss in project.source_sets.values():- if ss.is_main:+ if ss.is_main and not android_test:continuefor src_dir in ss.src_dirs:prefix = f"{base}/{src_dir}/" if base else f"{src_dir}/"diff --git a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.pyindex d2b7e93cf..03ca1ac44 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py@@ -64,6 +64,11 @@ _ROOT_PROJECT_NAME_RE = re.compile(r'rootProject\s*\.\s*name\s*=\s*"([^"]+)"')# A quoted fully-qualified class name: lowercase package segments, then a# capitalised type (``'org.elasticsearch.system.indices.SystemIndicesQA'``)._QUOTED_FQN_RE = re.compile(r"""['"]((?:[a-z_]\w*\.)+[A-Z]\w*)['"]""")+# A default-package class (``build-logic`` convention plugins) has no dots, so+# only a class-naming key vouches for the bare name.+_KEYED_BARE_CLASS_RE = re.compile(+ r"""\b(?:implementationClass|mainClass)\s*(?:=|\.set\s*\()\s*['"]([A-Z]\w*)['"]"""+)_DEFAULT_MAIN_SRC_ROOTS = ("src/main/java", "src/main/kotlin")_DEFAULT_TEST_SRC_ROOTS = ("src/test/java", "src/test/kotlin")@@ -441,13 +446,14 @@ def build_jvm_gradle_index(def build_script_class_names(ctx: ResolverContext) -> set[str]:- """Fully-qualified class names quoted in any Gradle build script.+ """Class names quoted in any Gradle build script, fully qualified or bare.A build names a class it loads by reflection: a plugin's``esplugin { classname '...' }``, ``gradlePlugin`` ``implementationClass``,an application ``mainClass``. Every build script in the tree is read,since a settings file that adds projects by walking directories namesnone of them. A name only counts once it resolves to a class in the repo.+ A name without a package is the default package's class."""names: set[str] = set()if ctx.repo_path is None:@@ -459,9 +465,11 @@ def build_script_class_names(ctx: ResolverContext) -> set[str]:prune_nested_git=ctx.prune_nested_git,):try:- names.update(_QUOTED_FQN_RE.findall(script.read_text(encoding="utf-8", errors="ignore")))+ text = script.read_text(encoding="utf-8", errors="ignore")except OSError:continue+ names.update(_QUOTED_FQN_RE.findall(text))+ names.update(_KEYED_BARE_CLASS_RE.findall(text))return namesdiff --git a/tests/unit/dead_code/test_android_jvm_roots.py b/tests/unit/dead_code/test_android_jvm_roots.pynew file mode 100644index 000000000..30d2a72b4--- /dev/null+++ b/tests/unit/dead_code/test_android_jvm_roots.py@@ -0,0 +1,146 @@+"""Android and Gradle classes the build or the runtime starts, never an importer.++Shapes from nowinandroid and ktor-samples, every one reported dead: a+``build-logic`` convention plugin in the default package registered by+``implementationClass``, a ``com.android.test`` benchmark module, an app whose+launcher activity the manifest names relative to the Gradle ``namespace``, and+a sample module whose directory name makes all of it test code.+"""++from __future__ import annotations++from pathlib import Path++from repowise.core.analysis.dead_code import DeadCodeAnalyzer, DeadCodeKind+from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder++_LAUNCHER = (+ "<intent-filter>\n"+ ' <action android:name="android.intent.action.MAIN" />\n'+ ' <category android:name="android.intent.category.LAUNCHER" />\n'+ "</intent-filter>\n"+)++_FILES = {+ "settings.gradle.kts": (+ 'include(":catalog")\ninclude(":legacy")\ninclude(":benchmarks")\n'+ 'include(":orphan")\ninclude(":jwt-auth-tests")\n'+ ),+ # Convention plugins: one registered by bare name, one nothing names.+ "build-logic/settings.gradle.kts": 'include(":convention")\n',+ "build-logic/convention/build.gradle.kts": (+ "plugins { `kotlin-dsl` }\n"+ "gradlePlugin {\n plugins {\n"+ ' register("androidLibrary") {\n'+ ' id = "sample.android.library"\n'+ ' implementationClass = "AndroidLibraryConventionPlugin"\n'+ " }\n }\n}\n"+ ),+ "build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt": (+ "class AndroidLibraryConventionPlugin {\n fun apply() {}\n}\n"+ ),+ "build-logic/legacy/src/main/kotlin/UnregisteredPlugin.kt": (+ "class UnregisteredPlugin {\n fun apply() {}\n}\n"+ ),+ # App whose manifest has no ``package``: ``.CatalogActivity`` completes+ # against the Gradle ``namespace``.+ "catalog/build.gradle.kts": (+ 'plugins { id("com.android.application") }\n'+ 'android {\n namespace = "org.sample.catalog"\n}\n'+ ),+ "catalog/src/main/AndroidManifest.xml": (+ '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'+ " <application>\n"+ f' <activity android:name=".CatalogActivity" android:exported="true">\n{_LAUNCHER}'+ " </activity>\n </application>\n</manifest>\n"+ ),+ "catalog/src/main/kotlin/org/sample/catalog/CatalogActivity.kt": (+ "package org.sample.catalog\n\nclass CatalogActivity {\n fun onCreate() {}\n}\n"+ ),+ # Older layout: the manifest ``package`` qualifies a bare service name.+ "legacy/build.gradle": "apply plugin: 'com.android.application'\n",+ "legacy/src/main/AndroidManifest.xml": (+ '<manifest xmlns:android="http://schemas.android.com/apk/res/android"\n'+ ' package="org.sample.legacy">\n'+ ' <application>\n <service android:name="SyncService" />\n'+ " </application>\n</manifest>\n"+ ),+ "legacy/src/main/java/org/sample/legacy/SyncService.java": (+ "package org.sample.legacy;\n\npublic class SyncService {\n}\n"+ ),+ # A ``com.android.test`` module, applied through a convention plugin.+ "benchmarks/build.gradle.kts": (+ "plugins {\n alias(libs.plugins.sample.android.test)\n}\n"+ 'android {\n namespace = "org.sample.benchmarks"\n'+ ' targetProjectPath = ":catalog"\n}\n'+ ),+ "benchmarks/src/main/kotlin/org/sample/benchmarks/StartupBenchmark.kt": (+ "package org.sample.benchmarks\n\nclass StartupBenchmark {\n fun startup() {}\n}\n"+ ),+ # A library module with a manifest that registers nothing.+ "orphan/build.gradle.kts": (+ 'plugins { id("com.android.library") }\n'+ 'android {\n namespace = "org.sample.orphan"\n}\n'+ ),+ "orphan/src/main/AndroidManifest.xml": "<manifest />\n",+ "orphan/src/main/kotlin/org/sample/orphan/Helper.kt": (+ "package org.sample.orphan\n\nclass Helper {\n fun help() {}\n}\n"+ ),+ # Every file under ``*-tests`` is test code; its symbols must not keep+ # the module a package candidate.+ "jwt-auth-tests/build.gradle.kts": "plugins { application }\n",+ "jwt-auth-tests/src/main/kotlin/org/sample/jwt/Main.kt": (+ "package org.sample.jwt\n\n"+ "fun main(args: Array<String>) {}\n\n"+ "fun Application.main() {\n main(1)\n}\n\n"+ "fun Application.main(x: Int) {}\n"+ ),+}+++def _report(root: Path):+ for rel, text in _FILES.items():+ path = root / rel+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(text, encoding="utf-8")+ parser = ASTParser()+ builder = GraphBuilder(repo_path=root)+ source_map: dict[str, bytes] = {}+ for fi in FileTraverser(root).traverse():+ source = Path(fi.abs_path).read_bytes()+ source_map[fi.path] = source+ builder.add_file(parser.parse_file(fi, source))+ builder.set_source_map(source_map)+ graph = builder.build()+ builder.add_framework_edges([])+ analyzer = DeadCodeAnalyzer(graph, git_meta_map={}, source_map=source_map, repo_root=root)+ return analyzer.analyze({"min_confidence": 0.0})+++def _of(report, kind: DeadCodeKind) -> set[str]:+ return {f.symbol_name or Path(f.file_path).name for f in report.findings if f.kind is kind}+++def test_convention_plugin_registered_by_bare_name_is_a_root(tmp_path: Path):+ unreachable = _of(_report(tmp_path), DeadCodeKind.UNREACHABLE_FILE)+ assert "AndroidLibraryConventionPlugin.kt" not in unreachable+ assert "UnregisteredPlugin.kt" in unreachable+++def test_manifest_components_resolve_relative_names(tmp_path: Path):+ report = _report(tmp_path)+ unreachable = _of(report, DeadCodeKind.UNREACHABLE_FILE)+ assert unreachable.isdisjoint({"CatalogActivity.kt", "SyncService.java"})+ assert "CatalogActivity" not in _of(report, DeadCodeKind.UNUSED_EXPORT)+++def test_android_test_module_is_run_not_imported(tmp_path: Path):+ report = _report(tmp_path)+ assert "StartupBenchmark.kt" not in _of(report, DeadCodeKind.UNREACHABLE_FILE)+ assert "StartupBenchmark" not in _of(report, DeadCodeKind.UNUSED_EXPORT)+++def test_zombie_packages(tmp_path: Path):+ zombies = _of(_report(tmp_path), DeadCodeKind.ZOMBIE_PACKAGE)+ assert zombies.isdisjoint({"catalog", "legacy", "benchmarks", "jwt-auth-tests"})+ assert "orphan" in zombiesdiff --git a/tests/unit/ingestion/test_jvm_framework_edges.py b/tests/unit/ingestion/test_jvm_framework_edges.pyindex b5898586d..0e7e692d6 100644--- a/tests/unit/ingestion/test_jvm_framework_edges.py+++ b/tests/unit/ingestion/test_jvm_framework_edges.py@@ -208,6 +208,23 @@ class TestAndroidManifest:graph = _seed_graph(parsed)ctx = _ctx(tmp_path, parsed)add_framework_edges(graph, parsed, ctx)- manifest_rel = "app/src/main/AndroidManifest.xml"activity_rel = "app/src/main/java/com/x/MainActivity.java"- assert graph.has_edge(manifest_rel, activity_rel)+ assert graph.has_edge("framework:android", activity_rel)+ assert graph.nodes[activity_rel].get("is_entry_point") is True++ def test_single_quoted_manifest_completes_relative_names(self, tmp_path: Path) -> None:+ src_dir = tmp_path / "app" / "src" / "main" / "java" / "com" / "x"+ src_dir.mkdir(parents=True)+ (src_dir / "MainActivity.java").write_text("package com.x;\npublic class MainActivity {}\n")+ (tmp_path / "app" / "src" / "main" / "AndroidManifest.xml").write_text(+ "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n"+ " package = 'com.x'>\n"+ " <application>\n"+ " <activity android:name = '.MainActivity' />\n"+ " </application>\n"+ "</manifest>\n"+ )+ parsed = _build_parsed(tmp_path)+ graph = _seed_graph(parsed)+ add_framework_edges(graph, parsed, _ctx(tmp_path, parsed))+ assert graph.has_edge("framework:android", "app/src/main/java/com/x/MainActivity.java")