1

I'm currently trying to learn about source generators, and have implemented a very simple one.

[Generator]
public class KeysSourceGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        var code = """
        namespace MyLibrary;

        public partial class Keys
        {
            public static string Product1 = "1";
            public static string Product2 = "2";
            public static string Product3 = "3";
        }
        """;

        context.AddSource("GeneratedKeys.g.cs", code);
    }

    public void Initialize(GeneratorInitializationContext context)
    {

    }
}

where I can use the generated keys in another project like this :

 internal class Program
 {
     static void Main(string[] args)
     {
         string product1Key = MyLibrary.Keys.Product1;
         string product2Key = MyLibrary.Keys.Product2;
         string product3Key = MyLibrary.Keys.Product3;
     }
 }

However, the issue I am having, is that if I add a new key in the KeysSourceGenerator.Execute method e.g Product4 key, and then also use the new key in the linked project.

Whilst it builds and works. Intelli-sense still only shows the first 3 keys. I have to restart visual studio for it to pick up the new 4th key.

And this happens every time I want to add a new key. I have to restart VS.

Am I doing something wrong here?

I've copied the proj files below for reference

KeysGenerator (generator project)

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

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>

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


</Project>

The project referencing KeysGenerator

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\KeysGenerator\KeysGenerator.csproj" OutputItemType="Analyzer"/>
    </ItemGroup>

</Project>
4
  • 1
    This behavior is mentioned here: You might need to restart Visual Studio to see IntelliSense and get rid of errors as the tooling experience is actively being improved. Commented Sep 9, 2024 at 4:23
  • It won't solve this problem but note that if you search around the Roslyn github you'll see that ISourceGenerator has already been deprecated and they recommend building incremental generators instead. It's beyond irksome that this isn't mentioned anywhere on the learn.microsoft.com site. Commented Sep 9, 2024 at 7:21
  • See e.g. Source Generators Cookbook Commented Sep 9, 2024 at 7:29
  • IME JetBrains Rider doesn't suffer from this restriction and will update without having to be restarted. Commented Sep 9, 2024 at 9:05

1 Answer 1

0

As the document provided by shingo says,

Note

You might need to restart Visual Studio to see IntelliSense and get rid of errors as the tooling experience is actively being improved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.