在 GithubActions CI 中安装用于 UWP 开发的 SDK?

Install SDK for UWP development in GithubActions CI?

我正在尝试构建一些 UWP 库,但收到此错误:

D:\a\ZXing.Net.Xamarin\ZXing.Net.Xamarin\Source\ZXing.Net.Mobile.WindowsUniversal\ZXing.Net.Mobile.WindowsUniversal.csproj(155,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v11.0\Microsoft.Windows.UI.Xaml.CSharp.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

.csproj 文件中的导入子句如下所示:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />

所以我尝试通过 chocolatey 在 Windows2019 VM 中以编程方式安装 UWP 工作负载(两者都使用 this package and this other; not the Windows10SDK 一个,因为它在尝试安装时会出错):

- run: |
  choco install visualstudio2019-workload-universal
  choco install visualstudio2019-workload-universalbuildtools

但这似乎并没有解决问题(显然,软件包已成功安装),因为我仍然不断遇到编译错误。

也试过包括可选包,但无济于事:

choco install visualstudio2019-workload-universal --package-parameters "--includeOptional"

更新:原来我的 CI VM 已经有文件 Microsoft.Windows.UI.Xaml.CSharp.targets,但它位于 C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Microsoft\WindowsXaml\v16.0(和其他它的版本在这个下面的子文件夹中)。详情看我的回答

如果 chocolatey 似乎没有按预期工作,您可以尝试使用 CLI 修改 Visual Studio。例如,当从 powershell 运行 时,下面将通用平台工作负载添加到 VS 时没有提示。

& "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" modify `
  --installPath "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise" `
  --add Microsoft.VisualStudio.Workload.Universal --passive --norestart

Visual Studio Enterprise 2019 包含在所有环境的 Windows Server 2019 environment and Visual Studio Enterprise 2017 is included Windows Server 2016 R2 中。

我在这里创建了一个示例存储库:https://github.com/adam7/modify-visual-studio-install

您可以在 Visual Studio 文档中找到一组 Command-line parameter examples for Visual Studio installation and Visual Studio workload and component IDs as well as how to Use command-line parameters to install Visual Studio

事实证明,GithubActions VM 中安装的 VS2019 版本确实已经包含 UWP 组件(以及 Xamarin.iOS、Xamarin.Android、Xamarin.Mac 等其他组件);问题是我的构建以某种方式默认使用不知道这些扩展的旧版本的 MSBuild(位于 C:/windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe 中的版本)。

一旦我确定我使用的是与 VS2019 捆绑在一起的 MSBuild 版本(%ProgramFiles(x86)%\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\MSBuild.exe 中的版本),一切又开始工作了。

为了在我的特定构建中修复它,我必须修改一个 build.cake 文件(IMO 它实际上可能是 CAKE 中的一个错误,默认情况下无法检测到正确的 MSBuild 版本...)。如果您想查看确切的修复(使用 vswhere 定位 MSBuild),它是 this.