422

I have an ASP.NET MVC5 application that worked yesterday and now I am getting this error when I try to build:

This project references NuGet package(s) that are missing on this computer.

I have the two options checked that allow nuget to automatically download and install missing packages checked / turned ON. I have also tried deleting all of the files in the packages folder and then have nuget re-download them. Also when I open nuget and look for updates it says there are none that need to be installed. I can't figure what else to do to move beyond this amazingly annoying issue.

I have also enabled nuget restore by right clicking the project and selecting that option. It then added a nuget folder and three items in that folder and does nothing to resolve the problem. I've tried re-building and still get the same error.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Austin Harris
  • 5,150
  • 6
  • 26
  • 39
  • 1
    Does your solution includes a .nuget folder and have you updated NuGet to latest version? See here: http://stackoverflow.com/questions/18833649/error-using-nuget-in-vs2012-missing-packages – David Brabant Apr 07 '14 at 10:27
  • Yes, tried that and it did not solve my build error message problem. – Austin Harris Apr 07 '14 at 18:01
  • Another reason for this error is an `The operation has timed out.` error. during the build. You need to check your build log, or the **Diagnostics** tab in the Visual Studio Online Build Failed information screen. – Joshua Drake Oct 22 '15 at 18:15
  • 1
    None of the solutions work for me. I'm downloading from a repo and the packages restore in the correct file structure for the first project, second project can't find them. Checking the .csproj shows that the correct relative path is being used so I'm at a loss for trying to solve this. https://github.com/DanJ210/ProgrammersTest – Daniel Jackson Apr 08 '19 at 16:50
  • This happened to me when I was upgrading versions of an extension (right click on the project, Manage NuGet Packages, upgrade). Visual studio forgot to delete the reference to the old version and added a ref to the new one. Manually editing the project file and deleting the stale references (there were 2!) was the fix. – jozxyqk May 23 '20 at 20:48

23 Answers23

587

In my case, I had to remove the following from the .csproj file:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

In fact, in this snippet you can see where the error message is coming from.

