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): root C# classes with custom MEF export attributes (#3854) (#4009)

Rishabh Mishra11df3f8bcc70
diff --git a/packages/core/src/repowise/core/analysis/dead_code/csharp_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/csharp_reachability.py
index 7298fe2d4..90e9020c6 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/csharp_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/csharp_reachability.py
@@ -564,13 +564,34 @@ def scanned_implementations(
matches nothing: a wider match only widens a rescue.
"""
types = list(_csharp_types(graph))
+ heritage = _heritage_names(graph, parsed_files or {})
+ by_name: dict[str, set[str]] = {}
+ for (_path, name), parents in heritage.items():
+ by_name.setdefault(name, set()).update(parents)
+
+ export_attr_names = set(_EXPORT_ATTRIBUTES)
+ inherited_export_attr_names = set(_INHERITED_EXPORT_ATTRIBUTES)
+ for data in types:
+ name = data.get("name")
+ if not name or data.get("kind") not in ("class", "record"):
+ continue
+ key = _class_key(data)
+ if _reaches_base(key, heritage, by_name, _EXPORT_ATTRIBUTES):
+ export_attr_names.add(name)
+ if name.endswith("Attribute"):
+ export_attr_names.add(name[: -len("Attribute")])
+ if _reaches_base(key, heritage, by_name, _INHERITED_EXPORT_ATTRIBUTES):
+ inherited_export_attr_names.add(name)
+ if name.endswith("Attribute"):
+ inherited_export_attr_names.add(name[: -len("Attribute")])
+
bases = _scanned_type_names(graph, source_map) | {
data.get("name") or ""
for data in types
- if _attribute_bases(data) & _INHERITED_EXPORT_ATTRIBUTES
+ if _attribute_bases(data) & inherited_export_attr_names
}
classes = [data for data in types if _is_concrete_class(data)]
- out = {_class_key(d) for d in classes if _attribute_bases(d) & _EXPORT_ATTRIBUTES}
+ out = {_class_key(d) for d in classes if _attribute_bases(d) & export_attr_names}
if not bases:
return frozenset(out)
heritage = _heritage_names(graph, parsed_files or {})
diff --git a/tests/unit/dead_code/test_csharp_scanned_roots.py b/tests/unit/dead_code/test_csharp_scanned_roots.py
index abab43401..972cd1979 100644
--- a/tests/unit/dead_code/test_csharp_scanned_roots.py
+++ b/tests/unit/dead_code/test_csharp_scanned_roots.py
@@ -60,7 +60,7 @@ _FILES = {
"Providers/TmdbEpisodeProvider.cs": (
"namespace Providers;\nusing Model;\n"
"public class TmdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>\n"
- "{\n public string Name => \"Tmdb\";\n}\n"
+ '{\n public string Name => "Tmdb";\n}\n'
),
"Plugins/Scan.cs": (
"namespace Plugins;\n"
@@ -115,8 +115,12 @@ _FILES = {
}
-def _report(root: Path):
- source_map = {rel: text.encode() for rel, text in _FILES.items()}
+def _report(root: Path, custom_sources: dict[str, bytes] | None = None):
+ source_map = (
+ custom_sources
+ if custom_sources is not None
+ else {rel: text.encode() for rel, text in _FILES.items()}
+ )
for rel, source in source_map.items():
path = root / rel
path.parent.mkdir(parents=True, exist_ok=True)
@@ -185,3 +189,36 @@ def test_scans_over_framework_types_and_assertions_name_nothing() -> None:
b"x.Should().BeAssignableTo<IPlugin>();\n"
)
assert _scanned_type_names(nx.DiGraph(), {"Host.cs": blob}) == set()
+
+
+def test_custom_export_attributes_reach_mef_roots(tmp_path: Path) -> None:
+ sources = {
+ "App.csproj": b'<Project Sdk="Microsoft.NET.Sdk" />\n',
+ "Contracts.cs": (
+ b"public interface IContextMenuEntry { void Execute(); }\n"
+ b"[MetadataAttribute]\n"
+ b"[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]\n"
+ b"public sealed class ExportContextMenuEntryAttribute : ExportAttribute {\n"
+ b' public ExportContextMenuEntryAttribute() : base("ContextMenuEntry", typeof(IContextMenuEntry)) { }\n'
+ b"}\n"
+ b"[AttributeUsage(AttributeTargets.Class)]\n"
+ b"public class ExportSubContextMenuEntryAttribute : ExportContextMenuEntryAttribute { }\n"
+ b"public class PlainAttribute : Attribute { }\n"
+ ),
+ "SaveCodeContextMenuEntry.cs": (
+ b'[ExportContextMenuEntry(Header = "Save code")]\n'
+ b"public sealed class SaveCodeContextMenuEntry : IContextMenuEntry { public void Execute() { } }\n"
+ ),
+ "SubContextMenuEntry.cs": (
+ b"[ExportSubContextMenuEntry]\n"
+ b"public sealed class SubContextMenuEntry : IContextMenuEntry { public void Execute() { } }\n"
+ ),
+ "PlainDecorated.cs": (b"[Plain]\npublic sealed class PlainDecorated { }\n"),
+ "CSharpLanguage.cs": (b"[Export(typeof(Language))]\npublic class CSharpLanguage { }\n"),
+ }
+
+ flagged = _flagged(_report(tmp_path, sources))
+ assert "SaveCodeContextMenuEntry.cs" not in flagged
+ assert "SubContextMenuEntry.cs" not in flagged
+ assert "CSharpLanguage.cs" not in flagged
+ assert "PlainDecorated.cs" in flagged