Skip to content

OptionsValidator source-gen causes VS analyzer to crash #88152

Closed

Description

@xakep139

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:
image

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

Activity

  1. ghost added
    needs-area-label
    on Jun 28, 2023
  2. ghost added
    untriaged
    on Jun 28, 2023
  3. xakep139 commented on Jun 28, 2023

    @xakep139
    ContributorAuthor
  4. added and removed
    needs-area-label
    on Jun 28, 2023
  5. added this to the 8.0.0 milestone on Jun 29, 2023
  6. ghost removed
    untriaged
    on Jun 29, 2023
  7. self-assigned this
    on Jun 29, 2023
  8. ghost added
    in-pr
    on Jul 4, 2023
  9. eiriktsarpalis commented on Jul 5, 2023

    @eiriktsarpalis
    Member

    cc @layomia we might want to double check that other source generator aren't impacted by this.

  10. ghost removed
    in-pr
    on Jul 7, 2023
  11. ghost locked as resolved and limited conversation to collaborators on Aug 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions