Skip to content

fix: do not inspect compiler extensions without a public parameterless constructor#17

Open
Flash0ver wants to merge 1 commit intochore/code-demo-projects-commonfrom
fix/nuget-compiler-extensions-without-public-parameterless-constructor
Open

fix: do not inspect compiler extensions without a public parameterless constructor#17
Flash0ver wants to merge 1 commit intochore/code-demo-projects-commonfrom
fix/nuget-compiler-extensions-without-public-parameterless-constructor

Conversation

@Flash0ver
Copy link
Member

Summary

  • Fixes an Exception when a Compiler Extension is an abstract class
  • Fixes an Exception when a Compiler Extension does not define a public constructor
  • Fixes an Exception when a Compiler Extension does not define a parameterless constructor
  • Fixes incorrect behavior that Compiler Extension are found when the respective [Attribute] is only inherited

Remarks

@Flash0ver Flash0ver added this to the 0.1.0 milestone Feb 27, 2026
@Flash0ver Flash0ver self-assigned this Feb 27, 2026
@Flash0ver Flash0ver added changelog:fixed Something isn't working (SemVer:PATCH) type:fix Fix/patch a bug (this correlates with PATCH in Semantic Versioning) scope:nuget The Inspector inspects NuGet packages labels Feb 27, 2026
@Flash0ver Flash0ver marked this pull request as ready for review February 27, 2026 16:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes several issues with compiler extension inspection in the Roslyn component, specifically addressing exceptions that occurred when inspecting abstract classes, non-public types, or types without public parameterless constructors. It also corrects the behavior where inherited attributes were incorrectly detected.

Changes:

  • Added validation to skip abstract classes and types without public parameterless constructors before instantiation
  • Changed from GetExportedTypes() to GetTypes() to inspect internal and private compiler extensions
  • Modified GetCustomAttribute calls to use inherit: false to prevent false detection of inherited attributes
  • Added comprehensive demo classes and tests to verify edge cases

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
source/lib/FlashOWare.CodeAnalysis.Inspection.Core/Components/RoslynComponent.cs Core logic changes: added IsNotConstructible validation, switched to GetTypes(), fixed attribute inheritance
source/lib/FlashOWare.CodeAnalysis.Inspection.Core/FlashOWare.CodeAnalysis.Inspection.Core.csproj Updated package references to include CSharp and VisualBasic packages
source/tests/FlashOWare.CodeAnalysis.Inspection.Core.Tests/Components/RoslynComponentTests.cs Updated test expectations to include internal and private compiler extensions
source/tests/FlashOWare.CodeAnalysis.Inspection.Core.Tests/Assertions/CompilerExtensionAssertions.cs Added new AssertExtension method to validate extensions by type name
source/tests/FlashOWare.CodeAnalysis.Inspection.Core.Tests/Assertions/ClassInfoAssertions.cs Added overload to compare TypeName with ClassInfo
source/demo/FlashOWare.CodeAnalysis.Demo/Generators/CommonIncrementalGeneratorDemos.cs Added demo classes for edge case testing (abstract, internal, private, missing constructors, etc.)
source/demo/FlashOWare.CodeAnalysis.Demo/Generators/CommonSourceGeneratorDemos.cs Added demo classes for edge case testing
source/demo/FlashOWare.CodeAnalysis.Demo/Diagnostics/CommonDiagnosticAnalyzerDemos.cs Added demo classes for edge case testing
source/demo/FlashOWare.CodeAnalysis.Demo/Diagnostics/CommonDiagnosticSuppressorDemos.cs Added demo classes for edge case testing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +33 to +36
Type type = extension.GetType();
Assert.IsFalse(type.IsAbstract);
Assert.IsTrue(type.IsSealed);

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertions at lines 34-35 check properties of the CompilerExtension wrapper type (GeneratorInfo, AnalyzerInfo, SuppressorInfo) rather than the inspected compiler extension type. Since all wrapper types are sealed by design, these assertions will always pass and don't provide meaningful validation of the inspected types. Consider removing these assertions or changing them to validate properties of extension.Class (the inspected type) if that's the intent.

Suggested change
Type type = extension.GetType();
Assert.IsFalse(type.IsAbstract);
Assert.IsTrue(type.IsSealed);

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by design

private static ImmutableArray<CompilerExtension> Inspect(Assembly component)
{
Type[] types = component.GetExportedTypes();
Type[] types = component.GetTypes();
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from GetExportedTypes() to GetTypes() could throw ReflectionTypeLoadException if some types in the assembly cannot be loaded. Consider wrapping this call in a try-catch block or using GetTypes() with exception handling to gracefully handle assemblies with types that fail to load. Alternatively, use Assembly.GetTypes() with a try-catch that accesses the LoaderExceptions property to log or handle specific failures.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:fixed Something isn't working (SemVer:PATCH) scope:nuget The Inspector inspects NuGet packages type:fix Fix/patch a bug (this correlates with PATCH in Semantic Versioning)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants