msbuild: Source Generators fail in WinUI 3 projects because Csc is invoked without AnalyzerConfigFiles

Describe the bug

A project reunion project fails to build when it contains code that relies on a Source Generator in any of the XAML code-behind files.

Steps to reproduce the bug

(based on @dotMorten’s report https://github.com/microsoft/CsWin32/issues/219#issuecomment-811272354)

Minimal repro solution

Or manual steps:

  1. Install Project Reunion Project Templates: https://marketplace.visualstudio.com/items?itemName=ProjectReunion.MicrosoftProjectReunion
  2. Create a new Blank App, Packaged (WinUI 3 in Desktop) project.
  3. Install the Microsoft.Windows.CsWin32 nuget package (0.1.422-beta)
  4. Add NativeMethods.txt and some interop methods to it.
  5. Open MainWindow.xaml.cs, and in the constructor add a pinvoke method call.
  6. Compile
  7. Observe errors in the error list error list

Using VS16.10.p1

Expected behavior

The build is successful.

Analysis

Examining the build log I’ve found the problem: The XamlPreCompile target as defined in MSBuild\Current\Bin\Microsoft.CSharp.CurrentVersion.targets invokes the Csc task but omits AnalyzerConfigFiles that is normally passed in. As a result, the CsWin32 source generator is not getting the info it needs to generate the APIs referenced in the XAML code-behind.

As the faulty target is defined in Microsoft.CSharp.CurrentVersion.targets, this repo seemed the most appropriate place to file the issue.

Screenshots

The CoreCompile target’s invocation of Csc: image

XamlPreCompile target’s invocation of Csc: image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@jmarolf Since we have this (extremely unfortunate) dependency in core targets, can we get someone from Roslyn to port the changes to CoreCompile to the XamlPreCompile task? And I guess add the updating to the to-do list for changing that target?

https://github.com/dotnet/msbuild/blob/1cfebe4dd3b1bb26bbf76128ff5f9270b6c090bb/src/Tasks/Microsoft.CSharp.CurrentVersion.targets#L163-L169

@riverar It should be fixed in VS 16.10 preview 4.

I made those changes locally and it didn’t resolve the error.

Which changes, exactly? @chsienki was able to build a project after applying #6438, I believe.

Any news on which deliverable will contain this fix? Seems to still be broken in Visual Studio 16.10p3 / latest .NET 5 SDK. Also tested with 6.0.100-preview.4.21255.9 to no avail.

@dotMorten maybe your @(EditorConfigFiles) list is empty. Passing in an empty list would be no better than before. The fix would have to include whatever was necessary to initialize that list.