jessejbweld
jessejbweld

Reputation: 113

SourceGenerator Intellisense

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>

Upvotes: 1

Views: 110

Answers (1)

Fei Xu
Fei Xu

Reputation: 476

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.

Upvotes: 0

Related Questions