Home > OS >  `dotnet pack` on a fresh project fails
`dotnet pack` on a fresh project fails

Time:09-07

I've created a fresh class library (using VS2019). This is the CSProj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Authors>Itai</Authors>
    <Company>Company</Company>
    <Description>Test NuGet Package Creation using CLI</Description>
  </PropertyGroup>

</Project>

All it have is the Class1.cs file from the template:

using System;

namespace PkgTest
{
    public class Class1
    {
    }
}

but when I run dotnet pack .\PkgTest.csproj I get this:

PS C:\Users\itaib\source\repos\PkgTest\PkgTest> dotnet pack .\PkgTest.csproj
MSBuild version 17.3.0 92e077650 for .NET
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.CSharp.CurrentVersio
n.targets(317,5): error MSB4019: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\M
SBuild\Current\Bin\amd64\Roslyn\Microsoft.CSharp.Core.targets" was not found. Confirm that the expression in the Import
 declaration "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\amd64\Roslyn\Microsoft
.CSharp.Core.targets" is correct, and that the file exists on disk. [C:\Users\itaib\source\repos\PkgTest\PkgTest\PkgTes
t.csproj]

(BTW - Same thing happens when I try running dotnet build .\PkgTest.csproj)

When using VS2019 right click menu on the project and selecting "Pack" a package is created (or when I click on "Build" the project gets built), which means I have all the tools required to build and create a package.

I tried investigating the error, looking for the relevant files on disk and importing them, nothing works.

running dotnet --list-sdks returns this:

3.1.422 [C:\Program Files\dotnet\sdk]
5.0.403 [C:\Program Files\dotnet\sdk]
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.400 [C:\Program Files\dotnet\sdk]

Any idea how to solve this?

UPDATE

The output of dotnet build -v diag .\PkgTest.csproj can be found here: https://pastebin.com/RTtgWbZ1

CodePudding user response:

You have an environment variable set:

MSBUILD_EXE_PATH = C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe

Remove this environment variable, it will resolve your issue. It is causing the dotnet command to use the msbuild in the Visual Studio folder instead of its own folder.

  • Related