🔧 The Generated Code That Was Never Going to Appear
A C# source generator only ever sees the syntax trees of the compilation it’s attached to – it has no visibility into other assemblies’ source code at all, even ones referenced by the project – so a partial class intended to receive generator-produced members has to have ALL of its partial declarations inside the same assembly the generator is actually running against; splitting the class across a shared base project and a consuming project (a common pattern for sharing partial classes across multiple projects) means the generator simply never sees the declaration it’s supposed to be completing, and produces no output and no error for that type at all.
🔎 The Problem
// In SharedModels.csproj:
public partial class Order
{
public int Id { get; set; }
}
// In ApiProject.csproj, which references SharedModels AND has the
// source generator package installed:
[GenerateValidation] // a marker attribute the generator looks for
public partial class Order
{
}
// The generator runs during ApiProject's compilation and can see
// THIS partial declaration and its [GenerateValidation] attribute -
// but it has no access to SharedModels' source at all, since that
// assembly was already compiled into a binary reference by the
// time ApiProject builds. If the generator's logic needs to see
// the Id property to generate anything meaningful against it,
// it silently produces nothing useful, with no error raised.
✅ Fix: Keep Every Partial Piece the Generator Needs in One Assembly
- Move all partial declarations of a type that a source generator needs to read in full into the same project the generator actually runs in – if a class needs to be shared across multiple consuming projects, consider whether the generator should run in each consuming project (referencing the generator as a package, not just the output of running it once) rather than assuming it can see declarations living in a separately-compiled, already-referenced assembly.
- If the design genuinely requires splitting the type across projects, have each project’s source generator package regenerate its own members from only what’s locally visible, and design the generator’s logic to not depend on information that only exists in a different compilation unit’s syntax tree.
- Add a build-time check or generator diagnostic (reporting via SourceProductionContext.ReportDiagnostic) when the generator detects a marker attribute on a partial class but can’t find enough context to generate anything meaningful – turning a silent no-op into a visible build warning makes this mistake immediately obvious instead of only showing up as ‘why isn’t the generated method here’.
🚨 Why This Is Dangerous
- The project builds completely successfully with zero errors and zero warnings – a source generator that finds nothing actionable simply produces no output for that type, which is indistinguishable at build time from a generator that correctly determined there was nothing to generate.
- This specifically breaks when a working single-project prototype gets refactored into a multi-project solution for better separation of concerns – code that worked perfectly during initial development silently stops generating anything the moment the partial class gets split across the new project boundary, with the refactor itself looking completely unrelated to source generation.
A source generator can only complete a partial class from what it can actually see – and once a compilation boundary sits between two pieces of the same partial type, one of those pieces is permanently invisible to any generator running on the other side of it.

