Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if MSBuild is using .NET Core?

I am using Scripty.MsBuild to generate some files in a ASP.NET Core project. When I build from VS2017, the target runs successfully, but when I build via dotnet, I get the error:

Scripty.MsBuild.targets(31,5): error MSB4062: The "ScriptyTask" task could not be loaded from the assembly C:\Users\geirsagberg\.nuget\packages\scripty.msbuild\0.7.4\build\\..\tools\Scripty.MsBuild.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

My current workaround is to disable Scripty.MsBuild for Release configuration:

<PackageReference Include="Scripty.MsBuild" Version="0.7.4" Condition="'$(Configuration)'=='Debug'" />

This lets me build from VS in debug mode, and build with dotnet on the build server.

However, I still want to be able to do dotnet run locally in debug mode (for running from VSCode). Therefore I need to include the PackageReference conditionally, based on whether Visual Studio's MSBuild is running, or the dotnet version. As far as I can tell, these two have the exact same version number; am I missing something?

Is there a property in MSBuild I can use to distinguish between the VS MSBuild and the dotnet one?

like image 507
Geir Sagberg Avatar asked Jul 07 '17 08:07

Geir Sagberg


People also ask

Does .NET Core use MSBuild?

NET Core applications are typically built using MSBuild or dotnet.exe . MSBuild and dotnet.exe don't require Visual Studio to be installed, but in order to use them, the following prerequisites are required on a build server: Visual Studio Build Tools installed.

How do you check if I have MSBuild installed?

If you have Visual Studio, then you already have MSBuild installed. With Visual Studio 2022, it's installed under the Visual Studio installation folder. For a typical default installation on Windows 10, MSBuild.exe is under the installation folder in MSBuild\Current\Bin.

Is MSBuild included in .NET framework?

A target profile is a subset of a target framework. For example, the . NET Framework 4 Client profile does not include references to the MSBuild assemblies.


1 Answers

Turns out there is a property MSBuildRuntimeType that can be either Core, Mono or Full. So I can do:

<PackageReference Include="Scripty.MsBuild" Version="0.7.4" Condition="'$(MSBuildRuntimeType)'!='Core'" />

Reference from SDK

like image 179
Geir Sagberg Avatar answered Oct 15 '22 15:10

Geir Sagberg