Closed dotfede closed 2 years ago
@jasonmalinowski It seems likely that this line would either throw an exception while the new variable declaration is being typed, or fail to look specifically at the value for classes
and start using the wrong variable:
There is certainly a bug with the source generator itself, but it's not clear whether or not recovery is expected in this case.
@chsienki could comment better on recovery from the incremental generator API.
I'd expect that in the face of exceptions we'd either give no output (which maybe is what's happening here when the classes all disappear) or we'd keep reusing stale stuff. But the fact we don't snap back when there isn't an exception again is odd.
It's also possible that c11
ends up before classes
after the call to collect values, which would cause this to always evaluate to 0:
@sharwell that wouldn't explain why the build actually succeeds (given that the code is referencing source-generated symbols such as Class3
, Class5
and so on), but the IDE reports errors and fails to do syntax lightlight.
Though it's possible that my code is buggy, this is simply a toy project to demonstrate the issue. In practice my generator is much more complex, and involves network calls, but this was the smallest, simplest repro I could come up with.
The original source generator exhibits the same behavior: all out of a sudden, syntax highlight and intellisense stop working, Solution explorer reports "this generator is not generating files", and ctrl+clicking on source-generated classes stops working too. However, ctrl+B building the solution reports "build succeeded".
I'm feeling more certain that this issue is a duplicate of #54098. We'll know for sure once we can test to see if an exception is getting thrown during an incremental update.
I'm now 100% certain that the reproducer above is a duplicate of #54098. If you fix the transformation function to no longer throw a NullReferenceException
the issue is completely resolved.
transform: (ctx, _) => ((VariableDeclarationSyntax)ctx.Node).Variables.FirstOrDefault()?.Initializer?.Value.ToString());
I'm not sure it's necessary to include both the call to FirstOrDefault()
and null-check the Initializer
, but I verified that the above does resolve the issue.
Duplicate of #54098
I filed #58625 for the failure to recover from an error.
Version Used:
Visual Studio 2022 17.0.4 Microsoft.CodeAnalysis.Analyzers 3.3.3 Microsoft.CodeAnalysis.CSharp.Workspaces 4.0.1
Steps to Reproduce:
SourceGenerator.IntellisenseBug.Demo.sln
in Visual Studio 2022 17.0.4Expected Behavior:
Analyzers -> SourceGen.IntellisenseBug.Demo
instead of "this generator is not generating any files." message.Actual Behavior:
c11
is declared.c11
is declared, and never recover afterwards.More details:
The source generator creates a single file with several empty C# classes named
Class1
,Class2
,Class3
and so forth, depending on the number of classes declared in the consuming project in the first variable declaration:var classes = 5;
means there will beClass1
toClass5
var classes = 10;
means there will beClass1
toClass10
Using
<ProjectReference/>
and<PackageReference/>
to consume the source generator, in both cases the behavior is the same.