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 CommonLib using its source generators.
-
CommonLib references 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
CommonLib nuget package.
That CommonLib uses MessagePack under the hood is purely an implementation detail and
MessagePack should not become part of the public API of CommonLib. !!
-
CommonLib is 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.4 to 2.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 the Dto class in CommonLib, 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 in MessagePackNotFound.CommonLib.csproj, the problem will go away, because MessagePack.dll can 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
Bug description
We just wanted to upgrade MessagePack from
2.5.198to3.1.4but 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.UserControlsLibgenerated byMicrosoft.Build.Tasks.Windows.MarkupCompilePass1fromPresentationBuildTasks.dll: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 theCommonLibnuget package.That
CommonLibuses MessagePack under the hood is purely an implementation detail andMessagePack should not become part of the public API of
CommonLib. !!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.MarkupCompilePass1wants toload
MessagePack.dllwen compiling the XAML.3.1.4to2.5.198, the problem will go away because the older version of MessagePackdid not have source generators.
[MessagePackObject(AllowPrivate = true)]from theDtoclass inCommonLib, theproblem will go away, because then no source code is generated by the source generators of MessagePack.
<PrivateAssets>Analyzers; Build; ContentFiles; Compile</PrivateAssets>)from the MessagePack package reference in
MessagePackNotFound.CommonLib.csproj, the problem will go away, becauseMessagePack.dllcan now be found.For testing, this repo also contains an
Appthat referencesUserControlsLibas a nuget package. However the solution looks like,Appmust 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.