C# Source Generators — Silent Failure After .NET 8 Update
After updating to .NET 8, source generators may silently stop producing output.
20+ years shipping production .NET services in enterprise systems. Notes here come from systems that actually shipped.
- Source Generators are Roslyn components that produce C# source files at compile time.
- They run during compilation, not at runtime — zero startup cost, full type safety.
- The modern approach is IIncrementalGenerator — caches results between builds.
- Generated code is compiled into your assembly as if you wrote it manually.
- Biggest mistake: assuming generated code must use string concatenation — use SyntaxGenerator API.
Imagine you're a chef who has to hand-write the same recipe card every single day before you can start cooking. Now imagine hiring an assistant who watches what ingredients you have, then automatically prints all the recipe cards before you even walk into the kitchen. That assistant is a Source Generator — it runs before your app starts, reads your existing code, and generates new C# files so you never have to write the same boilerplate again. The generated code is real, compiled, fully typed C# — not strings, not reflection magic at runtime.
Every non-trivial C# codebase eventually drowns in boilerplate. You've written the same INotifyPropertyChanged implementation fifteen times. You've hand-rolled JSON serialization methods that drift out of sync with your models. You've copy-pasted mapping code between domain objects and DTOs until maintaining it felt like archaeology. The problem isn't laziness — it's that the language used to give you no good alternative between writing it yourself and paying the runtime cost of reflection or dynamic code generation.
Source Generators, introduced in .NET 5 and significantly improved through .NET 6, 7, and 8 with Incremental Generators, solve this at the right layer: compile time. Instead of generating code while your application runs and slowing down startup, or using T4 templates that live outside the build pipeline and break silently, Source Generators are first-class Roslyn components. They receive a full semantic model of your code, produce new C# source files, and those files are compiled into your assembly as if you wrote them yourself. Zero runtime overhead. Full IntelliSense. Full debuggability.
By the end of this article you'll understand how the Roslyn compilation pipeline hands control to your generator, how to build both a basic ISourceGenerator and the modern IIncrementalGenerator, how to handle real-world scenarios like caching, diagnostics, and multi-targeting, and — critically — the production gotchas that will bite you if you skip them. You'll walk away able to write a production-grade generator and explain it confidently in an interview.
What Are C# Source Generators?
Source Generators are components that plug into the Roslyn compiler pipeline. They receive the full compilation object — syntax trees, semantic model, references — and produce additional source files that become part of the same compilation. Unlike code generation tools that run as a separate build step (T4, custom MSBuild tasks), generators run inside the compiler. That means they have access to type information, can read attributes, and can produce code that the compiler type-checks in the same pass.
The magic happens around the CompilationUnit stage: after the compiler parses all files and binds symbols, it invokes registered generators before emitting IL. The generated files are merged into the compilation as if they were written by hand. No post-processing, no second compilation step.
The key distinction: you should never generate code that could be written as a direct method call or a generic. Generators shine when code must be repeated across types that can't share a common base class, or when the pattern depends on metadata unavailable at the generic level.
Building an Incremental Generator Pipeline
An incremental generator consists of a pipeline of steps. Each step takes an input (like a SyntaxNode or SemanticModel) and produces a value. The framework caches these values using equality comparers you provide. When a source file changes, only the affected pipeline branches re-execute.
The pipeline starts in the Initialize method where you register sources of data: CompilationProvider, SyntaxProvider, etc. The SyntaxProvider filters syntax nodes (e.g., all classes with a specific attribute). Then you transform those nodes into your model (like a DTO description), combine with the compilation, and finally register the source output.
Here's a real example that generates a TypeScript-like partial class for INotifyPropertyChanged without using reflection at runtime.
- SyntaxProvider creates an initial stream of candidate nodes.
- Select is lazy — it only runs when outputs need to be produced.
- Each step can be cached by providing a .
WithComparer()implementation. - The final RegisterSourceOutput is the action that emits generated files.
Combine() to gather cross-cutting info.Real-World Patterns: DTO Generation, Auto-Mapping, and Serialization
The most practical use of source generators is eliminating boilerplate that would otherwise require runtime reflection or code duplication. Three patterns dominate production code:
- DTO Generation from database models – Generate DTO classes with the same properties as your EF Core entities, automatically adding JsonPropertyName attributes from column names.
- Auto-Mapping – Generate extension methods that map object A to object B based on matching property names and types. No reflection, no slow runtime map building.
- Serialization helpers – Generate custom JSON serializers for specific types that avoid the overhead of System.Text.Json's reflection fallback. A source generator can produce highly optimised serialization code that outperforms the built-in serialiser by 2-3x for known types.
The pattern in all three is the same: decorate a type or member with an attribute, then the generator reads that attribute and produces the corresponding code. The generated code is a regular C# file that you can inspect and debug.
Production Pitfalls and How to Avoid Them
Source generators are powerful but they come with unique production traps. Here are the ones that bite even experienced teams:
Pitfall 1: The generator runs in a partial compilation context. You cannot call System.IO.File.ReadAllText inside a generator without first registering the file as an additional file in the csproj. The generator host restricts I/O for security and determinism.
Pitfall 2: Error reporting is silent by default. If your generator throws an unhandled exception, the build may succeed with a warning. You must report errors explicitly via context.ReportDiagnostic(Diagnostic.Create(...)).
Pitfall 3: Multi-targeting confusion. If your library targets multiple frameworks, the generator must be conditionally included only for netstandard2.0 (since it runs on the analyzer host). The generated code must also be compatible with the target framework — you can't use C# 12 syntax in code deployed to .NET Framework 4.6.1.
Pitfall 4: Assembly versioning. Generated code lives in the consuming assembly. If you update the generator package, old generated files are not automatically invalidated. Use incremental generators with versioned pipeline inputs to force regeneration when the generator version changes.
When Not to Use Source Generators
Source generators are not the right tool for every problem. They add complexity to the build process and increase assembly size. Avoid them in these scenarios:
- Simple runtime polymorphism – If a regular interface or abstract class solves the problem, don't bring a generator.
- Dynamic code that changes during runtime – Generators are compile-time only. Use Reflection.Emit or System.Linq.Expressions.
- Cross-cutting logging or tracing – Use a runtime AOP framework like Castle DynamicProxy or the .NET interceptors pattern instead of generating wrapper classes for every method.
- Small projects – The setup overhead of a generator project, package dependency, and build caching complexity is not justified for a single DTO mapping.
A good heuristic: if you can solve the problem with a generic type and a few interfaces, do that first. Only reach for a generator when the pattern repeats across unrelated types and the alternative would be runtime reflection or hand-written copy-paste.
How Source Generators Actually Work — The Compiler Pipeline
Stop thinking of Source Generators as magic. They're a compiler plugin. The Roslyn compiler parses your code into a syntax tree, runs semantic analysis, then hands that tree to your generator. Your generator walks the tree, extracts metadata, and spits out new syntax trees — which the compiler merges back in before emitting IL.
That's it. No runtime reflection. No IL weaving at build time. No post-compilation hacks. The generated code is first-class citizen from the start. If your generator throws, the build fails. If your generated code has a syntax error, the compiler tells you before it ever runs.
The critical detail most tutorials skip: generators run in an incremental pipeline. The compiler doesn't re-run your generator on every keystroke. It caches previous results and recomputes only when relevant input changes. That's why you implement IIncrementalGenerator, not the old ISourceGenerator. The old API kills IDE performance. The incremental API is mandatory for production use.
Your generator receives a SourceProductionContext and a pipeline of IncrementalValueProvider or IncrementalValuesProvider. These providers carry the syntax trees, compilation, or custom data you extract. Every generation step must be deterministic and side-effect free. No file I/O. No database calls. If you break that rule, the compiler can't cache your results, and your build time explodes.
DateTime.Now anywhere in the pipeline, the compiler cannot cache results. Your builds will be 10x slower and IDE responsiveness tanks. Keep generation pure.Getting Started — Your First Generator Without the Fluff
Forget the "Hello World" that prints nothing. Here's the real deal: a generator that creates strongly-typed enum extensions. You need three projects: a .NET standard library for the generator, the generator project itself (targeting netstandard2.0 — not negotiable), and a consuming project that references both.
Why netstandard2.0? Because Source Generators run during compilation, not at runtime. The compiler runs on .NET Framework or .NET Core. If you target anything newer, the compiler can't load your assembly. This is the #1 mistake beginners make.
Set up your `.csproj` like this: <Project Sdk="Microsoft.NET.Sdk"> with <TargetFramework>netstandard2.0</TargetFramework>. Add the Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis.Analyzers NuGet packages. That's it. No special analysis-level config. No <IsRoslynComponent> flag — that's an Analyzer thing, not a Generator thing.
Now write your generator class. It must implement IIncrementalGenerator and be decorated with [Generator]. The Initialize method sets up your pipeline. For a first pass, just grab all enum declarations and emit a partial class with a ToDisplayString() method that returns the enum name via reflection — no, that's stupid. Actually generate a switch expression. Compile-time resolution, zero reflection overhead.
Test your generator by adding a reference in your consuming project. Build. If no new files appear, check the error list. The C# compiler will tell you exactly what broke.
// <auto-generated/>. This tells the IDE to suppress warnings and skip analyzers on that file. Without it, you'll get CS1591 warnings on every method you generate.Practical Use Case: INotifyPropertyChanged Without Pain
You've seen the INotifyPropertyChanged pattern. You've cursed the boilerplate. Here's how Source Generators kill it dead.
Problem: you have a ViewModel with 15 properties. Each property needs a backing field, a getter, a setter that calls OnPropertyChanged, and optionally a SetProperty helper. That's 45 lines of repetitive garbage per property. Hand-writing it is error-prone. Using CallerMemberName helps slightly but doesn't eliminate the copy-paste.
Solution: mark your class as partial and slap a custom attribute on it. The generator scans for classes with [AutoNotify], reads each field marked [Notify], and generates the full property-with-change-notification for you. No reflection. No runtime overhead. The generated code is as fast as if you wrote it by hand.
The generator pipeline: filter for classes with the attribute, extract fields with a specific marker attribute (or a naming convention like underscore-prefixed), then emit the property wrappers. Keep the generated code in a separate file to avoid confusion. Your source stays clean: just fields and the attribute.
This pattern scales. Extend it to generate ICommand wrappers for methods. Generate logging decorators. Generate JSON serialization contracts. Every time you catch yourself writing the same three-line pattern, ask: "Can I generate this?" If the answer is yes, you just saved future you from a bug.
Remember: Source Generators aren't magic. They're a compiler plugin that writes code you'd write anyway, but faster and with zero typos.
[Notify] for fields, [AutoCommand] for methods. Makes the intent obvious. Don't hide the generation behind naming conventions (like fields with underscores) — attributes are explicit and compile-time checked.[Notify]. The generator wraps each field into a property with change notification. No runtime cost, zero boilerplate.You Ship Generators Without Tests? Hope You Like Living Dangerously
Source generators produce code at compile time. If your generator has a bug, you don't get a runtime exception. You get a broken .g.cs file that silently corrupts your entire build. No stack trace. No breakpoint. Just confusion.
Testing generators means executing the generator against real compilation objects and asserting the output code compiles and behaves correctly. The standard approach: create a CSharpCompilation with your source files, run the generator via CSharpGeneratorDriver, and inspect the resulting syntax trees. Then compile those trees into an assembly and run unit tests against it.
Your generator should live in a project that references Microsoft.CodeAnalysis.CSharp. Test projects reference both the generator project and the compilation utilities. You don't need a test harness—just a console test fixture that builds, runs, and validates. This catches everything from missing file-scoped namespaces to broken partial method signatures before they reach production.
Debug Generators Without Pulling Your Hair Out
Source generators run inside the compiler process. You can't just hit F5 in your generator project. The debugger attaches to the wrong process and you stare at breakpoints that never fire. That's the default experience—and it sucks.
Use a simple trick: add a static bool field to your generator, say Debugger.Launch(). Guard it with an environment variable check so it only triggers when you're explicitly debugging. Then compile your consuming project in Debug mode with that environment variable set. The debugger attaches directly to the compiler process and hits your breakpoints.
Better yet, output generator diagnostics using the context.ReportDiagnostic() method. This surfaces issues as compiler warnings/errors in VS Error List without attaching a debugger. Combined with incremental caching, you get fast iteration: change source, rebuild, and see diagnostics update immediately. No more black-box guesswork.
Key Takeaways
Source generators change the game by moving runtime work to compile time, but their power comes with strict constraints. You learned that an incremental generator pipeline is essential for performance—relying on caching and equality checks avoids re-running your generator unnecessarily. Real patterns like DTO generation or INotifyPropertyChanged eliminate boilerplate without reflection, yet demand rigorous testing because generated code is invisible until build time. Production pitfalls taught you to handle file paths carefully, avoid global state, and never use \(locals.currentUser. The compiler pipeline revealed that generators execute before emit, meaning they cannot access final assembly metadata. The rule stands: use generators only for repeatable, deterministic code transformations triggered by syntax or attributes. Avoid them for runtime logic, external data, or anything requiring dependency injection. Your generator is a build-time tool, not a runtime library—treat it with the same discipline as hand-written code.
Next Steps
Start by shipping an incremental generator with a real project today—choose a small pain point like automatic constructor generation or partial method stubs. Then, write unit tests using Microsoft.CodeAnalysis.Testing to validate generated output against edge cases: empty inputs, nested types, and multiple files. Next, implement a registration mechanism so your generator can coexist with others without collisions—use unique source name prefixes. For performance, add an IncrementalValueProvider that tracks attribute arguments and syntax tree changes, then profile with the Source Generator Verifier tool. Finally, publish your generator as a NuGet package with a .props file that automatically adds the analyzer reference—this makes adoption seamless. Avoid the trap of over-engineering: start simple, ship early, then expand based on real usage feedback. The best generators feel invisible—they reduce boilerplate so developers focus on business logic, not ceremony.
The Generator That Silently Stopped Running After an SDK Update
- Always specify the generator attribute's language version target to ensure forward compatibility.
- Migrate to IIncrementalGenerator for production generators — it's not optional anymore.
- Add a unit test that compiles the generator and verifies output exists after each SDK version bump.
Debugger.Launch(). Enable source generator debugging via <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> in csproj.WithComparer() on each step. Missing comparers cause full regeneration on every build.ReportDiagnostic() to surface errors. The generator exception is swallowed by the host.dotnet clean && dotnet build -v n -f net8.0Check IntermediateOutputPath for generated files: ls obj/Debug/net8.0/generated/Key takeaways
Debugger.Launch()Common mistakes to avoid
4 patternsUsing ISourceGenerator instead of IIncrementalGenerator
Omitting [Generator(LanguageVersion)] attribute
Accessing the file system inside the generator without registering additional files
Not handling incremental caching correctly (missing comparer)
Interview Questions on This Topic
Explain the difference between ISourceGenerator and IIncrementalGenerator. When would you use each?
Frequently Asked Questions
20+ years shipping production .NET services in enterprise systems. Notes here come from systems that actually shipped.
That's C# Advanced. Mark it forged?
11 min read · try the examples if you haven't