What is the purpose of Microsoft.Net.Compilers?
Asked Answered
F

4

95

What is the importance of this compiler? Is it a must have or could do without? What is the purpose of having another compiler anyway, or is it just a futuristic project? A brief overview would be appreciated.

Fecund answered 30/12, 2015 at 16:49 Comment(2)
It's also a dependency of Microsoft.CodeDom.Providers.DotNetCompilerPlatform in v1.0.8 and below so you might be seeing it for that reasonLamontlamontagne
According to Microsoft, Microsoft.Net.Compilers is deprecated in favor of Microsoft.Net.Compilers.Toolset.Gigahertz
E
100

The point of the Microsoft.Net.Compilers package is that the compilers distributed with that package will be used for compiling your project, rather than the compiler that comes with .NET Framework or with Visual Studio. The most practical direct benefit is that it allows your projects to use C# 6 features, and then allows those projects to be built on a system that doesn't have a C# 6 compiler installed, for instance on a continuous integration server where you don't want to install the full Visual Studio 2015.

Encircle answered 31/12, 2015 at 15:43 Comment(3)
I need to do a very simple thing, which is to compile any .cs file and emit il, No need to validate external references etc. Is it possible with Microsoft.Net.Compilers?Stationer
@Stationer It's not possible to compile C# without external references, with or without Microsoft.Net.Compilers. Referenced assemblies are needed for overload resolution, for instance. And if you do have the external references, then it's technically possible with Microsoft.Net.Compilers, but it's really the wrong tool for the job. You'd be launching an external process when the same compiler is already available in other packages in a way that you can call in your own program directly.Encircle
See the updated answer from @JaredPar (MS), this answer is no longer valid. Guess we should check our solutions and remove the old packages in favour of building with an up-to-date VS/VSCode/SDK toolset.Hyatt
A
40

At present there is no purpose to Microsoft.Net.Compilers. This is a NuGet package that is deprecated and will stop being produced after Visual Studio 16.5. There is a successor package named Microsoft.Net.Compilers.Toolset. This package has much the same functionality as Microsoft.Net.Compilers but works with both .NET Desktop and .NET Core MSBuild instances.

Even so Microsoft.Net.Compilers.Toolset is not meant for general consumption. This package serves two specific uses:

  1. Acts as a short term vehicle for unblocking customers that hit crashing bugs in the compiler. For such customers we can use this package to unblock their scenarios just an hour or so after we merge the fix into the Roslyn repository. This serves as a bridge until the fix makes it into the associated Visual Studio or .NET SDK servicing release. When that happens the customer is asked to remove the package from their solution and rely on the officially released toolsets.
  2. Serves as a mechanism to move binaries between Roslyn and the official builds of the .NET SDk.

This package is not meant for general long term consumption by customers for their build. I understand that some customers choose to do this but such use is also not supported. The package can and will regularly cause breaks by taking dependencies on new versions of MSBuild or tasks / targets in the .NET SDK.

Customers who want to use new versions of the compiler for their builds are instead encouraged to use one of the official distributions of the compiler:

  1. Use the Visual Studio Build Tools SKU.
  2. Use the .NET SDK

Note: the Microsoft.Net.Compilers package was officially supported as a part of the Microsoft.CodeDom.Providers.DotNetCompilerPlatform package several years ago as it was a dependency. Beginning in version 2.0 though the dependency on Microsoft.Net.Compilers was removed and it now stands as a completely independent package. At the same time we've moved to deprecate Microsoft.Net.Compilers as there are no longer any explicit use cases for it.

This does mean that some customers unexpectedly find themselves with Microsoft.Net.Compilers in their project file because it wasn't removed when Microsoft.CodeDom.Providers.DotNetCompilerPlatform dropped the dependency. The advice for such customers is to simply delete the reference to the package. It is not needed anymore.

Aluminiferous answered 10/3, 2020 at 18:34 Comment(3)
Very interesting insight. I am wondering why the aspect about short-term usage only is not mentioned in the description of the NuGet package: nuget.org/packages/Microsoft.Net.CompilersCirrus
@JackMiller That's mentioned in Microsoft.Net.Compilers.Toolset. ` This package is primarily intended as a method for rapidly shipping hotfixes to customers. Using it as a long term solution for providing newer compilers on older MSBuild installations is explicitly not supported. That can and will break on a regular basis.`Beer
What is the purpose of Microsoft.Net.Compilers vs Microsoft.CodeDom.Providers.DotNetCompilerPlatform? Both packages appear to contain the same compilers! Or is this happening because I'm looking at the newest version of DotNetCompilerPlatform?Absorbefacient
A
6

It's a package that provides open-source C# and Visual Basic compilers with rich code analysis APIs.

You can find extensive documentation on github:

https://github.com/dotnet/roslyn

Amos answered 30/12, 2015 at 16:55 Comment(2)
Thanks. I have been to the official documentation before but still find the purpose of the project vague.Fecund
@Fecund Basically it is a result of the open source movement from Microsoft, check wikipedia for example for additional info: en.wikipedia.org/wiki/.NET_Compiler_PlatformAmos
C
1

As others have said, it contains the .NET compilers for C# and VB.NET.

An interesting aspect of this being a package is that you can specify a specific build of the compiler to for your project, including a version that hasn't been shipped with Visual Studio yet.

We use this in https://github.com/dotnet/project-system which is an open source component of Visual Studio. It allows us to use pre-release versions of the compiler to dogfood language features not supported by the compiler that ships with VS.

Churr answered 9/8, 2019 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.