A .NET app references your C# source generator project but sees no generated code; how must the generator be targeted, referenced and inspected?
answer
- compiler's analyzer list, not library refs
- netstandard2.0 plus a private Roslyn package
- warnings before the errors
- obj folder, generated subfolder
basics
~10 sTarget netstandard2.0, reference Microsoft.CodeAnalysis.CSharp privately, and reference the project with OutputItemType="Analyzer" and ReferenceOutputAssembly="false". Then check for CS9057 or CS8785 warnings and set EmitCompilerGeneratedFiles to see the output.
solid answer
~30 sThe compiler runs generators from its analyzer list, so a plain `ProjectReference` never runs it: the build then fails with CS0246 on the marker attribute and CS8795 on unimplemented partial methods. The generator project targets `netstandard2.0`, references `Microsoft.CodeAnalysis.CSharp` with `PrivateAssets="all"`, and needs an `IsExternalInit` polyfill if it uses records (CS0518 otherwise). The app references it with `OutputItemType="Analyzer" ReferenceOutputAssembly="false"`; a package puts the DLL under `analyzers/dotnet/cs`. If it loads but adds nothing, look for warnings: CS9057 means it was built against a newer Roslyn than the running compiler, and CS8784 or CS8785 mean it threw. `EmitCompilerGeneratedFiles` writes the output under `obj/<config>/<tfm>/generated`.
code
xml · 12 lines<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Gen/Gen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>go deeper
Recall that a generator project is referenced as an analyzer, not a library, and that its output can be written to the obj folder for inspection.
Explain each project-file line: netstandard2.0, the private Roslyn reference, OutputItemType and ReferenceOutputAssembly, and EmitCompilerGeneratedFiles.
Diagnose a silent generator from warnings CS8032, CS9057, CS8784 and CS8785 before chasing the errors they cause downstream, and fix the Roslyn version floor.
Set the Roslyn version a shared generator compiles against as a deliberate support policy, because it decides which SDKs your consumers may build with.
## How the compiler finds a generator The C# compiler does not discover generators by scanning referenced libraries. It loads them from its **analyzer** list, the same list that carries Roslyn analyzers. In an SDK-style project that list is fed by: - a `ProjectReference` whose metadata says `OutputItemType="Analyzer"`, for a generator in the same solution; - a NuGet package that places the assembly in its `analyzers/dotnet/cs` folder, which NuGet adds to the consumer's analyzers automatically. A plain `<ProjectReference Include="../Gen/Gen.csproj" />` adds the generator as a **library** reference instead. The compiler never runs it, so nothing is generated, and the build fails in the user's code: the marker attribute is unknown (**CS0246** for `AutoDescribe` and `AutoDescribeAttribute`) and every partial method waiting for a body reports **CS8795**. ## The generator project ```xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> <LangVersion>latest</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" /> </ItemGroup> </Project> ``` - **`netstandard2.0`**: the generator is loaded into the compiler's process, not the app's, and one assembly has to load in every compiler host. The compiler docs' fix for **CS8850** (an analyzer that references .NET Framework) is to retarget to `netstandard2.0`. A side effect: C# `record` and `init` need a polyfill `System.Runtime.CompilerServices.IsExternalInit` class, or the build fails with **CS0518**. - **`Microsoft.CodeAnalysis.CSharp` with `PrivateAssets="all"`**: the generator compiles against the Roslyn API, but the consumer must not inherit that package; at build time the running compiler supplies its own copy. - **`EnforceExtendedAnalyzerRules`**: turns on the analyzer-authoring rules. With it, calling `System.IO.File.Exists` in the generator fails with **RS1035**, "The symbol 'File' is banned for use by analyzers: Do not do file IO in analyzers". ## The consuming project ```xml <ProjectReference Include="../Gen/Gen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> ``` `ReferenceOutputAssembly="false"` keeps the generator assembly out of the app's references and output, since it runs only inside the compiler. For a package, the cookbook's recipe sets `IncludeBuildOutput` to false and packs the DLL under `analyzers/dotnet/cs`; any library the generator itself uses while generating must be packed there too. ## Shipping it as a package The cookbook's packaging recipe makes the generator project produce a NuGet package whose only payload is the analyzer: - `GeneratePackageOnBuild` set to `true` produces the package at build time; - `IncludeBuildOutput` set to `false` keeps the DLL out of `lib/`, so consumers do not reference it as a library; - a `None` item with `Pack="true"` and `PackagePath="analyzers/dotnet/cs"` places the DLL where NuGet registers it as an analyzer for C# projects. Libraries the generator calls *while generating* must be packed into the same `analyzers/dotnet/cs` folder, because the compiler loads them from there; libraries the *generated code* calls at run time are ordinary package dependencies of the consumer. ## When the generator loads but still produces nothing | Diagnostic | Severity | Meaning | |---|---|---| | CS8032 | warning | An instance of the analyzer or generator could not be created from the assembly | | CS9057 | warning | The assembly references a newer compiler version than the one running | | CS8784 | warning | The generator threw during initialization and contributes nothing | | CS8785 | warning | The generator threw while generating and contributes nothing | All four are **warnings**: the build continues without the generator's output, and the first **errors** appear later in user code. So when generated members are missing, read the warnings above the errors first. CS9057 is the classic one after building a generator against a newer `Microsoft.CodeAnalysis.CSharp` than a teammate's SDK ships; the fix is to compile the generator against the oldest Roslyn version you support. ## Looking at what was generated Set `EmitCompilerGeneratedFiles` to `true` in the consuming project. The SDK's `Microsoft.Managed.Core.targets` then sets `CompilerGeneratedFilesOutputPath` to `$(IntermediateOutputPath)/generated` unless you set it yourself, and the compiler writes each file under a folder per generator assembly and generator type, for example `obj/Debug/net10.0/generated/Gen/Gen.AutoDescribeGenerator/Price.Describe.g.cs`. These files are copies for inspection; the compiler uses its in-memory text, and editing them changes nothing. ## A checklist 1. Generator project targets `netstandard2.0`, references Roslyn privately, polyfills `IsExternalInit` if it uses records. 2. Consumer references it with `OutputItemType="Analyzer"` and `ReferenceOutputAssembly="false"`, or through a package with the DLL in `analyzers/dotnet/cs`. 3. Build output has no CS8032, CS9057, CS8784 or CS8785. 4. `EmitCompilerGeneratedFiles` shows the expected files under `obj/.../generated`.
- Your generator builds on your machine, but a teammate's build shows CS9057 and then CS8795 errors. What happened?CS9057 says the generator assembly references a newer compiler version than the one running, so the teammate's older SDK refused to load it and generated nothing; CS8795 follows because the partial methods it should implement have no bodies. Build the generator against the oldest `Microsoft.CodeAnalysis.CSharp` version your users' SDKs ship, or require a newer SDK.
- Why can't the generator project simply target net10.0 like the app?The generator runs inside the compiler's process, not the app's, so its target framework must suit every compiler host that loads it, not the app's runtime. `netstandard2.0` is the target the compiler messages point to; the cost is older APIs and polyfills, such as `IsExternalInit` for records and `init` accessors.
- A normal ProjectReference is enough for the compiler to run the generator.
- The generator should target the same framework as the app it serves.
- Editing files under obj/generated changes what gets compiled.
- CS8785 is an error, so a failing generator always stops the build.
- Consumers need Microsoft.CodeAnalysis.CSharp at run time for generated code.