Closed lskyum closed 1 year ago
@jaredpar Seems like a good issue for @RikkiGibson to look into?
@jcouv can you take a look at this. Could be related to the bug that you just fixed.
I've noticed a similar issue, and reduced it to a minimal repro:
[Generator(LanguageNames.CSharp)]
public class SourceGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var data = context.SyntaxProvider.CreateSyntaxProvider(
static (syntaxNode, _) => syntaxNode.IsKind(SyntaxKind.ClassDeclaration) && ((ClassDeclarationSyntax)syntaxNode).Modifiers.Any(SyntaxKind.PartialKeyword),
static (ctx, _) => ((ClassDeclarationSyntax)ctx.Node).Identifier.ToString()
).Collect();
context.RegisterSourceOutput(data, (ctx, items) =>
{
Console.Beep();
ctx.AddSource("Test.g.cs", string.Join(Environment.NewLine, items.Select(i => $"// {i}")));
});
}
}
Target project:
public partial class Foo
{
}
public partial class Bar
{
}
This generator will beep every time the code is generated, which is very convenient.
Note the predicate
of CreateSyntaxProvider
returns true for all partial class
nodes.
Here's the observed behavior:
partial
keyword on Foo
, the generator will beep every time, as expected.partial
keyword to Bar
, not when you remove it.This looks like an off-by-one error, given it only fails for the last class in the file.
Looks like the filtering of the CreateSyntaxProvider
works fine - no code is generated when the last item is gone (it does not generate an empty file, but no file, so the generator is not involved at all) - but intellisense still seems to refer to the latest generated file, so that seems to be a caching issue.
Version 17.4.0 Preview 1.0:
The issue described here happens when when editing C# source with Visual Studio 2022. The generator seems to work, but it doesn't generate an empty source file, when the syntax provider should find zero syntax nodes. Instead it seems to use some cached syntax nodes.
Version Used: 4.0.1
Steps to Reproduce: Using Visual Studio 2022 (Version 17.1.6)
Expected Behavior: I would expect the file InvokedMethods.g.cs to be empty after deleting all method invocations in the code.
Actual Behavior: Instead InvokedMethods.g.cs contains one or more of the deleted method invocations. Note that when restarting Visual Studio, the generator produces an empty file, so maybe the is a bug in the caching?