Msbuild: PackageReference/ProjectReference transitive behavior

Created on 26 Apr 2018  路  2Comments  路  Source: dotnet/msbuild

Steps to reproduce

  1. Create a new solution
  2. Add 2 C# Class Library (.NET Framework) projects named ClassLibrary1 and ClassLibrary2
  3. Add a project reference: ClassLibrary2 references ClassLibrary1
  4. Add a NuGet reference in ClassLibrary1 to Newtonsoft.Json (<PackageReference>)
  5. In ClassLibrary2 add the following code

public class Class1 { public void A() => Newtonsoft.Json.JsonConvert.SerializeObject(null); // Use Newtonsoft.Json }

Execute the following command line

msbuild /bl

=> Build failed

In ClassLibrary2 add a NuGet reference to a NuGet package that doesn't depend on Newtonsoft.Json, such as NUnit (<PackageReference>)

Execute the following command line

msbuild /bl

=> Build succeed

Expected behavior

I think the first build should succeed, or the second one should fail.

Actual behavior

It fails.

Environment data

msbuild /version output:
15.6.85.37198

OS info:
Windows 10 1607

If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc):
Visual Studio 15.6.7 and command line (msbuild)

Most helpful comment

This is a NuGet behavior. Older versions of NuGet didn't treat dependencies as transitive in this way, so NuGet (currently) requires you to explicitly opt into the new behavior from your ClassLibrary2 with

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

https://github.com/NuGet/Home/issues/4581 would also help for this case.

All 2 comments

This is a NuGet behavior. Older versions of NuGet didn't treat dependencies as transitive in this way, so NuGet (currently) requires you to explicitly opt into the new behavior from your ClassLibrary2 with

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

https://github.com/NuGet/Home/issues/4581 would also help for this case.

Duplicate of NuGet/Home#4488

Was this page helpful?
0 / 5 - 0 ratings