Skip to content

Replace System.CodeDom with Roslyn Microsoft.CodeAnalysis#5142

Merged
OsirisTerje merged 8 commits intomainfrom
CodeDomToRoslyn
Feb 26, 2026
Merged

Replace System.CodeDom with Roslyn Microsoft.CodeAnalysis#5142
OsirisTerje merged 8 commits intomainfrom
CodeDomToRoslyn

Conversation

@manfred-brands
Copy link
Copy Markdown
Member

Fixes #5131

Most work done by Copilot.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request replaces System.CodeDom with Roslyn's Microsoft.CodeAnalysis for in-memory assembly compilation in tests, enabling cross-platform test execution. Previously, tests using System.CodeDom were restricted to .NET Framework only.

Changes:

  • Introduced new TestCompiler class using Roslyn APIs for cross-platform compilation
  • Removed .NET Framework-specific conditional compilation directives from test files
  • Updated package dependencies to include Microsoft.CodeAnalysis.CSharp 4.12.0
  • Upgraded System.Collections.Immutable to 8.0.0 for compatibility

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/NUnitFramework/tests/nunit.framework.tests.csproj Added Microsoft.CodeAnalysis.CSharp package reference
src/NUnitFramework/tests/TestUtilities/TestCompiler.cs New cross-platform test compiler using Roslyn APIs
src/NUnitFramework/tests/TestUtilities/TestAssemblyHelper.cs Removed old .NET Framework-only helper class
src/NUnitFramework/tests/Syntax/TestCompiler.cs Removed old .NET Framework-only compiler class
src/NUnitFramework/tests/Syntax/InvalidCodeTests.cs Migrated to use new TestCompiler, removed conditional compilation
src/NUnitFramework/tests/Attributes/LifeCycleAttributeTests.cs Migrated to use new TestCompiler, removed conditional compilation for assembly-level tests
src/NUnitFramework/Directory.Packages.props Added Roslyn package versions and upgraded System.Collections.Immutable

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@stevenaw
Copy link
Copy Markdown
Member

@manfred-brands Thanks for looking into this. In the event that you're running into the same reference assembly issue I did, I was able to reproduce it locally outside of any build server by running build -Target=Test from the command line.

@manfred-brands
Copy link
Copy Markdown
Member Author

I just noticed that myself. Maybe something related to how nunitlite runs tests

@OsirisTerje
Copy link
Copy Markdown
Member

OsirisTerje commented Feb 26, 2026

  1. src/NUnitFramework/Directory.Packages.props
  • Removed the framework condition from System.Collections.Immutable version definition (now available for all
    frameworks)
  1. src/NUnitFramework/tests/nunit.framework.tests.csproj
  • Removed the framework condition from System.Collections.Immutable reference (now referenced for all frameworks)
  1. src/NUnitFramework/nunitlite-runner/nunitlite-runner.csproj
  • Added System.Memory for net462 (generates binding redirects)
  • Added Microsoft.CodeAnalysis.CSharp for all frameworks (ensures all Roslyn dependencies are in deps.json for runtime
    resolution)

Root cause: When tests are run via nunitlite-runner, it's the host process and its deps.json/config file is used for
assembly resolution. The test project's dependencies (Roslyn and its transitive dependencies) need to be declared in
nunitlite-runner so:

  • For .NET Framework: binding redirects are generated in the .exe.config
  • For .NET Core: assemblies are listed in deps.json for proper runtime resolution

PS: Needed some help from my friend Claude to find the issues though. :-)

Copy link
Copy Markdown
Member

@stevenaw stevenaw left a comment

Choose a reason for hiding this comment

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

LGTM

@OsirisTerje OsirisTerje merged commit 7344279 into main Feb 26, 2026
5 checks passed
@OsirisTerje OsirisTerje deleted the CodeDomToRoslyn branch February 26, 2026 22:09
@manfred-brands
Copy link
Copy Markdown
Member Author

When tests are run via nunitlite-runner, it's the host process and its deps.json/config file is used for
assembly resolution. The test project's dependencies (Roslyn and its transitive dependencies) need to be declared in
nunitlite-runner

This is a downside that the runner needs to know all the dependencies of the assemblies it is testing.
That makes an nunit-lite nuget package of limited use.

@OsirisTerje
Copy link
Copy Markdown
Member

@manfred-brands Agree. Not sure what we, or if, we should do anything about it.
It is not like the nunit-lite runner is used much around, and MTP also fill in that space.

@stevenaw
Copy link
Copy Markdown
Member

From what I can recall the tests have historically used the console runner though that posed issues with cyclical dependencies, in particular with dependencies outside the framework even though the console runner is still within the Nunit umbrella.

I wonder if this could be a use case for us to lean on Nunit Lite's "self executing test" feature if that shifts the entry point from the runner to the test assembly itself

@OsirisTerje
Copy link
Copy Markdown
Member

Right you are, so it doesn't do that now. I missed that detail. Assumptions......

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.

Migrate in-memory assembly generation in the tests to run on all runtimes

4 participants