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.96k stars 4.03k forks source link

[Source Generator] Syntax highlighting partially broken by SyntaxReceiver #54768

Closed Veilenus closed 2 years ago

Veilenus commented 3 years ago

I wanted to get into Roslyn Source Generators and had some fun with the SourceGeneratorTemplate, just to get started. After a short while I noticed a problem I was not able to fix: Visual Studio's syntax highlighting seems partially broken by the generator, particularly by registering an ISyntaxReceiver (or ISyntaxContextReceiver which produces the same bug). Removing the SyntaxReceiver brings back the expected syntax highlighting, but renders my generator useless.

My demo generator's purpose is to generate a class (GeneratedClass) with a single method that returns the number of members of an inspected class (UserClass). The original "SourceGeneratorTemplate" has been stripped down in order to provide a minimal reproducible example.

I'm not sure whether this is a bug within Roslyn, Visual Studio or something else entirely. Please excuse me if this issue doesn't belong here. This is just my best guess.

Version Used:

Steps to Reproduce:

  1. Download and build the following solution: SyntaxHighlightingBug.zip.
  2. Restart Visual Studio to wire up the source generator (as is documented).
  3. Add an arbitrary member to UserClass.
  4. Open Program.cs and modify code. Type in some syntax errors and remove them. Keep an eye on the syntax highlighting.

Expected Behavior:

Actual Behavior:

huoyaoyuan commented 3 years ago

I'm also observing this after adding SG to my project, and did not test the detailed repro condition.

sharwell commented 3 years ago

@Veilenus I see that SourceGenerator.csproj has the following:

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.10.0" />

This isn't the right package reference for a source generator. Can you try changing it to the following and let us know if the problem still occurs?

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
Veilenus commented 3 years ago

Hi Sam, thank you very much for your assistance, it's much appreciated! I've replaced the wrong package reference with the one suggested and the source generator's project file now looks as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
  </ItemGroup>

</Project>

I did also clean and rebuild the solution and restart Visual Studio several times after changing the package references. Unfortunately, the problem still occurs. The observed behavior is pretty much the same as depicted in the gif above.

Update

After some more testing, I've found that syntax highlighting does indeed work as expected as long as the inspected class UserClass is not changed. Changing it and, in turn, recreating the GeneratedClass breaks the syntax highlighting again. Once changed, even rebuilding and / or restarting VS doesn't seem to fix the problem. I did, however, fix it temporarily by using git clean -xfd on my local repository and then building the solution from scratch and restarting VS (again: this "fix" only lasts until UserClass is changed). As a side note: It feels like reproducing the bug is somewhat harder since I changed the NuGet package, but it is still there.

Maybe one initial change to the inspected class is essential to reproducing the bug. I will add it to the reproduction steps.

Veilenus commented 2 years ago

Update: Upgrading to Visual Studio Enterprise 2022 17.0.0 seems to have solved the problem for me.