C
C#12h 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)
);
}
);
}
}
[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>
<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>
10 Replies
math2
math2OP12h ago
and here is the testbench csproj im using to test the generators:
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Reference Include="AnalyzerExperiment" OutputItemType="Analyzer" ReferenceOutputAssembly="false">
<HintPath>..\..\AnalyzerExperiment\AnalyzerExperiment\bin\Debug\netstandard2.0\AnalyzerExperiment.dll</HintPath> <!-- this generates nothing-->
</Reference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\AnalyzerExperiment\AnalyzerExperiment\AnalyzerExperiment.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/> <!-- this however executes the generator-->
</ItemGroup>

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

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

<ItemGroup>
<Reference Include="AnalyzerExperiment" OutputItemType="Analyzer" ReferenceOutputAssembly="false">
<HintPath>..\..\AnalyzerExperiment\AnalyzerExperiment\bin\Debug\netstandard2.0\AnalyzerExperiment.dll</HintPath> <!-- this generates nothing-->
</Reference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\AnalyzerExperiment\AnalyzerExperiment\AnalyzerExperiment.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/> <!-- this however executes the generator-->
</ItemGroup>

</Project>
ive ensured the dll is in the build output and all, im really unsure of whats happening, if this is an actual issue or if im just a little dumb
jcotton42
jcotton4211h ago
ProjectReference is indeed the correct way to do that. I don't think Reference works to load analyzers. Why do you want to avoid ProjectReference anyhow? I'm not sure what you mean by "ship the codeanalysis dll independently"
333fred
333fred11h ago
I don't think Reference works to load analyzers.
Correct, that will not work. You'd need to manually add it to the Analyzers list I also do not understand why you'd be trying to avoid ProjectReference and/or PackageReference
math2
math2OP9h ago
i wanted to put it over on nuget, but i wanted to make sure the dll actually worked first
333fred
333fred9h ago
Then just test with the nuget package, not with the raw dll You can just put a nupkg in a folder, point your nuget.config at that folder, and use a PackageReference
math2
math2OP9h ago
ic wasnt aware you could test nuget packages locally, ty
333fred
333fred9h ago
nuget.config File Reference
NuGet.Config file reference including the config, bindingRedirects, packageRestore, solution, and packageSource sections.
math2
math2OP9h ago
apologies im p new to nuget stuff im testing the nuget package, and the generators all work fine except for one that uses Microsoft.Extensions.Primitives
0>CSC: Warning CS8784 : Generator 'AssetGenerator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'.
0>CSC: Warning CS8784 : Generator 'AssetGenerator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'.
math2
math2OP9h ago
tyvm

Did you find this page helpful?