This repository was archived by the owner on Jul 6, 2026. It is now read-only.
[generator-Tests] Run directly with Roslyn as DotNetCompilerPlatform doesn't work on .NET Core. - #638
Merged
Merged
Conversation
…doesn't work on .NET Core.
jpobst
marked this pull request as ready for review
April 30, 2020 20:04
jonpryor
pushed a commit
that referenced
this pull request
May 6, 2020
How do we test the C# output of `generator`? *A* way is to check the output against "known good" files, which we do. *Another* way is to use a compiler to ensure that the output compiles, which we *also* do. Unfortunately, the "just compile it!" approach has a major flaw, as the easiest accessible C# compiler is `Microsoft.CSharp.CSharpCodeProvider`, for use with System.CodeDom, but *on Windows* `CSharpCodeProvider` only supports up to C# 5. `generator`, meanwhile, may currently produce C# 8 output. This conundrum was addressed in commit 968b474 by using the [Microsoft.CodeDom.Providers.DotNetCompilerPlatform][0] NuGet package when running on Windows, as that supported C#6+. Unfortunately, `DotNetCompilerPlatform` does *not* run under .NET Core, because it uses the MSBuild `CodeTaskFactory` which is not available on .NET Core. ([This issue has been fixed][1]; the fix is unreleased.) Now that we support multitargeting net471 and netcoreapp3.1 (95f698b), we would like to be able to build *and run* our unit tests under .NET Core as well as Mono (macOS) or .NET Framework (Windows). Migrate away from the `DotNetCompilerPlatform` NuGet package and instead use the [Microsoft.CodeAnalysis.CSharp][2] NuGet package, which contains an up-to-date C#8 Roslyn compiler. This new package supports .NET Core; we just need to update `Compiler.Compile()` to work in terms of Roslyn SyntaxTrees instead of CodeDom objects. This allows us to run the `generator` unit tests under .NET Core: dotnet test bin\TestDebug\generator-tests.dll [0]: https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/ [1]: aspnet/RoslynCodeDomProvider#51 [2]: https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
generator-Testscontains a few hacks to get things building on Roslyn back when it was written. It does this with a package from ASP.Net called Microsoft.CodeDom.Providers.DotNetCompilerPlatform.This package uses the MSBuild
CodeTaskFactorywhich is not available on .NET Core. Although it has recently been fixed, there hasn't been a new NuGet package in 1.5 years, so it is unknown when we can expect one.Luckily we're not afraid to get our hands dirty! We can reference the
Microsoft.CodeAnalysis.CSharppackage directly and accomplish the same thing for roughly the same amount of effort.This new version will now build on .NET Core.