Skip to content

Conversation

@jpobst
Copy link
Contributor

@jpobst jpobst commented Apr 30, 2020

generator-Tests contains 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 CodeTaskFactory which 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.CSharp package directly and accomplish the same thing for roughly the same amount of effort.

This new version will now build on .NET Core.

@jpobst jpobst marked this pull request as ready for review April 30, 2020 20:04
@jpobst jpobst requested a review from jonpryor April 30, 2020 20:05
@jonpryor jonpryor merged commit cbb50af into master Apr 30, 2020
@jonpryor jonpryor deleted the test-compiler branch April 30, 2020 21:39
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/
@jpobst jpobst added this to the 10.4 (16.7 / 8.7) milestone May 11, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants