How can I fix missing nuget references after moving project in Visual Studio 2015 - visual-studio

I had a project structure like this:
WebApp
-- WebApp
-- WebApp.sln
WebApp.Tests
-- WebApp.Tests.csproj
I moved WebApp.Tests into WebApp using a move (simple click & drag into the WebApp folder). I edited WebApp.sln to fix the project reference so that it will load.
When I build, Nuget complains that packages are missing and to do a restore.
I downloaded and used nuget.exe restore on my solution and it reported everything was there.
Based on other Stack Overflow answers, I have tried the following:
Edit the test project reference hint paths. I changed from ..\WebApp\packages\PACKAGE to ..\packages\PACKAGE
Reload Visual Studio (multiple times)
Delete contents of packages folder and bin/obj folders of the projects
Use the package manager console to reinstall packages on the Test Project
All of these failed to fix the problem. When I used the package manager to try to reinstall the packages with the command, it gave me the same error that project building does - I must restore the packages first.
Is there any quick way to fix my project? I really don't want to go through each package and uninstall/reinstall manually. Also, how could I have prevented this problem in the first place? Is there a built-in way to move project locations?

There was XML similar to this at the end of my project file:
<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('..\WebApp\packages\SPECIFICPACKAGE')" Text="$([System.String]::Format('$(ErrorText)', '..\WebApp\packages\SPECIFICPACKAGE'))" />
</Target>
By changing the ..\Webapp\packages to ..\packages like the rest of the file, my solution compiles just fine now.

I had the same issue where I physically moved a project folder into the src folder and got the missing packages error. My fix was to open up the .csproj file in notepad and replace every instance of
this:
..\packages\
with this:
..\..\packages\
It solved the issue...

Related

Including Nuget Content Files to Target Projects Publish on .net Core Project

before i start i must state that i pretty new to this nuget package things, so come basic at me.
My app has a core project to carry some .dll and .sql files and an api project. I have made the core project a nuget package with dotnet pack and automated it on github action so it creates a new version at all prs and pushes it to my private nuget server and it is all fine till here.
When i install this core package to my api project, there is no problem either. It installs successfully and my sql files shows up on the target project as i expected(but as linked sources).
But when i try to publish this project with dotnet pack, the .sql files are not copied to output folder. I looked to the properties of the files and saw that their copy to output directory prop has been reset to do not copy and the file path was absolute.
My goal is to build and deploy this api with github actions so absolute paths are not acceptable and besides i don't want to arrange something in the target project manually.
I'm packing the core project with dotnet pack
I don't have any
.nuspec file
package config xml
nuget config
package metadata
or other things like than and i don't know how to use them.
I have tried adding to csproj file <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> and <RestoreProjectStyle>PackageReference</RestoreProjectStyle> things and they did not work.
I have tried adding to csproj file
and it did not work too.
Can anyone help me?
The only thing you need to do is that you should write this node <PackageCopyToOutput>true</PackageCopyToOutput> to every content file of the nuget net core project:
<ItemGroup>
<Content Include="Queries\Account\CheckAccountRef.sql">
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
.....
</ItemGroup>
Then, re-pack your nuget project with dotnet pack and then reinstall this new version into your main project. Before you install, you should delete the old caches under C:\Users\xxx\.nuget\packages.
There is a similar issue about this.
You can use .nuspec file to configure all your dependencies. Please refer https://learn.microsoft.com/en-us/nuget/reference/nuspec

Where do I put files in a nuget package so that they will be restored?

I'm working on setting up nuget packages for dacpacs for some large database schemas we have. Here's a sample of the metadata:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>xxxx.DACPAC</id>
<version>1.0.0</version>
<title>xxxxDatabase dacpac</title>
<authors>xxxxxxxxx</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>xxx-database.git</projectUrl>
<iconUrl>http://github.com/contoso/UsefulStuff/nuget_icon.png</iconUrl>
<description>DACPAC for xxxx Database</description>
<releaseNotes>Initial release</releaseNotes>
<copyright>Copyright ©2019 xxxx</copyright>
<tags>dacpac database xxxxxx</tags>
</metadata>
</package>
The .nupkg looks like this
and the content directory like this:
This works fine. I then create a solution with an ssdt project and a skeleton c# project. I use the c# project to hold the dacpacs. I can nuget them, then in the ssdt project add database refs pointing to the dacpacs. The dacpacs are not in git (part of my goal is to keep them out).
I commit the repo then clone it to an empty directory. Good so far.
Next, i use nuget to restore the packages in visual studio. However the dacpacs are not restored! I think that's because they are in the "content" directory in the package and from what I can find, those files are not restored by nuget.
So, my question is, where to put these files so that they will be restored? Which nupkg directory? Content doesn't work. Tried tools to no avail. Thought I should ask before another blind attempt.
So, my question is, where to put these files so that they will be
restored? Which nupkg directory? Content doesn't work. Tried tools to
no avail. Thought I should ask before another blind attempt.
Nuget Restore is to download the mssing packages. And the content in xx.nupkg will only be added to projects at install time of one package. Nuget restore can't get the content files back, that's not restore's job. So it has nothing to do with which directory we put the files in, it's just one not supported job for nuget restore...
As a workaround: You can use Update-Package -reinstall in Package Manager Console to reinstall the package. And the content files will be added back to project. More details see this document. Hope it helps :)

NuGet only imports my .targets file into the top project

I have multiple projects in my solution, they all have the same NuGet package installed. I installed this NuGet package via the 'Manage Nuget Packages for Solution' option in my menu.
Now this particular NuGet Package has a .targets file in my build folder that imports some files into the build.
When I rebuild my solution these files are only copied for the top-level project. The other projects don't get the files copied into their build folder. This is causing me headaches because all the projects need these files in their build folder.
When I look into my .csproject files I see the following difference:
Project 1
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\NuGetTester.1.0.1\build\NuGetTester.targets" Condition="Exists('..\packages\NuGetTester.1.0.1\build\NuGetTester.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\NuGetTester.1.0.1\build\NuGetTester.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NuGetTester.1.0.1\build\NuGetTester.targets'))" />
</Target>
</Project>
Project 2
The 2nd project does not have these lines.
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
When I include these lines in the 2nd project everything seem to work.
Can someone explain why this only happens for the first project? Can I somehow force that every project in the solution that installs this NuGet will also include these lines? I can't force my users to include this manually.
Edit, this is the .targets file.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)\Versions\*.sql">
<Link>App_Data\Versions\%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Thanks
Can someone explain why this only happens for the first project? Can I somehow force that every project in the solution that installs this NuGet will also include these lines?
Without install nuget package log, I could not give the reason that why this only happens for the first project. But you should make sure you have installed the nuget package to all projects when you installed the NuGet package via the 'Manage Nuget Packages for Solution' option:
If all check boxes are checked, you still have this issue, you should install individually that package to the problematic project, then check the install log.
Just in case, there is a workaround for this issue. You can create a MSBuild project file named "Directory.Build.props" in the same folder as your solution with you content in it.
It will be primitive imported into all projects in the directory hierarchy.
Check this document for more details.
Hope this helps.
It appears, to my knowledge, that the order of the projects was not the problem.
The problem only appears when I have no files in my target (net46) folder in the build folder.
Once I placed a random file into the .NET targetted folder the .targets file was imported into the project. Or if I removed all the .NET target folders from the build it works as usual.
So just to make a summary.
Build folder with no .NET framework target folders -> It works
Build folder with an empty .NET framework target folder -> Does not work
Build folder with a .NET framework target folder filled with one or many files -> It works

How do I set Visual Studio to build a NuGet package?

How can I get Visual Studio to build a NuGet package for my library component on build?
I’m using a Portable Class Library as the example project.
Ensure the NuGet.exe file in .nuget folder is latest.
Default values come from AssemblyInfo.cs, so clean that up.
Add a NuGet package reference if you do not reference any, preferably something simple like JSON.NET. Often, PCL projects have no external dependencies, in which case no NuGet refs and without any NuGet refs, the required MSBuild config won't get set properly, so we need to add a 'dummy'.
Enable NuGet Package Restore.
Edit the NuGet.targets file and ensure BuildPackage is true.
<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">true</BuildPackage>
Edit your .csproj file and add this to the first, default PropertyGroup
<BuildPackage>true</BuildPackage>
Rebuild your project and then search in the Output for nupkg and confirm creation and location of the package file.
Remove the dummy NuGet package reference and build again and check the Output.
To further customize the package creation, you can stick a MyProjectName.nuspec file next to your .csproj file.
See http://docs.nuget.org/docs/reference/nuspec-reference for more on the NuSpec format. You can also pull one from an existing package (its just a zip file) and have a nose around, see how it was done.
Add a post-build event like this:
$(SolutionDir)\.nuget\nuget.exe pack "$(MSBuildProjectFullPath)" -p Configuration=Release -o "$(MSBuildProjectDirectory)\bin\Release" -symbols"
And download and place nuget.exe in the .nuget folder alongside your solution file.
You can use nuget update -self to keep the .exe fresh.
Note
nuget.exe pack has a bug currently where it'll see a packages.config file and try to look for the packages it mentions in your solution but it fails to find them if the packages folder is in a strange place, e.g. if your solution file isn't a level up from the project.
To workaround this, add another post build event to copy the packages folder into the project folder.
The repositorypath config setting seems to do nothing for me.
See GitHub reports:
https://github.com/NuGet/Home/issues/5316
So funny. I was having problems with my usual way of auto-building a package on build when I arrived at this new way. So I looked for a suitable SO question to answer with my new post-build method when I came across my own question here!

remove nuget package restore from solution

