dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.23k stars 1.35k forks source link

Nuget dependencies not found when using reflection #4838

Open ldubrois opened 5 years ago

ldubrois commented 5 years ago

Steps to reproduce

I have a project P1 containing a class C1 and C2. I publish it as a nuget package P1.

In an another solution, I create a console project P2, add a nuget reference to P1. The code in the project P2 tries to find by reflection C1.

From Visual Studio (2017 & 2019), I start the P2 project (F5).

Expected behavior

I should find the type C1.

Actual behavior

C1 and the P1 assembly does not exists.

If I publish the project, the P1 dll is present and the app works fine.

Still in Visual Studio, if I had a direct reference to C2 in the P2 project, then I can find C1 by reflection.

Visual Studio (msbuild) seems to make 'optimizations' and does not provide correctly the nuget dependencies if they are only used by reflection.

Environment data

msbuild /version output:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise>msbuild /version
Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

16.3.1.50202

OS info: image

livarcocc commented 5 years ago

Can you share what your project looks like? And what the nuspec for p1 looks like as well, or the package?

ldubrois commented 5 years ago

I've created a very dumb project to illustrate the point : MS_NugetBug_Sample.zip

Just with F5 on P2, the type is not found. If you publish the app, the type is found.

If you uncomment the first line in P2, making an explicit reference to P1, the with F5 and publish, it does work.

While making the sample project I've constated a difference between net core app 2.2 and net core app 3.0. The failing case in 2.2 is passing in 3.0.

In 2.2, the P1.dll file is not in the debug build directory, while with the exact same code, in 3.0, P1.dll is in the debug build directory.

ldubrois commented 4 years ago

Hi,

Any news on this issue ?

Regards,

Luke

bananacles commented 4 years ago

@ldubrois I'm just starting my search on this so take this with a grain of salt.

The reason reflection isn't finding your stuff is because it isn't there. The NuGet package is a .zip containing your .dlls, not the .dlls themselves. If you wanted to you could extract the .dlls out of it at runtime, but that seems hacky and I'm trying to avoid doing this myself.