dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.91k stars 4.01k forks source link

GeneratedRegex Attribute Failed to generate code and shows CS8795 error #65850

Open laolarou726 opened 1 year ago

laolarou726 commented 1 year ago

Version Used:

Steps to Reproduce:

  1. Create a class with GeneratedRegex

private partial class Test{
    [GeneratedRegex("(\\d+\\.?\\d*|\\d*\\.?\\d+)")]
    private static partial Regex DecimalRegex();
}
  1. The error will shows
image image

SourceGenerator does not generate anything.

Diagnostic Id:

CS8795

jasonmalinowski commented 1 year ago

This doesn't reproduce in 17.5.0 Preview 2.0; we'd be hearing pretty loudly if we broke this generally. @laolarou726 any idea if there's something special with your setup, or was that screenshot from a larger project where there might be some extra things impacting it?

laolarou726 commented 1 year ago

This doesn't reproduce in 17.5.0 Preview 2.0; we'd be hearing pretty loudly if we broke this generally. @laolarou726 any idea if there's something special with your setup, or was that screenshot from a larger project where there might be some extra things impacting it?

I think this bug is randomly happening to me. Sometime I need close Visual Studio and open again then the problem will resolve.

jasonmalinowski commented 1 year ago

If you get to the point where you have the squiggle, if you click on the method name and invoke Go to Definition, what happens? The request may seem odd since it seems like it shouldn't work if there's a squiggle at all, but it's possible a few different systems are out of sync here.

laolarou726 commented 1 year ago

If you get to the point where you have the squiggle, if you click on the method name and invoke Go to Definition, what happens? The request may seem odd since it seems like it shouldn't work if there's a squiggle at all, but it's possible a few different systems are out of sync here.

Sometimes it will jump to the right definition. But VS still shows error there.

cheenamalhotra commented 1 year ago

This is happening in VS 2022 as well in .NET 7.0 projects.

Microsoft Visual Studio Enterprise 2022 Version 17.6.0 Preview 2.0 VisualStudio.17.Preview/17.6.0-pre.2.0+33513.286 Microsoft .NET Framework Version 4.8.09032

onionhammer commented 1 year ago

Same thing happening in VS Code with C# dev kit (not omnisharp), using Riok.Mapperly (which also uses source generators)

image

sharwell commented 1 year ago

@onionhammer This issue is also specific to Visual Studio (not VS Code). Can you file a new issue for the behavior you observed?