为什么 `dotnet msbuild` 会在第一次 运行 时失败并显示警告,然后第二次 运行 成功?

Why would `dotnet msbuild` fail with warnings on the first run and then run successfully the second time?

我正在加入一个新的工作团队,之前从未使用过 .NET,但我遇到了一个我什至不知道如何开始诊断的问题。

基本上,我需要构建项目,我在 VisualStudio Code 终端上使用了以下命令:

dotnet msbuild -restore -target:build -p:VisualStudioVersion=16

在第一个 运行 上,编译器抱怨如下错误:

此时我没有尝试 运行 应用程序,因为我想先解决警告。

我再次 运行 命令 (dotnet msbuild -restore -target:build -p:VisualStudioVersion=16),它在没有产生任何警告的情况下进行了编译。

任何对 .NET 有更多了解的人能告诉我为什么会发生这种情况吗?

Why would dotnet msbuild fail with warnings on the first run and then run successfully the second time?

根本问题是您将情况描述为与真实情况相反。 MSBuild 没有 "fail with warnings"。它成功,但有警告。项目搭建成功,所以成功了

一旦您对工具工作原理的心智模型正确,诊断问题就会容易得多。

I ran the command again and it did the compilation without producing any warnings.

没有,它没有再次编译。相反,它什么也没做。建立一个 already-successfully-built 项目什么都不做;该项目已经建成。

你说你想建它,它已经建好了,所以我们完成了。 MSBuild 应该足够聪明,不会 re-do 已经完成的工作;它并不完美,但在不浪费您的时间 re-do 工作方面做得很好。

如果您想强制项目从头开始重建,包括进行所有分析,通常您会使用

/target:Rebuild

没有

/target:Build

如果您想让项目回到 pre-built 状态而不进行重建,您通常会这样做

/target:Clean

I am having an issue that I don't even know how to begin to diagnose.

那么今天是阅读项目文件并了解其工作原理以及阅读 msbuild 文档的好日子:

https://docs.microsoft.com/en-us/visualstudio/msbuild/walkthrough-using-msbuild?view=vs-2019

现在您知道如何开始诊断它了:在使用该工具之前阅读项目文件和您的工具文档