MSBuild missing automatic binding redirects when using NuGet transitive pinning
See original GitHub issueIssue Description
MSBuild doesn’t produce binding redirects automatically for indirect nuget dependencies, leading to runtime failure of my code.
Steps to Reproduce
git clone https://github.com/microsoft/vs-servicehub.git
cd vs-servicehub
git checkout 7659b150cd580d35f3751b33341bcb50f63c32dd
Microsoft.ServiceBroker.sln
Within VS, run the Microsoft.ServiceHub.Analyzers.Tests (net472) tests.
Expected Behavior
I expect them to pass (as they do at the command line).
Actual Behavior
Most of the tests fail with this error:
System.TypeInitializationException : The type initializer for ‘Microsoft.CodeAnalysis.Testing.ReferenceAssemblies’ threw an exception. ---- System.IO.FileLoadException : Could not load file or assembly ‘NuGet.Packaging, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Analysis
The Directory.Packages.props
file forces the NuGet.Protocol
transitive dependency to 6.4.0, where it would naturally be in the 5.x range. Because this is not a direct dependency, and the indirect dependency holds PrivateAssets="compile"
on it such that it isn’t passed to my compiler (or RAR?), msbuild doesn’t produce the necessary binding redirect.
When I add my own top-level dependency on NuGet.Protocol
, such that it goes to RAR, the binding redirect is created and the tests pass.
I believe MSBuild should be enhanced to be aware of transitive dependencies so that it can produce the necessary binding redirects.
Versions & Configurations
.NET SDK 7.0.101 Dev17.5
Issue Analytics
- State:
- Created 8 months ago
- Comments:14 (13 by maintainers)
@AR-May It’s difficult to understand if the linked item will truly resolve this. In order for binding redirects to be generated, the change would need to impact the inputs to
ResolveAssemblyReference
such that itsSuggestedRedirects
output includes consideration for assemblies that are transitive dependencies of the current project but are not included as references of the current project.I updated the resolution state to reflect that this was marked as a duplicate of external issue NuGet/NuGet.Client#4953.
Just an update, I am actively investigating a fix but am on call this week. I’ll let you know when I’ve made progress on this.