You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新建类库项目执行dotnet pack命令失败求助

.NET CLI构建/打包项目时出现MSB4019错误的解决求助

项目情况

使用VS2019创建了一个全新的类库项目,目标框架为netcoreapp3.1:

PkgTest.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>

项目代码(Class1.cs)

using System;

namespace PkgTest
{
    public class Class1
    {
    }
}

问题现象

执行dotnet pack .\PkgTest.csproj或dotnet build .\PkgTest.csproj命令时,均出现以下错误:

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.CurrentVersion.targets(317,5): error MSB4019: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\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\PkgTest.csproj]

错误说明:系统返回MSB4019错误,提示找不到路径C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\amd64\Roslyn\下的Microsoft.CSharp.Core.targets文件,需要确认导入声明的路径是否正确,以及该文件是否存在于磁盘中。

但通过VS2019右键菜单选择「打包」,或点击「生成」按钮,均能正常完成构建和打包操作,说明本地已具备所有必要的构建工具。

已排查操作

  • 检查磁盘中是否存在错误提示里的缺失文件,并尝试手动导入相关文件,问题未解决。

环境信息

已安装的.NET SDK版本:

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]

补充诊断信息

已执行dotnet build -v diag .\PkgTest.csproj获取详细诊断日志,日志包含MSBuild执行过程中的各项细节。

请问有解决该问题的方法吗?


内容的提问来源于stack exchange,提问作者Itai Bar-Haim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:21:58