2

I'm trying to build some UWP libraries and I receive this error:

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.

The import clause in the .csproj file looks like this:

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

So I've tried to install the UWP workload programatically in the Windows2019 VM via chocolatey (both with this package and this other; not the Windows10SDK one because it gives an error when trying to install) with:

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

But this doesn't seem to cut it (the package gets installed successfully, apparently) because I still keep getting that compilation error.

Also tried including optional packages, to no avail:

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

UPDATE: Turns out that my CI VM already had the file Microsoft.Windows.UI.Xaml.CSharp.targets, but it was located in C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\WindowsXaml\v16.0 (and other versions of it in subfolders underneath this one). For more details, look at my answer.

knocte
  • 16,941
  • 11
  • 79
  • 125
  • Have you checked if your project works well without ZXing and other third party libraries? – Roy Li - MSFT Sep 30 '19 at 03:12
  • My project doesn't use ZXing, it is ZXing! (it's a fork) – knocte Sep 30 '19 at 03:55
  • So when you checked the Microsoft.Windows.UI.Xaml.CSharp.targets file's location, does it exist in the path mentioned by the error log? – Roy Li - MSFT Sep 30 '19 at 09:02
  • of course not :) but I found out it's in a different place, will update my question shortly – knocte Sep 30 '19 at 09:22
  • It seems the chocolatey doesn't work correctly. I'd suggest you to use Visual Studio Installer to download the workload. – Roy Li - MSFT Oct 01 '19 at 03:14
  • this is in a VM spawned by GitHubCI, can the VisualStudio installer be launched from the command line in a headless way? – knocte Oct 01 '19 at 09:43
  • I'm sorry to say I can't tell that because I'm not familiar with visual studio installer.My suggestion is that you could ask about this in VS's forum – Roy Li - MSFT Oct 02 '19 at 05:29

2 Answers2

3

Turns out that the VS2019 version installed in GithubActions VMs does already include UWP components (and others such as Xamarin.iOS, Xamarin.Android, Xamarin.Mac, etc); the problem was that my build was somehow defaulting to use an old version of MSBuild (the one located in C:/windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe) that wasn't aware of these extensions.

As soon as I made sure that I used the version of MSBuild bundled with VS2019 (the one in %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe) everything started to work again.

To fix it in my particular build I had to modify a build.cake file (IMO it might actually be a bug in CAKE to not detect the proper version of MSBuild by default...). If you want to take a look at the exact fix (that uses vswhere to locate MSBuild), it's this.

knocte
  • 16,941
  • 11
  • 79
  • 125
1

If chocolatey doesn't seem to be working as expected you could try modifying Visual Studio using the CLI. For example the below adds the Universal Platform workload to VS with no prompts when run from powershell.

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

Visual Studio Enterprise 2019 is included in the Windows Server 2019 environment and Visual Studio Enterprise 2017 is included Windows Server 2016 R2 in all environments.

I created a sample repo here: https://github.com/adam7/modify-visual-studio-install

You can find a set of 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 in the Visual Studio docs.

Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
  • thanks! I'll try this out, but where is `vs_enterprise.exe` located? and if VS is not installed, is there an official URL where one can wget `vs_community.exe` somewhere? – knocte Oct 04 '19 at 10:35
  • I couldn't find anywhere to wget vs_enterprise so I just downloaded it and added it to my repo. – Adam Cooper Oct 04 '19 at 12:12
  • vs_enterprise is the enterprise edition which is not free, that's why it must not be locatable? – knocte Oct 04 '19 at 18:32
  • VS Enterprise is installed on all Windows GitHub Action VMs, I should have thought about this a little more because the instlaller is available in the VS Installer directory. I've updated my answer so now there's no need to download anything. – Adam Cooper Oct 04 '19 at 19:15
  • You're right it's not free but it does seem to installed on the VMs which is super nice and means you can all sorts of nice things when building using an Action. – Adam Cooper Oct 04 '19 at 19:16
  • thanks so much for your answer, but I think I've found the culprit to be something else! I'll post my findings in the form of another answer, but please don't delete yours because I think it might be helpful to someone! I will upvote it too – knocte Oct 06 '19 at 08:11
  • and I just posted my answer – knocte Oct 06 '19 at 16:23