-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Description
Description
Recently added OptionsValidator (issue #85475 and PR #87587) under some circumstances emits a diagnostics with location outside of the compilation unit it works on. This causes VS analyzers to crash.
Reproduction Steps
Source code for the first assembly:
using Microsoft.Extensions.Options;
namespace MyAssembly;
[OptionsValidator]
public partial class MyOptionsValidator : IValidateOptions<MyOptions>
{
}
public class MyOptions
{
[ValidateObjectMembers]
public AnotherAssembly.ClassInAnotherAssembly? TransitiveProperty { get; set; }
}Source code for another assembly:
using System.ComponentModel.DataAnnotations;
namespace AnotherAssembly;
public class ClassInAnotherAssembly
{
[Required]
public string? Foo { get; set; }
// !!! line below causes the generator to emit a warning outside of its compilation !!!
public SecondClassInAnotherAssembly? TransitiveProperty { get; set; }
}
public class SecondClassInAnotherAssembly
{
[Required]
public string? Bar { get; set; }
}Expected behavior
No errors happen, a warning is emitted normally:
SYSLIB1212: Type SecondClassInAnotherAssembly has validation annotations, but member TransitiveProperty doesn't specify [ValidateObjectMembers] which could be an oversight.
Actual behavior
Most of the VS analyzer are crashed:

Here's the stacktrace:
System.ArgumentException : SyntaxTree is not part of the compilation
Parameter name: syntaxTree
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree,Boolean ignoreAccessibility)
at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.IsDiagnosticSuppressed(Diagnostic diagnostic,SuppressMessageInfo& info)
at Microsoft.CodeAnalysis.Diagnostics.SuppressMessageAttributeState.ApplySourceSuppressions(Diagnostic diagnostic)
at Microsoft.CodeAnalysis.GeneratorDriver.FilterDiagnostics(Compilation compilation,ImmutableArray`1 generatorDiagnostics,DiagnosticBag driverDiagnostics,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsCore(Compilation compilation,DiagnosticBag diagnosticsBag,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Compilation compilation,CancellationToken cancellationToken)
at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.FinalizeCompilationAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetOrBuildCompilationInfoAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetCompilationSlowAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Document.GetSemanticModelAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.SemanticDocument.CreateAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.IntroduceVariable.AbstractIntroduceVariableService`6.IntroduceVariableAsync[TService,TExpressionSyntax,TTypeSyntax,TTypeDeclarationSyntax,TQueryExpressionSyntax,TNameSyntax](<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.IntroduceVariable.IntroduceVariableCodeRefactoringProvider.ComputeRefactoringsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.CodeRefactoringService.GetRefactoringFromProviderAsync(<Unknown Parameters>)
Regression?
No response
Known Workarounds
No response
Configuration
.NET SDK version 8.0.100-preview.7.23327.3
Other information
No response