Skip to content

Make ImplicitUsings/Nullable conditional on TFM in test templates - #54594

Merged
Evangelink merged 5 commits into
mainfrom
dev/amauryleve/fix-test-templates-langversion
Jun 8, 2026
Merged

Evangelink merged 5 commits into
mainfrom
dev/amauryleve/fix-test-templates-langversion

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #51969.

Test templates previously emitted <LangVersion>latest</LangVersion>, <ImplicitUsings>enable</ImplicitUsings> and <Nullable>enable</Nullable> unconditionally, even for .NET Framework target frameworks (e.g. net472) where these settings are not officially supported and can produce build errors (e.g. CS8370 about global usings requiring C# 10).

This change follows the pattern already used by ClassLibrary-CSharp / ConsoleApplication-CSharp:

C# test templates (MSTest, Playwright-MSTest, NUnit, XUnit, Playwright-NUnit)

  • langVersion symbol now defaults to "" so no <LangVersion> property is emitted unless the user explicitly passes --langVersion.
  • csharp* / csharpFeature_* computed symbols gate <ImplicitUsings>, <Nullable> and the <Using> items so they only appear when the target language version supports them.
    • For templates that allow .NET Framework TFMs (mstest, mstest-playwright), a netFramework regex symbol (^net4\d) is added and combined with the language gate.
  • Source files (Test1.cs / UnitTest1.cs / MSTestSettings.cs) use template-engine #if blocks to switch between:
    • file-scoped namespaces + implicit usings (modern), and
    • traditional namespace { ... } blocks with explicit using directives (pre-C# 10 / netFramework).

MSTest VB / F# templates

Removed unconditional C#-only properties (<ImplicitUsings>, <Nullable>, <Using> items). These had no effect on the VB/F# compilers and are inconsistent with the existing NUnit-VB/F# and XUnit-VB/F# templates which don't emit them.

VB / F# langVersion

Changed default from "latest" to "" for MSTest-VisualBasic, NUnit-VisualBasic, MSTest-FSharp, NUnit-FSharp.

Localization

The auto-generated templatestrings.*.json files under XUnit-CSharp/.template.config/localize/ were updated by the templates build target to include the new langVersion symbol description (English strings as placeholders — will be properly localized via the standard loc flow).

Verification

Templates package builds clean (0 errors / 0 warnings).

Manually generated and built all 15 applicable combinations:

template net11.0 net472
mstest (C# / VB / F#) ✅ ✅
mstest-playwright (C#) ✅ ✅
nunit (C# / VB / F#) ✅ n/a (only net11.0 supported)
xunit (C# / VB / F#) ✅ n/a
nunit-playwright (C#) ✅ n/a

For net472, generated csproj has none of <LangVersion> / <ImplicitUsings> / <Nullable> / <Using> items, and the source files use traditional namespaces with explicit using directives.

For net11.0, the modern user experience is preserved (<ImplicitUsings>enable</ImplicitUsings>, <Nullable>enable</Nullable>, file-scoped namespaces, no explicit usings).

Fixes #51969. Test templates previously emitted `<LangVersion>latest</LangVersion>`,
`<ImplicitUsings>enable</ImplicitUsings>` and `<Nullable>enable</Nullable>`
unconditionally, even for .NET Framework target frameworks (e.g. net472) where
these settings are not officially supported and can produce build errors.

Follow the pattern used by ClassLibrary-CSharp/ConsoleApplication-CSharp:

* langVersion symbol defaults to """" (no LangVersion property emitted unless
  user explicitly passes --langVersion).
* csharp*/csharpFeature_* computed symbols gate ImplicitUsings, Nullable and
  `<Using>` items so they only appear when the target language version
  actually supports them.
* Source files (Test1.cs/UnitTest1.cs/MSTestSettings.cs) use template-engine
  `#if` blocks to choose between file-scoped namespaces (modern) and
  traditional namespaces with explicit `using` directives (pre-C# 10).
* MSTest VB/F# templates previously had unconditional C#-only properties
  (ImplicitUsings/Nullable/`<Using>`); remove them for consistency with
  NUnit-VB/F# and XUnit-VB/F# templates.

Tested manually: mstest, mstest-playwright, nunit, xunit, nunit-playwright
all generate and build successfully for both net11.0 and net472 (where
applicable).

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 4, 2026 17:33
@Evangelink
Evangelink requested a review from a team as a code owner June 4, 2026 17:33
@github-actions github-actions Bot added the Area-Common templates Covers templates in the repo (classlib, console, common item templates) label Jun 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Evangelink
Evangelink enabled auto-merge June 5, 2026 07:23
@Evangelink

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s), but failed to run 1 pipeline(s).

@Evangelink
Evangelink merged commit 59d45d3 into main Jun 8, 2026
24 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/fix-test-templates-langversion branch June 8, 2026 15:24
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Common templates Covers templates in the repo (classlib, console, common item templates)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revise test templates TFM+LangVersion combinations

3 participants