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): treat C# compiler polyfills, Vogen converter markers and file-based apps as roots (#2990)
Raghav Chamadiyad05b863de209
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py b/packages/core/src/repowise/core/ingestion/framework_edges/__init__.pyindex 5e657ae33..de57ac192 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/__init__.py@@ -24,6 +24,7 @@ from . import (aws_lambda,django,dotnet_discovery,+ dotnet_roots,express,fastapi,flask,@@ -62,6 +63,7 @@ _HANDLERS: list[FrameworkHandler] = [*flask.HANDLERS,*aspnet.HANDLERS, # ASP.NET edges, then the any-C# extension-method scan*dotnet_discovery.HANDLERS, # types a .NET framework finds by assembly scan+ *dotnet_roots.HANDLERS,*rails.HANDLERS,*laravel.HANDLERS,*spring.HANDLERS,diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_roots.py b/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_roots.pynew file mode 100644index 000000000..0bc3e8091--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_roots.py@@ -0,0 +1,158 @@+"""C# files the compiler or a build tool consumes without a source reference.++Three kinds of file have no caller in source and are used all the same:++* a polyfill the compiler looks up by name (``IsExternalInit`` is what an+ ``init`` accessor or a ``record`` compiles against on an older framework);+* a marker a source generator reads from the compilation (Vogen's+ ``[EfCoreConverter<T>]`` class is the generator's input);+* a file-based app, started by ``dotnet run <file>``: it opens with ``#:``+ directives (``#:sdk``, ``#:package``), as Polly's Cake script ``cake.cs`` does.++The first two mark a file a reachability root when every top-level type in+it is one of these. A file that also declares an ordinary type is left to the+usual checks, so a dead sibling still reports.+"""++from __future__ import annotations++import re+from typing import TYPE_CHECKING, Any++from ..type_names import bare_type_name+from .base import DetectionContext, FrameworkHandler, read_text++if TYPE_CHECKING:+ import networkx as nx++ from ..resolvers import ResolverContext++_LANGUAGE = "csharp"+_TYPE_KINDS = frozenset({"class", "interface", "struct", "enum", "record"})++#: Types the compiler binds to by fully qualified name, so a copy declared in+#: the project is used by every ``init`` accessor, ``required`` member and+#: nullable annotation it compiles.+_COMPILER_CONSUMED_TYPES: dict[str, frozenset[str]] = {+ _LANGUAGE: frozenset(+ {+ "System.Runtime.CompilerServices.IsExternalInit",+ "System.Runtime.CompilerServices.RequiredMemberAttribute",+ "System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute",+ "System.Runtime.CompilerServices.NullableAttribute",+ "System.Runtime.CompilerServices.NullableContextAttribute",+ }+ )+}++#: Attributes that make a class the input of a source generator. Vogen reads+#: ``[EfCoreConverter<T>]`` off a ``partial class`` and emits the converters.+_GENERATOR_MARKER_ATTRIBUTES: dict[str, frozenset[str]] = {+ _LANGUAGE: frozenset({"EfCoreConverter"})+}++# A ``partial class Name;`` has no body, so the parser reports no symbol for+# it and the marker has to be read from the text.+_MARKER_ATTRIBUTE_RE = re.compile(r"\[\s*(?:[\w.]+\.)?(\w+)\s*<")+# Comments and string literals, blanked before the marker is looked for so a+# commented-out or quoted attribute roots nothing. A copy of the blanker in+# ``analysis.dead_code.csharp_reachability`` (that layer sits above this one).+_COMMENT_OR_STRING = re.compile(+ r'@"(?:[^"]|"")*"|"(?:\\.|[^"\\\n])*"|/\*.*?\*/|//[^\n]*', re.DOTALL+)++# The opening of a file-based app: an optional BOM, shebang and ``//`` lines,+# then a ``#:sdk`` / ``#:package`` / ``#:property`` directive.+_APP_DIRECTIVE_RE = re.compile(+ r"\A(?:#![^\n]*\n)?\s*(?://[^\n]*\n\s*)*#:[a-z]+[ \t]", re.IGNORECASE+)+_DIRECTIVE_HEAD_CHARS = 512+++def _namespaces(symbols: list[Any]) -> dict[str, Any]:+ return {s.name: s for s in symbols if s.kind == "module"}+++def _namespace_of(sym: Any, modules: dict[str, Any]) -> str:+ """The namespace *sym* is declared in, nested blocks joined with dots."""+ parts: list[str] = []+ name = sym.parent_name+ while name in modules and name not in parts:+ parts.append(name)+ name = modules[name].parent_name+ if not parts:+ # File-scoped ``namespace A.B;``: the one module with no parent.+ roots = [m.name for m in modules.values() if m.parent_name is None]+ return roots[0] if len(roots) == 1 else ""+ return ".".join(reversed(parts))+++def _is_consumed(sym: Any, modules: dict[str, Any]) -> bool:+ namespace = _namespace_of(sym, modules)+ if f"{namespace}.{sym.name}" in _COMPILER_CONSUMED_TYPES[_LANGUAGE]:+ return True+ markers = _GENERATOR_MARKER_ATTRIBUTES[_LANGUAGE]+ return any(bare_type_name(d) in markers for d in sym.decorators)+++def _is_generator_input(parsed: Any) -> bool:+ """A typeless file carrying a generator marker on a ``;``-bodied class."""+ text = _COMMENT_OR_STRING.sub(" ", read_text(parsed, encoding="utf-8-sig"))+ markers = _GENERATOR_MARKER_ATTRIBUTES[_LANGUAGE]+ return any(m.group(1) in markers for m in _MARKER_ATTRIBUTE_RE.finditer(text))+++def _is_file_based_app(parsed: Any) -> bool:+ """Whether the file opens with ``#:`` directives, so ``dotnet run`` starts it."""+ head = read_text(parsed, encoding="utf-8-sig")[:_DIRECTIVE_HEAD_CHARS]+ return _APP_DIRECTIVE_RE.match(head) is not None+++def _is_consumed_file(parsed: Any) -> bool:+ if _is_file_based_app(parsed):+ return True+ modules = _namespaces(parsed.symbols)+ types = [+ s+ for s in parsed.symbols+ if s.kind in _TYPE_KINDS and (not s.parent_name or s.parent_name in modules)+ ]+ if not types:+ return _is_generator_input(parsed)+ return all(_is_consumed(s, modules) for s in types)+++def consumed_files(parsed_files: dict[str, Any]) -> list[str]:+ """C# files the compiler or a tool uses with no reference in source."""+ return [+ path+ for path, parsed in parsed_files.items()+ if parsed.file_info.language == _LANGUAGE and _is_consumed_file(parsed)+ ]+++def _mark_roots(graph: nx.DiGraph, parsed_files: dict[str, Any], path_set: set[str]) -> None:+ for path in consumed_files(parsed_files):+ node = graph.nodes.get(path)+ if path in path_set and node is not None:+ node["is_reachability_root"] = True+++class _DotNetRootsHandler:+ """Marks files the compiler or a tool uses; adds no edges."""++ def detect(self, dctx: DetectionContext) -> bool:+ return any(p.file_info.language == _LANGUAGE for p in dctx.parsed_files.values())++ def add_edges(+ self,+ graph: nx.DiGraph,+ parsed_files: dict[str, Any],+ ctx: ResolverContext,+ path_set: set[str],+ ) -> int:+ _mark_roots(graph, parsed_files, path_set)+ return 0+++HANDLERS: list[FrameworkHandler] = [_DotNetRootsHandler()]diff --git a/tests/unit/ingestion/test_dotnet_roots.py b/tests/unit/ingestion/test_dotnet_roots.pynew file mode 100644index 000000000..7e8c51b88--- /dev/null+++ b/tests/unit/ingestion/test_dotnet_roots.py@@ -0,0 +1,93 @@+"""C# files the compiler or a build tool consumes are marked reachability roots."""++from __future__ import annotations++from pathlib import Path++import networkx as nx+import pytest++from repowise.core.ingestion.framework_edges import add_framework_edges++from .test_csharp_framework_edges import _build_parsed_files, _ctx++ISEXTERNALINIT_FILE = (+ "namespace System.Runtime.CompilerServices;\ninternal static class IsExternalInit {}\n"+)+ISEXTERNALINIT_BLOCK = (+ "namespace System.Runtime.CompilerServices { internal static class IsExternalInit {} }\n"+)+++def _roots(tmp_path: Path, files: dict[str, str]) -> set[str]:+ for name, text in files.items():+ (tmp_path / name).write_text(text)+ parsed = _build_parsed_files(tmp_path)+ graph = nx.DiGraph()+ graph.add_nodes_from(parsed)+ add_framework_edges(graph, parsed, _ctx(tmp_path, parsed))+ return {p for p, d in graph.nodes(data=True) if d.get("is_reachability_root")}+++ "text",+ [ISEXTERNALINIT_FILE, ISEXTERNALINIT_BLOCK],+ ids=["file-scoped-polyfill", "block-polyfill"],+)+def test_consumed_file_is_a_root(tmp_path: Path, text: str) -> None:+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}+++def test_polyfill_name_in_another_namespace_is_not_a_root(tmp_path: Path) -> None:+ text = "namespace App;\ninternal static class IsExternalInit {}\n"+ assert _roots(tmp_path, {"A.cs": text}) == set()+++def test_sibling_type_keeps_the_file_judged(tmp_path: Path) -> None:+ text = ISEXTERNALINIT_FILE + "public class DeadSibling { }\n"+ assert _roots(tmp_path, {"A.cs": text}) == set()+++ "text",+ [+ "// [EfCoreConverter<X>] partial class A;\nSystem.Console.WriteLine(1);\n",+ 'var s = "[EfCoreConverter<X>]";\n',+ 'var s = @"\n[EfCoreConverter<X>]\npartial class A;\n";\n',+ "/* [EfCoreConverter<X>]\npartial class A; */\nSystem.Console.WriteLine(1);\n",+ ],+ ids=["line-comment", "string", "verbatim-string", "block-comment"],+)+def test_generator_marker_in_a_comment_or_string_is_not_a_root(tmp_path: Path, text: str) -> None:+ assert _roots(tmp_path, {"A.cs": text}) == set()+++def test_generator_marker_on_a_bodiless_partial_class(tmp_path: Path) -> None:+ text = "using Vogen;\nnamespace App;\n[EfCoreConverter<UserId>]\ninternal partial class Conv;\n"+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}+++def test_generator_marker_on_a_braced_partial_class(tmp_path: Path) -> None:+ text = (+ "using Vogen;\nnamespace App;\n[EfCoreConverter<UserId>]\ninternal partial class Conv { }\n"+ )+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}+++def test_file_with_no_marker_and_no_type_is_not_a_root(tmp_path: Path) -> None:+ assert _roots(tmp_path, {"A.cs": "System.Console.WriteLine(1);\n"}) == set()+++def test_file_based_app_with_directives_is_a_root(tmp_path: Path) -> None:+ text = '#:sdk Cake.Sdk\n#:package Newtonsoft.Json\nvar t = Argument<string>("target");\n'+ assert _roots(tmp_path, {"cake.cs": text}) == {"cake.cs"}+++def test_directives_after_a_leading_comment_still_count(tmp_path: Path) -> None:+ text = "// build script\n#:package Spectre.Console\nSystem.Console.WriteLine(1);\n"+ assert _roots(tmp_path, {"run.cs": text}) == {"run.cs"}+++def test_preprocessor_lines_are_not_app_directives(tmp_path: Path) -> None:+ text = "#pragma warning disable CS1591\n#region R\nSystem.Console.WriteLine(1);\n#endregion\n"+ assert _roots(tmp_path, {"A.cs": text}) == set()