I was converting from MSBuild-Integrated Package Restore to Automatic Package Restore (https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#migrate-to-automatic-package-restore)

Loren Paulsen
  • 8,960
  • 1
  • 28
  • 38
  • 17
    This worked for me, but I only needed to remove the element. VS[2013] seemed to restore it if I removed the element as well. – Robert Taylor Dec 18 '14 at 10:52
  • What exactly is thist nuget.targets. What purpose does it serve besides preventing project to compile? – Anderson Jan 22 '15 at 23:07
  • @Anderson - before visual studio supported Automatic Package Restore, this build "target" could be bolted on to solutions to enable packages to be restored during build. – Loren Paulsen Jan 25 '15 at 06:37
  • 14
    This is seriously unbelievable. Why does Microsoft make everything so difficult? – dimiguel Feb 04 '15 at 23:53
  • Another reason for this error is an `The operation has timed out.` error. during the build. You need to check your build log, or the **Diagnostics** tab in the Visual Studio Online Build Failed information screen. – Joshua Drake Oct 22 '15 at 18:15
  • This answered it for me, but in my case I just removed the following element within : ... – Nicholas Petersen Nov 03 '15 at 15:58
  • 35
    if this can be removed, why is it there at the first place? – OK999 Aug 17 '16 at 19:43
  • 4
    OK9999, at one point, you must have enabled it from an earlier version of Visual Studio, by right clicking on the solution and choosing "Enable NuGet package restore" which did it the old way. We don't need that anymore – Loren Paulsen Aug 18 '16 at 19:19
  • I didn't have the `Import` but removing the `Target` worked – Someone Somewhere Feb 13 '17 at 10:25
  • @LorenPaulsen I don't think that is true. I just created a project in an existing solution. The first time I compiled it, it worked fine. After that, I started getting this message. I am using VS 2015. I don't see that option in the context menu nor have I used that option in the past. – fizch Oct 18 '17 at 15:34
  • Did anyone check if "$(SolutionDir)\.nuget\NuGet.targets" is missing? – SINGULARITY Jan 04 '18 at 19:35
  • @SINGULARITY Yes, it was there for me but I kept getting the error message. After deleting the error condition the project compiled normally – Luís Gonçalves Apr 05 '18 at 09:39
  • Removed below lines – Amila Thennakoon Mar 19 '19 at 16:00
  • I added .nutget directory to my solution folder – sobelito May 19 '19 at 14:03
  • 2
    This error usually occurs when you change the project directory name or path. edit the .proj file and replace the new directory name of path to restore packages. – Kashif Saleem Sep 02 '20 at 07:51
  • 1
    Can't believe this still happens with the latest version of visual studio. Thanks for this simple solution – Darthchai Feb 03 '21 at 17:56
  • Still seeing this error in VS 2017 in 2021. That's pathetic -- Microsoft has *got* to fix this. Thanks for the help, @LorenPaulsen!! – Carthax Jul 08 '21 at 13:40
  • In my case the scenario was that an R&D type project was excluded from the solution. Later the other projects in the solution were changed from framework 4.6.1 to 4.8. Some months after that the R&D project was brought back into the solution, but we neglected to change the target framework. After trying this solution, realized the problem with the framework version and updating that solved the problem. – Paul Evans Jan 20 '22 at 17:44
  • It works for me but if the same error message still pops up, you need to specify clean build within teamcity settings: edit project/build step/advanced options/command/ select clean form the drop box – fragg May 20 '22 at 12:13
  • This problem (with slightly different XML) was triggered when I upgraded Microsoft.Bcl.Build on the insistence of a different error. Because the old version of the package was no longer present, VS freaked out. – Grault Nov 03 '22 at 15:10
  • "https://stackoverflow.com/users/1603399/dimiguel" looks like you never use android or ios build tools.... – user1005462 Jan 25 '23 at 16:24
121

One solution would be to remove from the .csproj file the following:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

How?

  1. Right click on project. Unload Project.
  2. Right click on project. Edit csproj.
  3. Remove the part from the file. Save.
  4. Right click on project. Reload Project.
Ivan Santiago
  • 1,682
  • 1
  • 17
  • 13
  • 2
    When you move a project from one place to another, this works great. – Dean Seo Aug 16 '17 at 10:55
  • 4
    @ClintEastwood My answer explained **HOW** to do it. That's the difference. If a user is looking for a **HOW TO** my answer has it, in contrast with the answer above. – Ivan Santiago May 16 '18 at 13:30
  • 7
    @IvanSantiago You could have: added it as a comment, or, edited the original answer with the How To. – Colin Dec 28 '18 at 23:50
71

In my case it happened after I moved my solution folder from one location to another, re-organized it a bit and in the process its relative folder structure changed.

So I had to edit all entries similar to the following one in my .csproj file from

  <Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

to

  <Import Project="packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

(Note the change from ..\packages\ to packages\. It might be a different relative structure in your case, but you get the idea.)

Nikita R.
  • 7,245
  • 3
  • 51
  • 62
  • 4
    Similar issue....I had moved the .csproj file up a level in the directory structure and had to change from "..\..\packages\..." to "..\packages\...". – tmgirvin Jun 03 '15 at 23:20
  • 3
    I had a similar issue, but really weird. I was using it in a subsolution module, so it was fine in that solution, but when I referenced that solution from another solution, the packages were in a different place. I changed ..\packages to $(SolutionDir)packages throughout the .csproj and that fixed it. – JoeNCA Jul 15 '15 at 21:15
  • 3
    If you don't want to muck around with the .csproj file manually I've found that taking a note of all the nuget packages you have installed for the project, deleting them and reinstalling them resolved this issue for me. I was trying to remove a project from a solution to put into its own git repository when I ran into this issue. – Ebsan Aug 26 '15 at 02:36
  • 1
    does this mean your .csproj is at the same level as your .sln file? – Simon_Weaver May 13 '17 at 02:35
  • 1
    @Simon_Weaver the position of your `.csproj` relative to your `.sln` does not matter in this context. What matters is whether anything that is referenced in your `.csproj` had moved somewhere else. If so, then you need to fix it. If you moved your '.csproj' with everything that it references intact, but kept your `.sln` where it was, then you'd have to fix the `.sln` file to the new location of `.csproj`-es, but there would be no need to edit `.csproj` files. – Nikita R. May 15 '17 at 04:58
  • This was my exact issue. It worked well to follow this solution. – JosephDoggie Aug 17 '22 at 12:59
23

I easily solve this problem by right clicking on my solution and then clicking on the Enable NuGet Package Restore option

(P.S: Ensure that you have the Nuget Install From Tools--> Extensions and Update--> Nuget Package Manager for Visual Studio 2013. If not install this extention first)

Hope it helps.

M.A
  • 273
  • 2
  • 8
  • 12
    This is the old way of restoring nuget packages and should be avoided. – The Muffin Man Aug 25 '15 at 17:49
  • 4
    @TheMuffinMan: Can you please clarify what the new way is and why this way should be avoided (considering that VS 2013 error output tells you to do exactly that)? – CantrianBear Aug 19 '16 at 17:15
  • 3
    @CantrianBear Navigate to this page https://docs.nuget.org/consume/package-restore and find the section called `MSBuild-Integrated Package Restore`. That is the old way and it lists some reasons why you should use the new way. – The Muffin Man Aug 19 '16 at 17:58
  • 1
    See David Ebbo's blog on this http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html Now... "NuGet now always restores packages before building in VS." – timB33 Mar 04 '19 at 09:54
20

In my case it had to do with the Microsoft.Build.Bcl version. My nuget package version was 1.0.21, but my project files were still pointing to version 1.0.14

So I changed my .csproj files from:

  <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
   <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
  </Target>

to:

 <Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
  <Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
    <Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />

And the build was working again.

henkie14
  • 949
  • 9
  • 7
18

I have the same issue. I encountered it when I copied an existing project and transferred it on the folder of my solution directory and added it as existing project to my empty solution. So I have to edit my csproj file and look for this specific line of code, most of the time, this is can be found on the last lines:

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

After that line, I have to comment these out:

  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.props'))" />
    <Error Condition="!Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets'))" />
  </Target>
  <Import Project="..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets" Condition="Exists('..\..\..\..\..\packages\EntityFramework.6.4.0\build\EntityFramework.targets')" />

Your solution will prompt that there was a change on your project, just select Reload All:

enter image description here Then everything works fine after rebuilding my solution.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
13

If you are using TFS

Remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder. Make sure the files themselves are also removed from the solution workspace. Retain the NuGet.Config file to continue to bypass adding packages to source control.

Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file. Open the project file(s) in the editor of your choice and remove the following settings:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

If you are not using TFS

Remove the .nuget folder from your solution. Make sure the folder itself is also removed from the solution workspace.

Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file. Open the project file(s) in the editor of your choice and remove the following settings:

<RestorePackages>true</RestorePackages>  
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

Reference: Migrating MSBuild-Integrated solutions to use Automatic Package Restore

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
11

Removed below lines in .csproj file

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" 
Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
 <ErrorText>This project references NuGet package(s) that are missing on this computer. 
 Enable NuGet Package Restore to download them.  For more information, see 
 http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" 
Text="$([System.String]::Format('$(ErrorText)', 
'$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Amila Thennakoon
  • 419
  • 1
  • 5
  • 15
9

Is it possible that the packages have been restored to the wrong folder? Check that the paths in the csproj files are correct.

If they are different it could be caused by the packages now being restored to a different location. This could be caused by a NuGet.Config file being checked in specifying a node like this:

<add key="repositoryPath" value="..\..\Packages" />

The packages are being restored, by the projects are still looking at the old location.

infojolt
  • 5,244
  • 3
  • 40
  • 82
  • 1
    I believe it could be a path issue since I did move the location of the files but I don't see where there is a hard coded path anywhere. I looked in the proj file and all the packages files seem to be relative like this: False ..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll – Austin Harris Apr 07 '14 at 17:57
  • adding this to web.config did not help: – Austin Harris Apr 07 '14 at 22:55
  • You don't want to add that to the web.config. I was referring to the NuGet.config file and you want to check the relative path. Where are your packages relative to the csproj files? Are they in ..\packages? It sounds like the packages are being restored correctly, but your projects are looking in the wrong place. – infojolt Apr 08 '14 at 07:42
  • I can't find nuget.config file – Kashif Saleem Sep 02 '20 at 07:42
8

I had the same issue. In my case installing the Microsoft.Bcl.Build package fixed the problem.

mheyman
  • 4,211
  • 37
  • 34
  • This worked for me as well - but I have no idea whether the right thing to do was install that package (which has the same effect as henkie14's version changing answer below or just delete all those Targets - are they actually doing anything useful? – Gaz Feb 05 '15 at 10:15
  • In `1.0.21` version no files in package, installation of `1.0.14` version fixed this. – FLCL Feb 05 '16 at 12:33
6

One solution would be to remove from the .csproj file the following:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.

Umar Khaliq
  • 61
  • 1
  • 4
6

The first thing to try is to right-click the solution and select "Restore Nuget Packages".

In my case that did not work, so I followed some recommendations on deleting "Import" and "Target" on project file, this worked for 2 of my 3 projects, but got a different error on the last one.

What worked was to open the Package Manager Console and run:

Update-Package -reinstall -ProjectName MyProjectName

It takes some time but since it reinstall all packages your project will compile without problems

3

The mistake appears to have been triggered somewhere in the code. Let's look at the.csproj file, where this can be found towards the end.

Step 1 Remove the package from package.config file.

<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net46" />

Step 2 Edit the .csproj project file and removed the below settings

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />

Step 3 Go to package manager console and run the command Update-Package –reinstall

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
subeesh k
  • 334
  • 2
  • 3
2

These are the steps I used to fix the issue:

To add nuget packages to your solution:

  1. Right click on the project (not solution) you want to reference nuget packages.
  2. Choose: Manage nuget packages
  3. On the popup window, on the left you have three choices. If you choose Online > Microsoft & .NET, you will be able to install Microsoft ASP.NET Web API 2.2 package grouper (or whatever package you need - mine was this).
  4. Now right click on your solution (not project) and choose Enable nuget package restore. This will cause the packages to be automagically downloaded at compilation.
gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • 1
    All I had to do was enable nugget package restore for the solution. Apparently everything else was already set up correctly. – schmiddy98 Feb 23 '15 at 16:28
2

For me it worked as I just copied a .nuget folder from a working solution to the existing one, and referenced it's content!

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
2

I had this when the csproj and sln files were in the same folder (stupid, I know). Once I moved to sln file to the folder above the csproj folder my so

2

I had this issue when I got a new computer from my company and tried to build the project after cloning it with Git. The problem was my NuGet settings did not include a remote repository from which to fetch packages. Following the FAQ at NuGet.org, I found this:

I don't see nuget.org in my list of repositories, how do I get it back?

  • Add https://api.nuget.org/v3/index.json to your list of sources, or Delete %appdata%\.nuget\NuGet.Config (Windows) or ~/.nuget/NuGet/NuGet.Config (Mac/Linux) and let NuGet re-create it.

In my case, there wasn't a %appdata%\.nuget directory on my machine at all, so from within Visual Studio, I followed these steps to fix the issue:

  1. Click on Tools > NuGet Package Manager > Package Manager Settings in the toolbar
  2. Select NuGet Package Manager > Package Sources from the list on the left
  3. Click the green + in the top-right to add a new source
  4. Set the Name: value to NuGet.org
  5. Set the Source: value to https://api.nuget.org/v3/index.json

After that, NuGet was able to download the packages it couldn't find.

To get rid of the errors in the build, I did these steps:

  1. Delete the bin and obj directories in the solution
  2. Click on Build > Rebuild Solution in the toolbar

Once the build was complete, the errors were gone.

Jonathan E. Landrum
  • 2,748
  • 4
  • 30
  • 46
1

I had the same issue when i reference the Class library into my MVC web application,

the issue was the nuget package version number mismatch between two projects.

ex: my class library had log4net of 1.2.3 but my webapp had 1.2.6

fix: just make sure both the project have the same version number referenced.

Srini
  • 708
  • 1
  • 8
  • 23
1

Editing .sln and .csproj is not always that easy or desirable. Once you get the error list you can see what projects have missing packages (also, the References node usually indicate that there are missing assemblies unless packages are source code, resources, images, or just text-based ones).

Removing and then adding the packages is not a good idea unless you use the latest version of the package. Otherwise be prepared for surprises, not always pleasant ones.

If, say, the package is EntityFramework then from NuGet gallery you get the latest version which at the time of writing this comment it is 6.1.3.

So, maybe the safest way to handle the situation is to restore the missing packages one by one. Yes, a bit painful exercise but chasing subtle bugs due to different package version maybe much more unpleasant.

Having this said, and let again EntityFramework be the missing package, you can issue the following command in the Package-Manager Console:

PM> Install-Package EntityFramework -Version 6.0.1 

This will install the correct version, that is 6.0.1, that is the one that is specified in packages.config:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="EntityFramework" version="6.0.1" targetFramework="net451" />
    </packages>
Alexander Christov
  • 9,625
  • 7
  • 43
  • 58
1

I had the same error but in my case it was not related to nuget packages at all. My solution had project that had reference to other projects that were not a part of my solution and were not built. After building them with some other solution (or I could include them into my solution as well), AND re-opening my solution in visual studio the issue was resolved.

sarh
  • 6,371
  • 4
  • 25
  • 29
1

After some time to solve this problem. Simply add a Package source with the source https://api.nuget.org/v3/index.json solve the error on my side

adriy
  • 11
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 11 '22 at 13:02
0

I created a folder named '.nuget' in solution root folder Then added file 'NuGet.Config' in this folder with following content

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
 <add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>

Then created file '.nuGet.targets' as below $(MSBuildProjectDirectory)..\

    <!-- Enable the restore command to run before builds -->
    <RestorePackages Condition="  '$(RestorePackages)' == '' ">false</RestorePackages>

    <!-- Property that enables building a package from a project -->
    <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

    <!-- Determines if package restore consent is required to restore packages -->
    <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

    <!-- Download NuGet.exe if it does not already exist -->
    <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
    <!--
        <PackageSource Include="https://nuget.org/api/v2/" />
        <PackageSource Include="https://my-nuget-source/nuget/" />
    -->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
    <!-- Windows specific commands -->
    <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
    <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
    <PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
    <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
    <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
    <PackagesConfig>packages.config</PackagesConfig>
    <PackagesDir>$(SolutionDir)packages</PackagesDir>
</PropertyGroup>

<PropertyGroup>
    <!-- NuGet command -->
    <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
    <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

    <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
    <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>

    <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

    <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
    <!-- Commands -->
    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch) -o "$(PackagesDir)"</RestoreCommand>
    <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(RestorePackages) == 'true'">
        RestorePackages;
        $(BuildDependsOn);
    </BuildDependsOn>

    <!-- Make the build depend on restore packages -->
    <BuildDependsOn Condition="$(BuildPackage) == 'true'">
        $(BuildDependsOn);
        BuildPackage;
    </BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
    <!-- Raise an error if we're unable to locate nuget.exe  -->
    <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
    <SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
    <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')"  />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(RestoreCommand)"
          Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

    <Exec Command="$(RestoreCommand)"
          LogStandardErrorAsError="true"
          Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
    <Exec Command="$(BuildCommand)" 
          Condition=" '$(OS)' != 'Windows_NT' " />

    <Exec Command="$(BuildCommand)"
          LogStandardErrorAsError="true"
          Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <OutputFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Reference Include="System.Core" />
        <Using Namespace="System" />
        <Using Namespace="System.IO" />
        <Using Namespace="System.Net" />
        <Using Namespace="Microsoft.Build.Framework" />
        <Using Namespace="Microsoft.Build.Utilities" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try {
                OutputFilename = Path.GetFullPath(OutputFilename);

                Log.LogMessage("Downloading latest version of NuGet.exe...");
                WebClient webClient = new WebClient();
                webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

                return true;
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                return false;
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

 <UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <EnvKey ParameterType="System.String" Required="true" />
        <EnvValue ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Using Namespace="System" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try {
                Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
            }
            catch  {
            }
        ]]>
        </Code>
    </Task>
</UsingTask>

Sofia Khwaja
  • 1,909
  • 3
  • 17
  • 20
0

This worked for me, don't fully understand why:

If you have a repository:

  1. Remove local folder by changing its name (e.g. at suffix __BAK)

  2. Re-get code from repository

I hadn't made changes, nor were any intended, but for some reason, re-getting from the repository (in my case ADO based on TFVC) worked.

Obviously, this won't work in all cases, and requires a working version to be stored in the repository.

However, it was probably simpler for me than any other work-around.

By the way, when I tried to re-add the missing package, the version number available from Nuget was different, and this would have caused all sorts of problems, so I abandoned this potential solution.

JosephDoggie
  • 1,514
  • 4
  • 27
  • 57