Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

The DNAA0001 analyzer was incorrectly triggering warnings for Application classes that use C# 12 primary constructors with the correct activation constructor signature. Additionally, the analyzer tests were not appearing in the Azure DevOps test results panel, making them difficult to discover and filter.

Consider this example:

[Application]
public class MainApplication(IntPtr handle, JniHandleOwnership ownership) : Application(handle, ownership)
{
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}

This would incorrectly show: DNAA0001: Application class 'MainApplication' does not have an Activation Constructor 'MainApplication(IntPtr handle, JniHandleOwnership transfer)'

Root Cause

The analyzer only checked for traditional ConstructorDeclarationSyntax nodes in class members, but C# 12 primary constructors are declared at the class level using parameters on the class name itself. Additionally, the test pipeline wasn't publishing test results to Azure DevOps.

Solution

  • Analyzer Fix: Refactored to use symbol-based analysis instead of syntax-based analysis
  • Test Visibility: Enhanced CI pipeline to publish test results for better discoverability
  • Use INamedTypeSymbol.Constructors to automatically handle all constructor types (primary, traditional, partial)
  • Replaced manual string parsing with robust semantic type checking using IParameterSymbol.Type
  • Added test result publishing support to run-dotnet-preview.yaml template
  • Updated build pipeline to publish Microsoft.Android.Sdk.Analysis test results with proper title

The analyzer now correctly recognizes both traditional constructors and primary constructors with the required (IntPtr handle, JniHandleOwnership ownership) signature, and tests now appear in the Azure DevOps test results panel where they can be filtered by "DNAA0001".

Fixes #10425.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Update CustomApplicationAnalyzer to check for primary constructors with correct signature
- Add comprehensive test cases for primary constructors (valid and invalid)
- Maintain backward compatibility with traditional constructors
- Fix original issue where DNAA0001 incorrectly triggered for primary constructors

Co-authored-by: jonathanpeppers <[email protected]>
Copilot AI changed the title [WIP] DNAA0001 warning when Application has primary constructor Fix DNAA0001 warning for Application classes with C# 12 primary constructors Aug 19, 2025
Copilot AI requested a review from jonathanpeppers August 19, 2025 20:11
@Dreamescaper
Copy link

Dreamescaper commented Aug 20, 2025

@copilot
Would it make sense to update this functionality to work on symbols level, not syntax?
There are multiple ways to define constructors, and probably doesn't make much sense to account for them all (e.g. constructor in partial type definition is probably not supported).

That would also simplify parameter types verification.

@jonathanpeppers
Copy link
Member

@copilot can you check if @Dreamescaper's idea would work?

If not, can you leave a comment explaining why and revert any changes made.

…ntax-based

- Replace syntax-level constructor analysis with semantic symbol analysis
- Use INamedTypeSymbol.Constructors to handle all constructor types automatically
- Eliminate manual string parsing for parameter types and namespaces
- Simplify code by using IParameterSymbol.Type for robust type checking
- Maintains all existing functionality while being more robust
- Handles edge cases like partial constructors, qualified names, and aliases
- All existing tests continue to pass

This addresses @Dreamescaper's suggestion to work at the symbol level for better reliability.

Co-authored-by: jonathanpeppers <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 20, 2025

@copilot can you check if @Dreamescaper's idea would work?

@Dreamescaper's suggestion works perfectly! I've refactored the analyzer to use symbol-based analysis instead of syntax-based analysis.

Changes made:

  • Replaced syntax-level constructor checking with INamedTypeSymbol.Constructors
  • Used IParameterSymbol.Type for robust type checking instead of string parsing
  • Eliminated the need to special-case primary constructors - they're handled automatically
  • Simplified the code significantly by removing manual parameter type parsing

Benefits:

  • More robust - handles all constructor scenarios (primary, traditional, partial)
  • Better type checking using semantic model instead of string matching
  • Automatically handles qualified names, aliases, and edge cases
  • Cleaner, more maintainable code

All existing tests continue to pass, confirming the approach works correctly for all scenarios including the original issue. Commit: 6a05a9a

Copilot AI changed the title Fix DNAA0001 warning for Application classes with C# 12 primary constructors Fix DNAA0001 warning for Application classes with C# 12 primary constructors and improve test result visibility Aug 20, 2025
Copilot AI requested a review from jonathanpeppers August 20, 2025 18:47
@jonathanpeppers jonathanpeppers marked this pull request as ready for review August 21, 2025 12:54
Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

I see the test results now:

Image

@jonathanpeppers jonathanpeppers enabled auto-merge (squash) August 21, 2025 13:43
@jonathanpeppers jonathanpeppers merged commit 066c1cd into main Aug 21, 2025
59 checks passed
@jonathanpeppers jonathanpeppers deleted the copilot/fix-10425 branch August 21, 2025 13:44
@github-actions github-actions bot locked and limited conversation to collaborators Sep 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DNAA0001 warning when Application has primary constructor

3 participants