Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using dotnet and MsBuild for building .NET applications?

I've just had to do some builds without using Visual Studio for the first time, and clearly there is a gap in my knowledge regarding MsBuild and the build process.

So, what are the differences between the two build processes below?


Option 1:

dotnet build C:\Dev\trunk\Mvc.sln

This option uses "Build Engine version 16.8.3+39993bd9d for .NET" - I presume this means this way can be used for .NET Core as it has no reference to "Framework"?


Option 2:

msbuild C:\Dev\trunk\Mvc.sln

This option uses "Build Engine version 16.8.2+25e4d540b for .NET Framework".


My assumption was that the "dotnet build" command was just a shorthand way of using MsBuild. However, the logging provided by both is pretty different and they both produce different results.

like image 781
Murphybro2 Avatar asked Feb 24 '21 11:02

Murphybro2


1 Answers

Roslyn -- the C# compiler platform -- is a .NET Standard 2.0 library, meaning that it can run on both .NET Framework 4.6.1+ and .NET Core 2.0+(1).

Visual Studio, which includes MSBuild, runs on .NET Framework. When you build a project using Visual Studio (or directly using MSBuild), it runs Roslyn on .NET Framework. Visual Studio knows how to process both SDK-style csprojs and the legacy non-SDK-style csprojs, and invoke Roslyn accordingly. The version of Roslyn which is used is tied to the Visual Studio version.

dotnet build is a separate tool, and is a .NET Core application. It knows how to build SDK-style csprojs only, and it does this by running Roslyn on .NET Core. Roslyn is distributed with the .NET Core SDKs, and dotnet build loads Roslyn from one of these installed SDK versions (normally the latest).

These two ways of building a C# project are more-or-less equivalent, and they invoke the same compiler code. However, they differ on where they can run (Visual Studio is .NET Framework and Windows-only, dotnet build is .NET Core and can run on multiple platforms), and whether they can build legacy non-SDK-style csprojs. dotnet build is also a bit nicer to use from the command-line.

Note that the runtime which Roslyn is loaded into has no bearing on the compiled IL which Roslyn can emit: Roslyn running on .NET Framework can emit IL which is executed by .NET Core just fine, and vice versa.

If you are using analyzers which target .NET Core (unlikely, as Analyzers are encouraged to target .NET Standard 2.0), these will only run from dotnet build.


(1) I'm using ".NET Core" to refer to both .NET Core and .NET 5+.

like image 145
canton7 Avatar answered Sep 23 '22 05:09

canton7