Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Source Generator Never Firing on an Incremental Build

- 19.11.26 - ErcanOPAK

🔧 Fix a Source Generator Never Firing on an Incremental Build

An incremental source generator builds a pipeline of value providers, and the whole point of the incremental model is that a stage only re-runs when its inputs actually change, judged by equality comparison on the values flowing through it. A pipeline stage that produces a custom record as its output without that type having correct, value-based equality — for example, one holding an array without overriding equality to compare elements, since arrays compare by reference by default — will either appear to have “changed” on every single build when nothing relevant did, or the more confusing opposite: appear unchanged across builds that compare equal by reference but shouldn’t, silently leaving stale generated code in place on an otherwise successful incremental build.

🔎 The Problem

// Wrong: holds an array; two instances with identical contents are NOT
// equal, so the incremental cache thinks every build's output "changed."
readonly record struct ModelInfo(string Name, string[] Properties);

// Fix: implement value-based equality explicitly instead of relying on
// the record's compiler-generated default.
readonly record struct ModelInfo(string Name, ImmutableArray<string> Properties)
{
    public bool Equals(ModelInfo other) =>
        Name == other.Name && Properties.SequenceEqual(other.Properties);
    public override int GetHashCode() =>
        HashCode.Combine(Name, Properties.Length);
}

✅ Fix: Give Your Pipeline Types Real Equality

  • Audit every custom type that flows through an incremental pipeline stage for value-based equality — a plain record with an array or list member looks like it has value equality but doesn’t, because the compiler-generated `Equals` compares those members by reference.
  • Prefer `ImmutableArray<T>` with an explicit `SequenceEqual`-based equality override — since even `ImmutableArray<T>`’s own default equality is reference-based — over a plain array or list for anything carried through the pipeline.
  • Add a test that runs the generator twice against two separate but logically identical compilations and asserts the same cached pipeline output is reused — this is the one test that catches a broken-equality regression, since a single-build functional test can’t distinguish “ran once and worked” from “will correctly skip re-running when nothing changed.”

🚨 Why This Is Dangerous

  • A source generator with broken incremental equality doesn’t fail a build and doesn’t produce wrong code on a full rebuild — it specifically produces stale generated code on an incremental build after a real source change, so the bug reproduces inconsistently depending on exactly how someone built the project.

An incremental pipeline only skips work it can prove is unnecessary – and a broken Equals is exactly what convinces it something unnecessary is.

— C# Best Practice

Related posts:

C#: Why a Try/Finally Block Doesn't Run When the Process Is Killed

C#: Why a Try/Finally Block Doesn't Run When the Process Is Killed

C#: Use Extension Methods to Add Functionality to Existing Types

C#: Use Extension Methods to Add Functionality to Existing Types

C#: Fix ConfigureAwait(false) Silently Breaking a Method That Needed the Original SynchronizationCon...

C#: Fix ConfigureAwait(false) Silently Breaking a Method That Needed the Original SynchronizationCon...

Views: 2

Post navigation

Photoshop: Fix the Gradient Tool Banding Across a Dark Sky
C#: Fix a Static Constructor Deadlocking on Its Own Type

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com