如果指定了 OutputPath,为什么 MSBuild 会忽略 ProjectReference Private=False

Why does MSBuild ignore ProjectReference Private=False if OutputPath is specified

我有一个包含非私有 ProjectReference 项的项目:

<ItemGroup>
    <ProjectReference Include="..\..\RProcessConfig\RProcessConfig.csproj">
      <Private>false</Private>
    </ProjectReference>
  </ItemGroup>

从命令行使用 "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Msbuild.exe" %cd%\Modules\AreaArithmetics\AreaArithmetics.csproj 就像一个魅力。依赖dll没有复制到输出目录,我很高兴。

但是如果我使用 "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Msbuild.exe" %cd%\Modules\AreaArithmetics\AreaArithmetics.csproj /p:OutputPath=%cd%\RProcessService\RProcessServiceCore\bin\publish\Modules 指定另一个输出路径而不是在 AreaArithmetics.csproj 中指定的路径,<Private>false</Private> 部分似乎被忽略并且我的依赖项出现在输出文件夹中

这两个调用之间的唯一区别是第二个调用指定了 MSBuild 的 OutputPath。这怎么会破坏参考配置?

在这种情况下,通过 CLI 调用设置的 OutputPath 将成为 global property,因此也适用于所有引用的项目,因此 RProcessConfig.csproj 也将生成相同的输出目录,主项目不会复制它,但它已经存在。

最新版本的 MSBuild (16.8+) 可以通过同时传递 -p:PassOutputPathToReferencedProjects=False(引入 by this PR)来抑制此全局 属性 的流动。