Skip to content

Leave #nullable enable out of headers for consumers below C# 8 [minor] - #61

Open
matt-edmondson wants to merge 3 commits into
mainfrom
fix/59-nullable-only-for-csharp8
Open

matt-edmondson wants to merge 3 commits into
mainfrom
fix/59-nullable-only-for-csharp8

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #59

Summary

Since #57, WriteFileHeader writes #nullable enable into every generated file. A generator whose hand-written body is C# 7.3-compatible therefore broke a netstandard2.0 or .NET Framework consumer on the default C# 7.3, with CS8370 at line 3 of every generated file.

  • GeneratorBase.Initialize combines ParseOptionsProvider into the pipeline and keeps the consumer's LanguageVersion in a [ThreadStatic] field while Generate runs.
  • WriteFileHeader(CodeBlocker, string?) is still static, so existing callers need no change. It writes the directive only for C# 8 and later. Outside Generate the version is unknown, so it keeps writing the directive as it does today.
  • New overload WriteFileHeader(CodeBlocker, string?, LanguageVersion) takes the version explicitly.
  • GeneratorHarness gains an init-only LanguageVersion property (default LanguageVersion.Default), which is passed to the driver as its parse options. Tests can now run a generator as an older consumer would.
  • README documents the new overload, the version rule and the harness property.

Marked [minor] for the new public overload and harness property. Nothing existing changes its signature.

Tests

In GeneratorHarnessTests:

The existing nullable tests (AnnotatedGeneratedMembersCompileWithoutCS8669, UnannotatedGeneratedMembersAreNotNullOblivious, TheHeaderEnablesNullableAfterTheGeneratedMarker) still pass. Full suite: 70/70. The Release build has 0 warnings. The Sonar analyzers were not run locally.


Generated by Claude Code

WriteFileHeader wrote #nullable enable into every generated file, so a
generator with a C# 7.3-compatible body broke the build of a
netstandard2.0 or .NET Framework consumer on the default C# 7.3 with
CS8370. GeneratorBase now reads the consumer's language version from
ParseOptionsProvider and, while Generate runs, WriteFileHeader writes the
directive only for C# 8 and later. A new overload takes the version
explicitly, and GeneratorHarness gains a LanguageVersion property so the
older-consumer case can be tested.

Fixes #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3i9xsUaiPekqznVhL
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated files stop compiling in C# 7.3 projects (CS8370 at line 3) because WriteFileHeader always emits #nullable enable

2 participants