I added the recent nuget package restore feature to a solution using 'Enable NuGet Package Restore':
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
However it broke my build server and I didn't have the time to fix it, so I wanted to remove it. There's no option for that as far as I know, so I removed the following line manually from all my *.csproj files:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
The problem now is that every time my *.csproj files are checked out or open my solution, the line is automatically added again, breaking my build if I accidentally check it in :(
Any ideas how I can remove it permanently?
UPDATE: despite the answer below it still keeps coming back when opening the solution, anyone with the same problem?
I didn't look very well, there's another property added to the project files:
<RestorePackages>true</RestorePackages>
Just have to remove this as well as all these lines manually from all *.csproj files:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
UPDATE:
Turns out it's a persistent little bugger, if you're manually editing your project files, make sure to close the solution and delete all the lines from the project at once, otherwise they're just added again once the project reloads...
UPDATE2:
Delete the .nuget folder from the solution root too
UPDATE3:
A later version of NuGet adds another section that you need to remove:
<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>
Update4
Inside the NuGet.Targets located in the .nuget folder, there is another section that gets added to new projects... switch it to false.
<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'false'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>
To disable Nuget Package Restore:
Delete .nuget folder
Remove specific lines from all .csproj files
Lines to remove:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<RestorePackages>true</RestorePackages>
Note: Make sure you do all changes in one go before reloading solution or else it will add them back.
This is based on the following article:
http://bartwullems.blogspot.no/2012/08/disable-nuget-package-restore.html
Also, you might want to double-check that this option is disabled:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
Isn't it this setting here?
Options... -> Nuget Package Manager -> [uncheck] Allow Nuget to download missing packages
I'm using Visual Studio Professional + Resharper 8.2
Solutions currently using MSBuild-Integrated package restore can be migrated to Automatic Package Restore. From what I understand, this should help those who are encountering CI build issues. (Please correct me if I am mistaken).
Please refer to the document on the nuget website: Migrating MSBuild-Integrated solutions to use Automatic Package Restore at http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
There is information there for converting with and without TFS.
David Ebbo also posted some information at http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html
We actually have a blog post about it and at the end of the post a powershell script was mentioned to help with the migration.
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
NuGet has a blog post about migrating to automatic package restore:
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
There is a powershell script referenced in the blog post that will take care of removing the necessary lines automatically (and recursively):
https://github.com/owen2/AutomaticPackageRestoreMigrationScript/blob/master/migrateToAutomaticPackageRestore.ps1
Offering a Disable NuGet Package Restore option directly is marked as Won't Fix:
https://nuget.codeplex.com/workitem/1883
I had the same issue. What I ended up doing:
1) go into each project .csproj file in the solution, open it in notepad then removed the portion of the xml and saved.
2)Then I removed the all of the package.config files in the entire solution.
3)Then I had to remove the .nuget and package folders.
At this point, I had a completely NuGet free solution.
4)Then I manually referenced any needed DLLs and hit compile and the solution ran like a champ without the NuGet packages being needed.
I followed the accepted solution to no avail using 2012. This did work though,
Completely close the VS
Update the <RestorePackages>true</RestorePackages> to <RestorePackages>false</RestorePackages> and delete the <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> line
Also renamed the nuget.exe to nuget.exe.NotExe
For anyone still needing to clean up a project using the old style NuGet package restore, the IFix tool available here automates the process.
Just run the installer (IFix will be added to PATH) and then run the following:
IFix nugetrestore --fix
You can run it in check mode first to see what it will clean up:
IFix nugetrestore --check
Go to your solution directory where you have [$(SolutionDir)\.nuget\nuget.targets]
.nuget folder and nuget.targets file under it delete the folder,
and change remove lines from your csproj for once last time.
The problem won't come back to bug you again.
Remove the packages.config file within your solution.
I accidentally enabled this "package restore" option while opening my project in VS2012 RC and started getting errors that looked something like:
"Error 1 Unable to locate 'C:\FolderX\SomeProject.nuget\nuget.exe'"
To fix the error I followed the above instructions, opened open each project file in notepad and removed that RestorePackage line.
I was able to resolve this issue by taking these steps:
1) make sure you take a backup of all your current checked-out files changes.
2) physically delete the solution folder from your C:\ (path that is mapped to TFS).
3) get latest from TFS for your solution.
4) copy (if any) your changes from the backup you took in step-1.
hope that helps !
I ran into the exact same problem and tried to remove all .nuget and RestorePackage tags from the project files but one project just wouldn't reload not matter how thoroughly I examined it for .nuget and RestorePackages tags. I guess there's some hidden references to this somewhere.
In the end it was easier to just copy the files and create a new project and import it to the solution.
Nuget sucks. Just remove nugets and remove or comment package elements from packages.config in root directory of the projects where this is a problem. Use direct references into some lib folder instead.
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--<package id="EntityFramework" version="6.0.2" targetFramework="net45" />-->
</packages>

Resources