-
-
Notifications
You must be signed in to change notification settings - Fork 754
Description
Bug description
We just wanted to upgrade MessagePack from 2.5.198 to 3.1.4 but get a compiler error after the upgrade. When using MessagePack with source generators, WPF XAML control library project can no longer be build under specific circumstances.
The repo https://github.com/bitbonk/Rerpoduce-MessagePackNotFound demonstrates this. A compiler error occurs when you reference a project that uses MessagePack with source generators in a WPF user control library that contains XAML.
If you build the project you will see the the following compiler error in the WPF project
MessagePackNotFound.UserControlsLib generated by Microsoft.Build.Tasks.Windows.MarkupCompilePass1 from PresentationBuildTasks.dll:
Microsoft.WinFX.targets(211,9): Error MC1000 : Unknown build error, 'Could not find assembly 'MessagePack,
Version=3.1.4.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be'. Either explicitly load this assembly using a
method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.'
These conditions must be met for the error to occur:
-
A project (
CommonLib) references and uses MessagePack. -
MessagePack generates source code in
CommonLibusing its source generators. -
CommonLibreferences the MessagePack nuget package with
<PrivateAssets>Analyzers; Build; ContentFiles; Compile</PrivateAssets>!! The goal behind this
<PrivateAssets>is to hide the MessagePack public APIs from anynone using the
CommonLibnuget package.
ThatCommonLibuses MessagePack under the hood is purely an implementation detail and
MessagePack should not become part of the public API ofCommonLib. !! -
CommonLibis referenced by a WPF project (UserControlsLib) that contains XAML that needs to be compiled.
This has to do something with the code that is generated by the MessagePack source generators. As soon as MessagePack
generates code (that is all internal) inside of CommonLib, Microsoft.Build.Tasks.Windows.MarkupCompilePass1 wants to
load MessagePack.dll wen compiling the XAML.
- If you downgrade MessagePack from
3.1.4to2.5.198, the problem will go away because the older version of MessagePack
did not have source generators. - If you remove the attribute
[MessagePackObject(AllowPrivate = true)]from theDtoclass inCommonLib, the
problem will go away, because then no source code is generated by the source generators of MessagePack. - If you remove the private assets element (
<PrivateAssets>Analyzers; Build; ContentFiles; Compile</PrivateAssets>)
from the MessagePack package reference inMessagePackNotFound.CommonLib.csproj, the problem will go away, becauseMessagePack.dllcan now be found.
For testing, this repo also contains an App that references UserControlsLib as a nuget package. However the solution looks like, App must compile without errors and ideally does not "see" any MessagePack APIs.
Repro steps
Clone the repo and build the solution.
Expected behavior
The solution builds without errors.
Actual behavior
The following compiler error occurs in the WPF XAML project.
Microsoft.WinFX.targets(211,9): Error MC1000 : Unknown build error, 'Could not find assembly 'MessagePack,
Version=3.1.4.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be'. Either explicitly load this assembly using a
method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.'
- Version used: 3.1.4
- Runtime: .NET 8