NuGet / Home

Repo for NuGet Client issues
Other
1.49k stars 250 forks source link

project.assets.json not updated when there are no PackageReferences #7041

Open ptsoccer opened 6 years ago

ptsoccer commented 6 years ago

Details about Problem

NuGet product used (NuGet.exe | VS UI | Package Manager Console | dotnet.exe): VS UI VS version (if appropriate): 15.7.1

My repro used the full .NET framework, but I'm not sure if it affects solely .NET framework

Detailed repro steps so we can see the same problem

  1. Create a .NET framework project
  2. Add a reference to Newtonsoft.Json, ensure it uses the new PackageReference format
  3. Add some code that uses the package
  4. Ensure the project builds
  5. Edit the project file manually and remove all PackageReference tags
  6. Reload project and build again, this should succeed
  7. Checking the project.assets.json file in the obj folder, it still has the Newtonsoft.Json package in it, also the references tab shows a reference to it still

Sample Project

Sample project contains steps up to and including step 4 NuGetStaleCache.zip

rohit21agrawal commented 6 years ago

is the option to allow nuget to download missing packages and automatically check for missing packages during build in visual studio enabled?

image

ptsoccer commented 6 years ago

Yes

image

jairbubbles commented 6 years ago

I've been experiencing the same kind of issues.

In VS (testing with 15.8) the behavior is correct if you remove the package reference through UI, it will generate an empty project.assets.json and it will break the build as expected.

But as described in this issue if you remove the reference and build using msbuild.exe /restore it will still compile as project.assets.json is not being modified. The log says there's nothing to restore.

IMO NuGet packages should always be restored as it leads to weird build issues.

It's also visible (and more problematic) when you reference a project that itself references NuGet packages. You'll get acess to its references only when yourself has at least one package reference. (I'll attach a sample project in another comment)

jairbubbles commented 6 years ago

Sample project 2

NoPackageReferences.zip

There are two projects in this solution with that structure:
NoPackageReferences.csproj => LibWithPackageReference.csproj => Newtonsoft.json

In NoPackageReferences we try to access Newtonsoft.json but we end up with:

error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

If you add a package reference to another package in NoPackageReferences, let's say NUnit it will compile fine. A project.assets.json file will be generated containing the project reference:

"LibWithPackageReference/1.0.0": {
        "type": "project",
        "framework": ".NETFramework,Version=v4.6.1",
        "dependencies": {
          "Newtonsoft.Json": "11.0.2"
        },
        "compile": {
          "bin/placeholder/LibWithPackageReference.dll": {}
        },
        "runtime": {
          "bin/placeholder/LibWithPackageReference.dll": {}
        }
jairbubbles commented 6 years ago

Please note that adding those lines to NoPackageReferences.csproj will fix the issue:

    <ResolveNuGetPackages>true</ResolveNuGetPackages>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
binki commented 5 years ago

Sample project 2

There are two projects in this solution with that structure: NoPackageReferences.csproj => LibWithPackageReference.csproj => Newtonsoft.json

I have this situation all the time in my build configuration. I have to work around it by adding a dummy reference to some random small NuGet package in my executable project so that it is capable of consuming transitive dependencies from the projects it <ProjectReference/>s.

binki commented 5 years ago

Please note that adding those lines to NoPackageReferences.csproj will fix the issue:

    <ResolveNuGetPackages>true</ResolveNuGetPackages>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>

Oh, cool, missed this earlier! I had only added <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to my Directory.Build.props file. Adding the other line fixes everything!