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)
Or manual steps:
- Install Project Reunion Project Templates: https://marketplace.visualstudio.com/items?itemName=ProjectReunion.MicrosoftProjectReunion
- Create a new Blank App, Packaged (WinUI 3 in Desktop) project.
- Install the Microsoft.Windows.CsWin32 nuget package (0.1.422-beta)
- Add NativeMethods.txt and some interop methods to it.
- Open MainWindow.xaml.cs, and in the constructor add a pinvoke method call.
- Compile
- Observe errors in the 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:
XamlPreCompile target’s invocation of Csc:
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (12 by maintainers)
Commits related to this issue
- Fix generated editor config files for xaml precompile (#6438) The CSC task was not being passing EditorConfig Files when invoked as part of XamlPreCompile. In addition the `GenerateMSBuildEditorConfi... — committed to dotnet/msbuild by chsienki 3 years ago
- Remove generated VM props: dotnet/msbuild#6323 ; Minor refactorings, fix NotDefault converter; Bump build number; — committed to agc93/dust-mother by agc93 3 years ago
@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.
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.