Skip to content

JsonSourceGenerator throwing exceptions after edit removing attribute #57455

@jasonmalinowski

Description

@jasonmalinowski

Version Used:

Steps to Reproduce:

  1. Launch VS under a debugger.
  2. Set a breakpoint at
    var errorCode = isInit ? provider.WRN_GeneratorFailedDuringInitialization : provider.WRN_GeneratorFailedDuringGeneration;

[remaining steps are in the debugee]

  1. Create a new .NET 6 console app.
  2. Expand the analyzers node, and observe System.Text.Json.SourceGeneration is not producing any files
  3. Paste in the following contents:
class Person { }

internal partial class MyJsonContext : JsonSerializerContext
{
}
  1. Observe still no generated files.
  2. Add [JsonSerializable(typeof(Person))] before the MyJsonContext class.
  3. Observe there are now generated files.
  4. Delete the attribute
  5. Observe that exceptions are now being thrown from the generator:
SyntaxTree is not part of the compilation
Parameter name: syntaxTreeat Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility) in F:\Projects\Roslyn\src\Compilers\CSharp\Portable\Compilation\CSharpCompilation.cs:line 2256
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonGetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility) in F:\Projects\Roslyn\src\Compilers\CSharp\Portable\Compilation\CSharpCompilation.cs:line 3547
at Microsoft.CodeAnalysis.Compilation.GetSemanticModel(SyntaxTree syntaxTree, Boolean ignoreAccessibility) in F:\Projects\Roslyn\src\Compilers\Core\Portable\Compilation\Compilation.cs:line 211
at System.Text.Json.SourceGeneration.JsonSourceGenerator.Parser.GetGenerationSpec(IEnumerable`1 classDeclarationSyntaxList)
at System.Text.Json.SourceGeneration.JsonSourceGenerator.Execute(Compilation compilation, ImmutableArray`1 contextClasses, SourceProductionContext sourceProductionContext)
at System.Text.Json.SourceGeneration.JsonSourceGenerator.<Initialize>b__4_3(SourceProductionContext spc, ValueTuple`2 source)
at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2) in F:\Projects\Roslyn\src\Compilers\Core\Portable\SourceGeneration\UserFunction.cs:line 66
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

My hunch here is when we are removing the attributes, we're still keeping around the old one for later generation passes which is now stale.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Active/Investigating

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions