dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
498 stars 254 forks source link

No Syntax highlight for classes when using Source Generators #881

Open Jejuni opened 2 years ago

Jejuni commented 2 years ago

Problem Description

When referencing a source generator the syntax highlighting of classes randomly stops working:

image (notice both ExampleViewModel and UseAutoNotifyGenerator should be green).

This image is using a stripped down version of the official sample: https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators

Sometimes syntax highlighting works for a second and classes appear green, but they then immediately turn white again. Sometimes, after fresh VisualStudio start without touching anything the classes appear green, but navigating to definition (F12) or switching tabs causes them to turn white again.

GitHub repo showing the problem:

https://github.com/Jejuni/SourceGeneratorsProblem

Repro steps and further description:

namespace GeneratedDemo { class Program { static void Main(string[] args) { var vm = new ExampleViewModel(); UseAutoNotifyGenerator.Run(); } } }

- change `CSharpSourceGeneratorSamples.csproj` to:
```cs
<Project Sdk="Microsoft.NET.Sdk">

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

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
  </ItemGroup>

</Project>


- close the solution
- delete the `.vs`  folder and the `bin` and `obj` folders in both `GeneratedDemo` and `SourceGeneratorSamples` to get a "fresh" environment
- open the `SourceGenerators.sln` again.
- The classes in `Program.cs` should appear green and stay that way. The error list should show errors for `UseAutoNotifyGenerator.cs` saying that the `AutoNotify` attribute doesn't exist. This is expected as per the documentation and will fix itself later.
- Compile the solution. Despite the error list showing errors it should compile successfully.
- Close the solution.
- Open the solution again.
- The errors are gone, but now the classes have no syntax highlighting and appear white. They sometimes appear green for a second and then turn white again. Navigation to definition (F12) and switching files may trigger the problem when the syntax highlighting still works
Jejuni commented 2 years ago

Further information:

The issue seems to be related to using context.RegisterForPostInitialization and / or adding an attribute. Removing everything but the following code still causes the issue:

private const string attributeText = @"
using System;
namespace AutoNotify
{
    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    [System.Diagnostics.Conditional(""AutoNotifyGenerator_DEBUG"")]
    sealed class AutoNotifyAttribute : Attribute
    {
        public AutoNotifyAttribute()
        {
        }
        public string PropertyName { get; set; }
    }
}
";

public void Initialize(GeneratorInitializationContext context)
{
    // Register the attribute source
    context.RegisterForPostInitialization((i) => i.AddSource("AutoNotifyAttribute", attributeText));
}

public void Execute(GeneratorExecutionContext context) {}