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
feat(dead-code): root .NET types MassTransit, NServiceBus, Azure Functions and WCF load (#4562)
Raghav Chamadiya7b1edf04e828
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_discovery.py b/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_discovery.pyindex 74889d952..474e69a2d 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_discovery.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_discovery.py@@ -3,7 +3,9 @@``modelBuilder.ApplyConfigurationsFromAssembly(...)`` makes EF Coreinstantiate every ``IEntityTypeConfiguration<T>`` in the assembly;``AddFastEndpoints()`` does the same for every ``Endpoint<,>`` and-``Validator<T>``, ``AddMediator()`` for every handler. No source names the+``Validator<T>``, ``AddMediator()`` for every handler, MassTransit's+``AddConsumers(...)`` for every consumer and an NServiceBus endpoint for every+message handler. No source names thediscovered class, so the import graph sees nothing and dead code called everyone of them unused.@@ -135,6 +137,46 @@ _DISCOVERIES: tuple[_Discovery, ...] = (_Discovery(frozenset({"IEndpoint"}), ("AddEndpoints",)),# AutoMapper profiles._Discovery(frozenset({"Profile"}), ("AddAutoMapper",)),+ # MassTransit consumers and their definitions. A ``...FromNamespaceContaining``+ # call scans one namespace; it is read as the whole project (ceiling: a+ # consumer outside that namespace is wired too, which can only hide a finding).+ _Discovery(+ frozenset({"IConsumer", "IJobConsumer", "ConsumerDefinition"}),+ ("AddConsumers", "AddConsumersFromNamespaceContaining"),+ ),+ # MassTransit routing-slip activities.+ _Discovery(+ frozenset(+ {"IActivity", "IExecuteActivity", "ActivityDefinition", "ExecuteActivityDefinition"}+ ),+ ("AddActivities", "AddActivitiesFromNamespaceContaining"),+ ),+ # MassTransit sagas and state machines.+ _Discovery(+ frozenset({"MassTransitStateMachine", "SagaDefinition", "ISaga"}),+ (+ "AddSagaStateMachines",+ "AddSagaStateMachinesFromNamespaceContaining",+ "AddSagas",+ "AddSagasFromNamespaceContaining",+ ),+ ),+ # NServiceBus: an endpoint scans every assembly it loads for these, so+ # building the endpoint (or hosting it) is the registration.+ _Discovery(+ frozenset(+ {+ "IHandleMessages",+ "IAmStartedByMessages",+ "IHandleTimeouts",+ "Saga",+ "IHandleSagaNotFound",+ "INeedInitialization",+ "Feature",+ }+ ),+ ("EndpointConfiguration", "UseNServiceBus"),+ ),)_CALL_RE = re.compile(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.pyindex b38800e5d..99dbd257e 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_roots.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/dotnet_roots.py@@ -7,7 +7,10 @@ Four kinds of file have no caller in source and are used all the same:* a marker a source generator reads from the compilation (Vogen's``[EfCoreConverter<T>]`` class is the generator's input);* a class a host loads by reflection: a PowerShell ``[Cmdlet]``, a Roslyn- ``[Generator]``, or a BenchmarkDotNet class with a ``[Benchmark]`` method;+ ``[Generator]``, a BenchmarkDotNet class with a ``[Benchmark]`` method, an+ Azure Functions class with a ``[Function]`` or ``[FunctionName]`` method, or a+ WCF service class implementing a ``[ServiceContract]`` interface (an IIS+ ``.svc`` file or ``web.config`` names it, never a C# source);* a file-based app, started by ``dotnet run <file>``: it opens with ``#:``directives (``#:sdk``, ``#:package``), as Polly's Cake script ``cake.cs`` does.@@ -65,6 +68,23 @@ _REFLECTION_LOADED_ATTRIBUTES: dict[str, frozenset[str]] = {#: BenchmarkDotNet runs every class that has a method carrying one of these._BENCHMARK_METHOD_ATTRIBUTES: dict[str, frozenset[str]] = {_LANGUAGE: frozenset({"Benchmark"})}+#: The Azure Functions host runs every class with a method carrying one of+#: these (``[Function]`` on the isolated worker, ``[FunctionName]`` in-process)+#: when the method binds a trigger. The trigger parameter is what tells it from+#: another library's ``[Function]``, such as linq2db's ``[Sql.Function]``.+_FUNCTION_METHOD_ATTRIBUTES: dict[str, frozenset[str]] = {+ _LANGUAGE: frozenset({"Function", "FunctionName"})+}+# ``[...Trigger]`` alone or after another attribute in one bracket.+_TRIGGER_PARAMETER_RE = re.compile(r"[\[,]\s*(?:[\w.]+\.)?\w+Trigger(?:Attribute)?\b")++#: A WCF host instantiates the class carrying this, or implementing an+#: interface that carries it; a class that also derives from a client base is a+#: proxy, not a service. Ceiling: contracts match by bare name, and a test fake+#: implementing a contract is rooted too, which can only hide a finding.+_SERVICE_CONTRACT_ATTRIBUTE = "ServiceContract"+_WCF_CLIENT_BASES = frozenset({"ClientBase", "DuplexClientBase"})+# 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*<")@@ -107,24 +127,64 @@ def _attribute_name(decorator: str) -> str:return name.removesuffix("Attribute") or name-def _has_benchmark_method(sym: Any, symbols: list[Any]) -> bool:- """Whether a method of *sym* carries ``[Benchmark]``.+def _is_host_run(method: Any) -> bool:+ """Whether a host runs *method*: ``[Benchmark]``, or a function bound to a trigger."""+ names = {_attribute_name(d) for d in method.decorators}+ if names & _BENCHMARK_METHOD_ATTRIBUTES[_LANGUAGE]:+ return True+ return bool(names & _FUNCTION_METHOD_ATTRIBUTES[_LANGUAGE]) and (+ _TRIGGER_PARAMETER_RE.search(method.signature or "") is not None+ )+++def _has_host_run_method(sym: Any, symbols: list[Any]) -> bool:+ """Whether a method of *sym* is one a host runs.``parent_name`` is the bare class name, so the method must also lie withinthe class's lines: two same-named classes in different namespaces of onefile are told apart."""- markers = _BENCHMARK_METHOD_ATTRIBUTES[_LANGUAGE]return any(s.kind == "method"and s.parent_name == sym.nameand sym.start_line <= s.start_line <= sym.end_line- and any(_attribute_name(d) in markers for d in s.decorators)+ and _is_host_run(s)for s in symbols)-def _is_consumed(sym: Any, modules: dict[str, Any], symbols: list[Any]) -> bool:+def _service_contracts(parsed_files: dict[str, Any]) -> frozenset[str]:+ """Names of the interfaces carrying ``[ServiceContract]`` anywhere in the repository."""+ return frozenset(+ s.name+ for parsed in parsed_files.values()+ if parsed.file_info.language == _LANGUAGE+ for s in parsed.symbols+ if s.kind == "interface"+ and any(_attribute_name(d) == _SERVICE_CONTRACT_ATTRIBUTE for d in s.decorators)+ )+++def _wcf_services(parsed: Any, contracts: frozenset[str]) -> set[str]:+ """Service classes of *parsed* and the contracts they implement.++ A contract counts with its implementation, so a file declaring both roots.+ """+ implemented: dict[str, set[str]] = {}+ clients: set[str] = set()+ for relation in getattr(parsed, "heritage", ()):+ parent = bare_type_name(relation.parent_name)+ if parent in contracts:+ implemented.setdefault(relation.child_name, set()).add(parent)+ elif parent in _WCF_CLIENT_BASES:+ clients.add(relation.child_name)+ services = set(implemented) - clients+ return services.union(*(implemented[name] for name in services))+++def _is_consumed(+ sym: Any, modules: dict[str, Any], symbols: list[Any], services: set[str]+) -> bool:namespace = _namespace_of(sym, modules)if f"{namespace}.{sym.name}" in _COMPILER_CONSUMED_TYPES[_LANGUAGE]:return True@@ -134,7 +194,12 @@ def _is_consumed(sym: Any, modules: dict[str, Any], symbols: list[Any]) -> bool:loaded = _REFLECTION_LOADED_ATTRIBUTES[_LANGUAGE]if any(_attribute_name(d) in loaded for d in sym.decorators):return True- return _has_benchmark_method(sym, symbols)+ if sym.name in services or (+ sym.kind == "class"+ and any(_attribute_name(d) == _SERVICE_CONTRACT_ATTRIBUTE for d in sym.decorators)+ ):+ return True+ return _has_host_run_method(sym, symbols)def _is_generator_input(parsed: Any) -> bool:@@ -150,7 +215,7 @@ def _is_file_based_app(parsed: Any) -> bool:return _APP_DIRECTIVE_RE.match(head) is not None-def _is_consumed_file(parsed: Any) -> bool:+def _is_consumed_file(parsed: Any, contracts: frozenset[str]) -> bool:if _is_file_based_app(parsed):return Truemodules = _namespaces(parsed.symbols)@@ -161,15 +226,17 @@ def _is_consumed_file(parsed: Any) -> bool:]if not types:return _is_generator_input(parsed)- return all(_is_consumed(s, modules, parsed.symbols) for s in types)+ services = _wcf_services(parsed, contracts) if contracts else set()+ return all(_is_consumed(s, modules, parsed.symbols, services) 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."""+ contracts = _service_contracts(parsed_files)return [pathfor path, parsed in parsed_files.items()- if parsed.file_info.language == _LANGUAGE and _is_consumed_file(parsed)+ if parsed.file_info.language == _LANGUAGE and _is_consumed_file(parsed, contracts)]diff --git a/tests/unit/ingestion/test_csharp_framework_edges.py b/tests/unit/ingestion/test_csharp_framework_edges.pyindex 9edcfcee1..23de9fdeb 100644--- a/tests/unit/ingestion/test_csharp_framework_edges.py+++ b/tests/unit/ingestion/test_csharp_framework_edges.py@@ -368,6 +368,94 @@ class TestAssemblyScanDiscovery:)assert graph.in_degree("Orders/CreateOrderHandler.cs") == 0+ def test_masstransit_consumer_and_definition_from_add_consumers(+ self, tmp_path: Path+ ) -> None:+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": (+ "builder.Services.AddMassTransit(x =>\n"+ " x.AddConsumersFromNamespaceContaining<SubmitOrderConsumer>());\n"+ ),+ "Consumers/FaultConsumer.cs": (+ "public class FaultConsumer : IConsumer<Fault<FulfillOrder>> {}\n"+ ),+ "Consumers/FaultConsumerDefinition.cs": (+ "public class FaultConsumerDefinition : ConsumerDefinition<FaultConsumer> {}\n"+ ),+ },+ )+ assert graph.has_edge("Program.cs", "Consumers/FaultConsumer.cs")+ assert graph.has_edge("Program.cs", "Consumers/FaultConsumerDefinition.cs")++ def test_masstransit_consumer_needs_a_consumer_scan(self, tmp_path: Path) -> None:+ """``AddSagaStateMachines`` scans state machines, not consumers."""+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": "services.AddMassTransit(x => x.AddSagaStateMachines(asm));\n",+ "Consumers/OrderConsumer.cs": (+ "public class OrderConsumer : IConsumer<OrderSubmitted> {}\n"+ ),+ "Sagas/OrderStateMachine.cs": (+ "public class OrderStateMachine : MassTransitStateMachine<OrderState> {}\n"+ ),+ },+ )+ assert graph.in_degree("Consumers/OrderConsumer.cs") == 0+ assert graph.has_edge("Program.cs", "Sagas/OrderStateMachine.cs")++ def test_nservicebus_handler_from_the_endpoint_configuration(self, tmp_path: Path) -> None:+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": 'var cfg = new EndpointConfiguration("Sales");\n',+ "Handlers/PaymentHandler.cs": (+ "public class PaymentHandler : IHandleMessages<PaymentDone> {}\n"+ "public class DeadSibling {}\n"+ ),+ },+ )+ edge = graph.get_edge_data("Program.cs", "Handlers/PaymentHandler.cs")+ assert edge is not None and edge["imported_names"] == ["PaymentHandler"]++ def test_nservicebus_handler_without_an_endpoint_is_not_wired(self, tmp_path: Path) -> None:+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": "var cfg = CustomizeEndpointConfiguration(settings);\n",+ "Handlers/PaymentHandler.cs": (+ "public class PaymentHandler : IHandleMessages<PaymentDone> {}\n"+ ),+ },+ )+ assert graph.in_degree("Handlers/PaymentHandler.cs") == 0++ def test_nservicebus_saga_from_the_hosted_endpoint(self, tmp_path: Path) -> None:+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": "builder.UseNServiceBus(endpointConfiguration);\n",+ "Sagas/ShippingPolicy.cs": (+ "public class ShippingPolicy : Saga<ShippingPolicyData> {}\n"+ ),+ },+ )+ assert graph.has_edge("Program.cs", "Sagas/ShippingPolicy.cs")++ def test_a_scan_call_in_a_comment_does_not_wire_consumers(self, tmp_path: Path) -> None:+ graph = self._edges(+ tmp_path,+ {+ "Program.cs": "// x.AddConsumers(typeof(Program).Assembly);\n",+ "Consumers/OrderConsumer.cs": (+ "public class OrderConsumer : IConsumer<OrderSubmitted> {}\n"+ ),+ },+ )+ assert graph.in_degree("Consumers/OrderConsumer.cs") == 0+class TestToolDiscoveredRoots:"""A type a tool finds with no call in code: its file is a root, not an edge target."""diff --git a/tests/unit/ingestion/test_dotnet_roots.py b/tests/unit/ingestion/test_dotnet_roots.pyindex e7e4906bb..bd3253c0a 100644--- a/tests/unit/ingestion/test_dotnet_roots.py+++ b/tests/unit/ingestion/test_dotnet_roots.py@@ -145,3 +145,94 @@ def test_benchmark_in_a_same_named_class_of_another_namespace_does_not_count(" public void Sort() { }\n }\n}\n")assert _roots(tmp_path, {"A.cs": text}) == set()+++_ISOLATED_FUNCTION = (+ "using Microsoft.Azure.Functions.Worker;\nnamespace App;\npublic class OrderFn\n{\n"+ ' [Function("GetOrder")]\n'+ ' public object Run([HttpTrigger(AuthorizationLevel.Function, "get")] object req) => null;\n'+ "}\n"+)+++ "text",+ [+ _ISOLATED_FUNCTION,+ "using Microsoft.Azure.WebJobs;\nnamespace App;\npublic static class Cleanup\n{\n"+ ' [FunctionName("Cleanup")]\n'+ ' public static void Run([TimerTrigger("0 */5 * * * *")] object timer) { }\n}\n',+ "namespace App;\npublic class Orchestrator\n{\n [Function(nameof(Run))]\n"+ " public void Run([OrchestrationTrigger] object context) { }\n}\n",+ ],+ ids=["isolated-http", "in-process-timer", "durable-orchestrator"],+)+def test_azure_function_class_is_a_root(tmp_path: Path, text: str) -> None:+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}+++def test_function_attribute_without_a_trigger_is_not_a_root(tmp_path: Path) -> None:+ """linq2db's ``[Sql.Function]`` maps a method to SQL; nothing hosts it."""+ text = (+ "using LinqToDB;\nnamespace App;\npublic static class SqlFns\n{\n"+ ' [Sql.Function("LOWER")]\n public static string Lower(string s) => s;\n}\n'+ )+ assert _roots(tmp_path, {"A.cs": text}) == set()+++def test_azure_function_next_to_an_unused_class_keeps_the_file_judged(tmp_path: Path) -> None:+ text = _ISOLATED_FUNCTION + "public class DeadSibling { }\n"+ assert _roots(tmp_path, {"A.cs": text}) == set()+++_CONTRACT = (+ "using System.ServiceModel;\nnamespace App;\n[ServiceContract]\n"+ "public interface IOrderService { [OperationContract] string Place(string id); }\n"+)+_SERVICE = "public class OrderService : IOrderService { public string Place(string id) => id; }\n"+++def test_wcf_service_implementation_is_a_root(tmp_path: Path) -> None:+ files = {"IOrderService.cs": _CONTRACT, "OrderService.cs": "namespace App;\n" + _SERVICE}+ assert _roots(tmp_path, files) == {"OrderService.cs"}+++def test_wcf_contract_and_service_in_one_file_is_a_root(tmp_path: Path) -> None:+ assert _roots(tmp_path, {"A.cs": _CONTRACT + _SERVICE}) == {"A.cs"}+++def test_wcf_client_proxy_is_not_a_root(tmp_path: Path) -> None:+ proxy = (+ "namespace App;\n"+ "public class OrderServiceClient : ClientBase<IOrderService>, IOrderService\n"+ "{ public string Place(string id) => Channel.Place(id); }\n"+ )+ assert _roots(tmp_path, {"IOrderService.cs": _CONTRACT, "Client.cs": proxy}) == set()+++def test_implementing_an_interface_without_service_contract_is_not_a_root(+ tmp_path: Path,+) -> None:+ files = {+ "IOrderService.cs": (+ "namespace App;\npublic interface IOrderService { string Place(string id); }\n"+ ),+ "OrderService.cs": "namespace App;\n" + _SERVICE,+ }+ assert _roots(tmp_path, files) == set()+++def test_trigger_after_another_attribute_in_one_bracket_counts(tmp_path: Path) -> None:+ text = (+ "namespace App;\npublic class Resize\n{\n [Function(\"Resize\")]\n"+ ' public void Run([Blob("out"), BlobTrigger("in/{name}")] string blob) { }\n}\n'+ )+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}+++def test_class_carrying_service_contract_itself_is_a_root(tmp_path: Path) -> None:+ text = (+ "using System.ServiceModel;\nnamespace App;\n[ServiceContract]\n"+ "public class Echo { [OperationContract] public string Say(string s) => s; }\n"+ )+ assert _roots(tmp_path, {"A.cs": text}) == {"A.cs"}