BerndGit
BerndGit

Reputation: 1660

\MSBuild\16.0\Bin\Microsoft.CSharp.targets file not found

I want to work on an open source project in c#.

When I want to rebuild the project I get:

Das Projekt "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\16.0\Bin\Microsoft.CSharp.targets" wurde von "C:\Program Files\dotnet\sdk\2.1.701\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets" nicht in (41,3) importiert, weil die Datei nicht vorhanden ist.

What means in english

The project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\16.0\Bin\Microsoft.CSharp.targets" was not imported by "C:\Program Files\dotnet\sdk\2.1.701\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets" because the file is missing.

I the folder: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\ I just have the subfolders:

Visual Studio Community 2019 Version 16.1.6., .Net Framework Version 4.7.03056

So I think I somehow need to upgrade MSBuild 15.0 -> 16.0.

What I have tried:

Thank you for any help. BerndGit

PS: the project I'm trying to compile ist https://github.com/BerndGit/Kamban/tree/feature/refactoring

UPDATE Dotnet info

dotnet --info
.NET Core SDK (gemäß "global.json"):
 Version:   2.1.701
 Commit:    8cf7278aa1

Laufzeitumgebung:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.701\

Host (useful for support):
  Version: 2.1.12
  Commit:  ccea2e606d

.NET Core SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.1.701 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
PM> 

Upvotes: 3

Views: 5737

Answers (1)

LoLance
LoLance

Reputation: 28176

Please check the way it imports Microsoft.CSharp.targets in Kamban.csproj:

<LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>

According to the content of Kamban.csproj, it uses $(VisualStudioVersion) to locate Microsoft.CSharp.targets file.It works well for VS2017 and earlier versions, but not recommended in VS2019 since the location of msbuild has changed in VS2019.

See this document: Starting in VS2019 preview, MSBuild will use "Current" instead of the major version number as part of its path. So the Microsoft.CSharp.targets file which you need for build process actually exists in path ...\2019\Community\MSBuild\Current\Bin\Microsoft.CSharp.targets instead of ...\2019\Community\MSBuild\16.0\Bin\Microsoft.CSharp.targets.

That's why it said file is missing in error message, cause it tried to find the targets file from wrong path.

As a Workaround:

Use the $(MSBuildToolsVersion) instead of $(VisualStudioVersion) to locate the .targets file.

Open and edit the Kamban.csproj file, change the value of LanguageTargets to:

<LanguageTargets>$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>

Then the build engine can get correct path to find missing file.

(Note:This change works for both VS2017 and VS2019)

In addition:I've checked the open-source solution, it seems to target .net framework and .net standard, I'm not sure why this error message comes from SDK.targets, I suggest you rebuild the project in Visual Studio IDE or by msbuild command-line.

Upvotes: 2

Related Questions