Description
What's wrong
#61 (fixing #59) changed WriteFileHeader so that consumers below C# 8 no longer get #nullable enable, which made the header compile under C# 7.3, the default for netstandard2.0 and .NET Framework projects. WriteSourceFile uses that header, but then passes the template to codeBlocker.AddSourceFile(sourceFileTemplate) unchanged (SourceGeneratorToolkit/GeneratorBase.cs:249-254). ktsu.CodeBlocker always writes a template's Namespace as a file-scoped namespace and has no block-scoped option. As a result, a template with a namespace still produces a file that fails to compile for any consumer on C# 7.3, 8 or 9.
WriteSourceFile also has no overload that takes a LanguageVersion, unlike WriteFileHeader.
Reproduction
This uses a GeneratorBase<M> generator shaped like the repo's ThingsGenerator fixture:
WriteSourceFile(cb, new SourceFileTemplate
{
Namespace = "Generated",
Usings = { "System" },
Classes = { /* public class A */ },
}, "Copyright");Run through GeneratorHarness { LanguageVersion = LanguageVersion.CSharp7_3 }. The generated file is:
// Copyright
// <auto-generated />
namespace Generated;
using System;
public class A
{
}
The header is now correct for C# 7.3: there is no #nullable enable. Compiling the tree, however, gives:
- C# 7.3:
error CS8370: Feature 'file-scoped namespace' is not available in C# 7.3 - C# 9:
error CS8773: Feature 'file-scoped namespace' is not available in C# 9.0. Please use language version 10.0 or greater. - Default/Latest: compiles
SyntaxTree.GetDiagnostics() returns 0 diagnostics in every case, because this is a compilation error and not a parse error. That is why GeneratorHarnessTests.AHeaderForACSharp73ConsumerParsesWithoutCS8370 cannot catch it. That test also drives PairGenerator, which writes no namespace. (The more general problem that the harness never compiles generated sources is #50.)
Why it matters
The audience #61 was written for, generators that target netstandard2.0/.NET Framework consumers, still gets uncompilable output as soon as it uses WriteSourceFile with a namespace. That is the normal way to use it. The error appears in the consumer's build, at line 4 of a generated file the consumer didn't write.
Suggested fix
-
In
WriteSourceFile, when(consumerLanguageVersion ?? LanguageVersion.Latest).MapSpecifiedToEffectiveVersion() < LanguageVersion.CSharp10andNamespaceis non-empty, write a block-scoped namespace:- Write
namespace Xand open a scope. AddSourceFilea copy of the template withNamespacecleared. Usings inside a namespace block are legal.- Close the scope.
An alternative is to add block-namespace support to ktsu.CodeBlocker and select it here.
- Write
-
Add
WriteSourceFile(CodeBlocker, SourceFileTemplate, string?, LanguageVersion)to matchWriteFileHeader.
Acceptance criteria
- A generator calling
WriteSourceFilewith a namespaced template, run atLanguageVersion.CSharp7_3andCSharp9, produces sources that compile with no errors (checked withCSharpCompilation.Create(...).GetDiagnostics(), not just a parse). - At C# 10 and later the output keeps the file-scoped namespace.
- The C# 7.3 harness test drives a generator that writes a namespace (for example
ThingsGenerator) and asserts that the result compiles.
Activity
matt-edmondson commentedon Nov 10, 2026
Triage
Category: Bug
Priority: High
Area:
GeneratorBase.WriteSourceFile, plus a possible upstream change in ktsu.CodeBlockerAssessment: This is a regression-adjacent gap left by #61, which fixed #59. That fix made the header safe for C# 7.3, but any namespaced template written with
WriteSourceFilestill fails to compile (CS8370/CS8773) for C# 7.3–9 consumers. Those are thenetstandard2.0/.NET Framework audience #61 was meant for, and a namespaced template is the normal way to useWriteSourceFile. The error also shows up in the consumer's build, in a file they didn't write. That is why it's High rather than Medium.Suggested assignment: SourceGeneratorToolkit maintainer. If block-scoped namespace support goes into ktsu.CodeBlocker instead of the local wrap-in-scope fix, it needs a coordinated CodeBlocker change.
Related / duplicates:
#nullable enable#59/Leave #nullable enable out of headers for consumers below C# 8 [minor] #61 (the header fix) and to GeneratorHarness can't tell whether generated sources compile: a duplicate type across two generated files, or a reference to a missing type, comes back with 0 diagnostics #50 (the harness never compiles its output). Fixing GeneratorHarness can't tell whether generated sources compile: a duplicate type across two generated files, or a reference to a missing type, comes back with 0 diagnostics #50 would have caught this.Next steps: Add the
LanguageVersionoverload ofWriteSourceFile, write a block-scoped namespace below C# 10, and switch the C# 7.3 harness test toThingsGeneratorwith a realCSharpCompilationdiagnostics check.Generated by Claude Code
Write a block-scoped namespace for consumers below C# 10 [minor] (kts…