dotnet / dotnet/msbuild

ResolveAssemblyReferences not run when project does not have project references

Open
#6,785 4 comments 0 reactions 0 assignees View on GitHub
Area: Task: Resolve Assembly References (RAR) triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 13h
Merged PRs (30d)
133

Description

## Summary

Project `MyLib` has a `PackageReference` with `PrivateAssets="compile"`, so that the package's dependencies don't leak as public APIs to downstream projects. Whatever package was used by this `PackageReference` has a dependency on `PkgA` 1.0.0

Project `MyLib` is packed into `MyLib.1.0.0.nupkg`

Project `MyApp` has a `PackageReference` to `MyLib` 1.0.0, and also `PkgA` 2.0.0, but not `ProjectReference` to anything.

It appears that ResolveAssemblyReferences is not run, and therefore no binding redirects are created for `PkgA.dll`. Therefore, at runtime when `MyLib.dll` tries to access `PkgA.dll, Version=1.0.0`, but instead finds `PkgA.dll, Version=2.0.0`, the fusion loader throws an assembly load exception.

## Repro steps

It's a bit involved, sorry 😞

[npnjRuntie.zip](https://github.com/dotnet/sdk/files/6025829/npnjRuntie.zip)

In this repro:

* `MyLib` has ``
* NuGet.Protocol has a dependency on Newtonsoft.Json version 9.0.0
* Pack MyLib to get `MyLib.1.0.0.nupkg`, and copy it to `$(root)\nupkgs\`.
* Now, clear `$(root)\gpf` if MyApp has already been restored, and then restore MyApp.
* MyApp has `` and ``

Run MyApp

## Actual

`MyApp.exe.config` does not have any binding redirects. Running the app gives this error:

> Unhandled Exception: NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://api.nuget.org/v3/index.json. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

## Expected

Modify MyApp's csproj and change `` to ``, and run the app again.

Now `MyApp.exe.config` has a binding redirect for Newtonsoft.Json, and running the app works without error.

## Analysis

Comparing the `project.assets.json` file of `MyApp` when `MyLib` is a `PackageReference` and `ProjectReference`, they're pretty much the same, except for MyLib being a project rather than a package. Therefore, I do not believe the problem is due to NuGet, but is instead due to the .NET SDK or msbuild.

Looking at binlogs of the two builds, when using the project reference, I see that ResolveAssemblyReferences is run, but when using the package reference, I see a log message explicitly saying `No suggested binding redirects from ResolveAssemblyReferences`. The binlog shows that `ResolveAssemblyReferences` did not run, although I didn't figure out why before creating this issue.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.