Home: project.assets.json not updated when there are no PackageReferences

Created on 19 Jun 2018  路  7Comments  路  Source: NuGet/Home

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

Icebox PackageReference Bug

All 7 comments

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

image

Yes

image

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)

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": {}
        }

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

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

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.

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!

Was this page helpful?
0 / 5 - 0 ratings