-
Notifications
You must be signed in to change notification settings - Fork 310
Skip [AssemblyFixtureProvider] under Native AOT + add MSTEST0072 analyzer #9941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amaury Levé (Evangelink)
merged 6 commits into
main
from
dev/amauryleve/aot-il2026-assembly-fixture-provider
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae871f3
Skip [AssemblyFixtureProvider] discovery under Native AOT
Evangelink b54f046
Add MSTEST0072: warn [AssemblyFixtureProvider] is unsupported under N…
Evangelink e0db202
Address PR review: broaden MSTEST0072 to referenced assemblies + runt…
Evangelink bbaba88
Address PR review round 2: referenced-provider runtime warning, accep…
Evangelink e8dedb4
Address PR review round 3: harden runtime probe, broaden analyzer, LINQ
Evangelink 1060141
Address PR review round 4: broaden MSTEST0072 message to all AOT modes
Evangelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| ; Unshipped analyzer release | ||
| ; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------|----------|----------|------- | ||
| MSTEST0072 | Usage | Warning | AssemblyFixtureProviderNotSupportedWithNativeAotAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0072) |
97 changes: 97 additions & 0 deletions
97
src/Analyzers/MSTest.Analyzers/AssemblyFixtureProviderNotSupportedWithNativeAotAnalyzer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| using Analyzer.Utilities.Extensions; | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| using MSTest.Analyzers.Helpers; | ||
|
|
||
| namespace MSTest.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// MSTEST0072: <inheritdoc cref="Resources.AssemblyFixtureProviderNotSupportedWithNativeAotTitle"/>. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
| public sealed class AssemblyFixtureProviderNotSupportedWithNativeAotAnalyzer : DiagnosticAnalyzer | ||
|
Evangelink marked this conversation as resolved.
|
||
| { | ||
| private static readonly LocalizableResourceString Title = new(nameof(Resources.AssemblyFixtureProviderNotSupportedWithNativeAotTitle), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.AssemblyFixtureProviderNotSupportedWithNativeAotMessageFormat), Resources.ResourceManager, typeof(Resources)); | ||
| private static readonly LocalizableResourceString Description = new(nameof(Resources.AssemblyFixtureProviderNotSupportedWithNativeAotDescription), Resources.ResourceManager, typeof(Resources)); | ||
|
|
||
| /// <inheritdoc cref="Resources.AssemblyFixtureProviderNotSupportedWithNativeAotTitle" /> | ||
| public static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( | ||
| DiagnosticIds.AssemblyFixtureProviderNotSupportedWithNativeAotRuleId, | ||
| Title, | ||
| MessageFormat, | ||
| Description, | ||
| Category.Usage, | ||
| DiagnosticSeverity.Warning, | ||
| isEnabledByDefault: true); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
| = ImmutableArray.Create(Rule); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
|
|
||
| context.RegisterCompilationAction(AnalyzeCompilation); | ||
| } | ||
|
|
||
| private static void AnalyzeCompilation(CompilationAnalysisContext context) | ||
| { | ||
| // [AssemblyFixtureProvider] discovery walks the runtime assembly reference graph, which is only | ||
| // supported when the runtime can generate dynamic code. Under ahead-of-time compilation the | ||
| // feature is skipped at run time, so warn the user that the attribute has no effect there. | ||
| // Report when the project opts into an AOT flavor detectable at build time: Native AOT | ||
| // (PublishAot) or Blazor WebAssembly AOT (RunAOTCompilation). | ||
| if (!(IsBuildPropertyTrue(context, "build_property.PublishAot") | ||
| || IsBuildPropertyTrue(context, "build_property.RunAOTCompilation"))) | ||
|
Evangelink marked this conversation as resolved.
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| INamedTypeSymbol? assemblyFixtureProviderAttributeSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingAssemblyFixtureProviderAttribute); | ||
| if (assemblyFixtureProviderAttributeSymbol is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Attributes applied in the compilation being built have a source location we can point at. | ||
| foreach (AttributeData attribute in context.Compilation.Assembly.GetAttributes() | ||
| .Where(attribute => SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, assemblyFixtureProviderAttributeSymbol))) | ||
| { | ||
| if (attribute.ApplicationSyntaxReference is null) | ||
| { | ||
| context.ReportNoLocationDiagnostic(Rule); | ||
| } | ||
| else | ||
| { | ||
| context.ReportDiagnostic(attribute.ApplicationSyntaxReference.CreateDiagnostic(Rule, context.CancellationToken)); | ||
| } | ||
| } | ||
|
|
||
| // The documented/default usage places [AssemblyFixtureProvider] on a referenced fixture library. | ||
| // Those attributes are declared outside this compilation (no source location), but the referencing | ||
| // Native AOT test project is exactly where the runtime guard silently skips discovery, so report a | ||
| // no-location diagnostic when any referenced assembly carries the marker. | ||
| if (context.Compilation.SourceModule.ReferencedAssemblySymbols | ||
| .Any(referencedAssembly => referencedAssembly.GetAttributes() | ||
| .Any(attribute => SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, assemblyFixtureProviderAttributeSymbol)))) | ||
| { | ||
| context.ReportNoLocationDiagnostic(Rule); | ||
|
Evangelink marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| private static bool IsBuildPropertyTrue(CompilationAnalysisContext context, string propertyName) | ||
| => context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue(propertyName, out string? value) | ||
| && bool.TryParse(value, out bool parsed) | ||
| && parsed; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.