Error Zone  

When nameof Breaks the Compiler

Recently, while working on a .NET 8 project, I encountered a build failure that didn’t just point to a syntax error — it caused the C# compiler (csc.exe) to crash entirely. If you’ve seen a cryptic "Exit Code -2146232797" in your build logs, this post is for you.

MSB6006 and a Silent Fail

The first sign of trouble was a generic error in Visual Studio:

Error MSB6006: “csc.exe” exited with code -2146232797.

There were no file name and no line number. Looking deeper into the build output revealed a stack trace originating from the Roslyn compiler itself:

System.InvalidOperationException: Unexpected value ‘Unknown’ of type ‘Microsoft.CodeAnalysis.CSharp.AccessorKind’
at Microsoft.CodeAnalysis.CSharp.RefSafetyAnalysis.MethodInfo.Create(…)

nameof() + Indexers

After lots of debugging, I found the line that caused the issue:

throw new ArgumentNullException(nameof(dataSet.Tables[0]));

Why did it crash?

The compiler’s Ref Safety Analysis (the part of the compiler that ensures memory safety for ref and scoped variables) tries to analyze the "safety" of every operation. When it sees an indexer ([0]) inside a nameof() expression, it tries to determine if you are performing a get or a set operation.

Because nameof doesn't actually execute code, the compiler hits an internal state called AccessorKind.Unknown. Instead of reporting a standard error, it triggers a FailFast—essentially a programmed "panic" that shuts down the compiler to prevent it from generating a corrupt assembly.

The Solution

The fix is simple but vital: Keep your nameof expressions simple. nameof is intended to return the name of a symbol, not the result of an operation. When you add an indexer like [0], you are moving from a symbol to an operation.

The “Correct” Way

// References the property symbol directly
throw new ArgumentNullException(nameof(dataSet.Tables), "The first table is missing.");

If you absolutely must have the [0] in your string for logging, use string interpolation instead:

$"{nameof(dataSet.Tables)}[0]"

Conclusion

  • Compilers are software too: Even the most robust tools like Roslyn can have edge-case bugs when new safety features (like Ref Safety) interact with older operators.

  • HRESULT -2146232797: If you see this specific code, it is almost always a System.InvalidOperationException occurring inside the compiler process.

  • Simplicity is safety: Using nameof on complex expressions (indexers, method calls, etc.) can cause build failure. Stick to property and variable names.

Have you ever encountered a “compiler panic”? How did you track it down? Let me know in the comments!

More Articles from my Account