Nuget package not found after restore VS 2019 16.5.0

Viewed 4039

I've been fighting with nuget all morning, trying to get a solution that builds in the UI AND from the command line. Here's the latest problem, which I haven't made any headway on:

  • I'm running nuget restore on the solution file. This works, all referenced packages are restored - I can see the files in the /packages folder under the solution folder.
  • I'm building with devenv command line - I have to because this solution contains project types that msbuild doesn't support.
  • The first project that references a nuget package, fails to compile with ...cs(3,7,3,17): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
  • The project builds (and rebuilds, and rebuilds with the package folder cleared out) just fine in the UI, but the command line build isn't seeing the restored packages.
  • The build that's failing is in a CLEAN folder on the same computer where I'm doing the UI build, so it's get from source control, nuget restore, devenv build.

Things I've tried

  • Looking for bad hint paths in the project file (saw this in another question/answer). These references don't appear in the project file at all - trying to add them produces an error saying that the reference can't be added because it's already added automatically by the build system.
  • Verifying that files do exist after restore.
  • Doing the same steps from a command line in the SAME folder where the UI is building. This works fine.

What am I missing? this shouldn't be so hard..

UPDATE: The solution consists of 14 projects: 9 C# class libraries, 2 c# applications, 1 reporting services project and 2 WiX installer projects. All C# projects target Net472, NOT Core. The key part of the solution structure appears to be:

  • Project A references
    • Newtonsoft.Json via nuget
  • Project B references
    • Project A
    • Newtonsoft.Json via nuget
    • Other packages via nuget

During build, project B fails to compile due to the lack of a reference to Newtonsoft.Json. Project A and all of the other nuget packages are supplied to the compier as references. Again, all nuget packages are in fact restored - Project A finds Newtonsoft.Json, project B does not.

In the detailed msbuild log output, this is the only mention of Newtonsoft.Json in the build of project 10 (Project B above):

10>  Dependency "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed".
10>      Resolved file path is "...ProjectA\bin\Release\Newtonsoft.Json.dll".
10>      Reference found at search path location "...ProjectA\bin\Release".
10>          For SearchPath "...ProjectA\bin\Release".
10>          Considered "...ProjectA\bin\Release\Newtonsoft.Json.winmd", but it didn't exist.
10>      Required by "...ProjectA\bin\Release\ProjectA.dll".
10>      Required by "C:\...ProjectA2\bin\Release\ProjectA2.dll".
10>      Found related file "...ProjectA\bin\Release\Newtonsoft.Json.xml".
10>      The ImageRuntimeVersion for this reference is "v4.0.30319".

(Folder and project names have been obscured)

3 Answers

A couple things going on here, finally got a solution that works. Why this built in the IDE is anyone's guess - it's adding some extra secret sauce to make things work (more than just the automatic nuget restore).

  • I tried changing all projects to use PackageRef instead of packages.config. That caused nuget restore to fail with an obscure msbuild error that I didn't try to diagnose.
  • I noticed that SOME of the nuget packages were referenced in the .csproj files with ordinary Reference elements, but some of them were not (specifically, Newtonsoft.Json in "Project B" - and some others that I hadn't noticed due to B failing).

To correct the situation:

  1. Remove ALL use of PackageRef elements - change back to packages.config in ALL projects
  2. Make sure the each of the nuget -provided DLLs is referenced in the .csproj files. You have to do this by editing the csproj file by hand - the IDE won't let you add the missing references.

I'm assuming that this is a temporary situation and that in the long run the solution will be to use PackageReference everywhere.

you already checked the files app.config and packages.config, and the dotnet framework version?

Nuget package not found after restore VS 2019 16.5.0

devenv /build command line does not have the job to restore nuget packages by default. However, there are such options in VS IDE so that it will restore packages first and then build. But these do not work in command line.See this similar issue.

enter image description here

But you still want to use devenv to build your project and since you use a framework project with packages.config, I suggest you could use nuget.exe.See this.You can try these:

1) download nuget.exe from this link and then configure its local address to PATH in the environment variable and make sure that you can call nuget from CMD.

2) open vs command prompt, cd the path of the solution and then type this first:

nuget restore

Then you can type your devenv command line and I am sure that this will execute without any errors.

devenv xxxx.sln /rebuild

Besides,you can add a custom target in any xxx.csproj file of your solution like this:

 <Target Name="restoresolution" BeforeTargets="BeforeBuild">
    <PropertyGroup>
      <nugetpath>C:\tools</nugetpath> /////the local path of the nuget.exe
    </PropertyGroup>
    <ItemGroup>
      <slns Include="$(MSBuildProjectDirectory)\..\**\*.sln" />
    </ItemGroup>
    <Exec command="$(nugetpath)\nuget restore %(slns.Identity)" />
  </Target>

Then you can run devenv xxxx.sln /rebuild directly.

Related