C#C
C#8mo ago
math2

issues with code generator

hi, im trying to make a basic codeanalysis project for a terraria mod im working on
id like to be able to ship the codeanalysis dll independently, rather than directly reference the codeanalysis project in each of my projects but whenever i reference the dll itself, the generators dont execute

i have made a minimum repro project below;

this is the basic generator i made:
[Generator(LanguageNames.CSharp)]
public class TestGenerator : IIncrementalGenerator {
    void IIncrementalGenerator.Initialize(
        IncrementalGeneratorInitializationContext context) {
        context.RegisterSourceOutput(context.AnalyzerConfigOptionsProvider, (x, options) => {
                x.AddSource(
                    "_.g.cs",
                    SourceText.From("/* hello */", Encoding.UTF8)
                );
            }
        );
    }
}

and this is the csproj of the analyzer project:
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>Latest</LangVersion>
        <Nullable>enable</Nullable>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <DevelopmentDependency>true</DevelopmentDependency>
    </PropertyGroup>

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

    <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
    </ItemGroup>

</Project>
Was this page helpful?