Visual Studio 2017: how to include NuGet packages in solution folder - visual-studio-2017

To date, using Visual Studio 2015, NuGet packagas are restored into a solution-specific "packages" folder, which I check in to source control. Our TFS Build server does not have Internet access, so it needs these local copies to successfully build.
I am now trying out Visual Studio 2017, with a .NETStandard Class library, and it no longer makes a local copy of NuGet packages - instead they appear to be stored in %HOMEDRIVE%%HOMEPATH%.nuget\packages. This means they are no longer available on the Build server.
How can I get NuGet to store packages locally?

You should be able to define a RestorePackagesPath property in your .NET Standard (SDK style) project which defines where the NuGet packages should be restored to.
You can use this to tell NuGet to not use the .nuget\packages directory but instead use some other directory relative to your project.
<PropertyGroup>
<RestorePackagesPath>..\path\to\MyPackages</RestorePackagesPath>
</PropertyGroup>
Looking at the NuGet documentation they mention three possible approaches when using a project that uses PackageReferences.
Set the NUGET_PACKAGES environment variable to a different folder.
Create a NuGet.Config file that sets globalPackagesFolder to a different folder.
Specify a different folder with the RestorePackagesPath MSBuild property.

Related

Building and running unit tests with Visual Studio build tools

I am adding unit tests to an existing C++ Visual Studio projects, using the Google Test adapter.
It's all running fine on my computer with Visual Studio 2019, but when I try to run them on the build server I get the following error
error : 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 ..\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets.
However, we're not using NuGet for package management. I tried installing it but complained about missing folders. This is not a .NET project, so I think that's a red herring.
I was able to install the Google Test adapter on my computer using the Visual Studio Installer, but it does not show up as a part of the VS Build Tools on the build server.
Running msbuild -t:restore does not help, it just reports "nothing to do."
I don't understand why the Google Test adapter isn't available for VS Build Tools, since it seems to be required in order to build the unit tests. Does anyone know why it doesn't work? What's the best practice for handling this?
Thanks!
The problem is that your c++ project has missed the content of googletest nuget package. So the solution is to restore the whole nuget package in your c++ project.
Update 1
First of all, take a brand new backed up project and restore it to when the problem started.
Besides, msbuild -t:restore command applies to projects with PackageReference nuget management format.
Since your c++ project used packages.config nuget management format, msbuild -t:restore will not work. See this official document.Instead, you should use nuget restore command.
This command works for your current project and running this command will restore the nuget packages and then you will never face the issue.
Before using it, you should download nuget.exe CLI and config its path into System Environment Variable PATH so that CMD can invoke nuget.
The steps about configing nuget.exe, you can refer to this link.
Steps
1) delete packages folder under the solution folder
2)Then, open build tool, run:
nuget restore xxx\xxx\xxx.sln(the full path of solution file containing the c++ project and the unit test project)
Then, you can build the project with the command. And I hope the error will disappear.

Missing file NuGet.targets on existing project

The scenario is:
1. Created a NEW solution
2. ENABLED download missing packages
3. Add projects to solution that already exists and depends on nuget packages
4. Building the solution generates an error:
... The missing file is <solution folder>\.nuget\NuGet.targets.
Why? Have missing something?
I'm using Visual Studio 2017 Pro on Windows 10
All my searches answer about the scenario where create a new solution and adds a new project that depends on nuget package, that's ok, but when have an existing project, nothing.
<solution folder>\.nuget\NuGet.targets is a file that NuGet's Visual Studio extension used to add in Visual Studio 2010, 2012 and 2013 when you right click on the solution and select "Enable NuGet Package Restore". It would then add an import in all of your project files to import the targets file. I was recently investigating a customer issue and as part of that investigation I found it made the following changes. Near the top of the csproj, it adds something like this:
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
and near the end of the csproj it adds somthing like this:
<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>
So, the problem is that you're creating a new solution, but using existing project files that have been modified in this way. One option is to edit your csrpoj files and remove these changes. Another option is to create new projects, in addition to the new solution, and then copy all your code and content files.
The only disadvantage is if you build on a CI server and use packages.config, your build script needs to run nuget.exe restore, whereas projects that use NuGet.targets could just build the solution and msbuild would execute nuget restore as needed. One advantage of no longer using NuGet.targets is that restoring the whole solution is faster than restoring project by project. Visual Studio automatically restores packages on build, even in VS2010, VS2012 and VS2013, so personally I discourage the use of using this feature, even if you use those old versions of Visual Studio. The benefit of reducing your build script by one step is not worth the issues it brings, in my opinion.

Unable to find nuget local source error occurred while restoring nuget packages the local source doesn't exist

I've been given a visual studio 2017 solution. When I open it fails as with the error:
Error occurred while restoring NuGet packages: The local source '\\network-location\' doesn't exist.
I can manually acquire a copy of these packages but I don’t know where this original path is configured and how I would go about changing it to the new location.
Any suggestions as to where I should look please?
The package sources can be found in the Visual Studio options (Tools -> Options) under NuGet Package Manager -> Package Sources or directly by clicking on the according icon in the NuGet dialog (context menu of a solution/project -> Manage NuGet Packages...):
Your local package source should then be listed in the following dialog:
The package source may, however, be solution or project specific and may therefore be specified in a NuGet.config file in the solution directory. Beginning with NuGet 3.4, Visual Studio looks in the project's directory or "or any folder up to the drive root", according to the NuGet.config reference. Up to NuGet 3.3, also subdirectories with the name .nuget where searched for NuGet.config files.
The file containing your local package source must be changed in order to restore the correct packages.
Nothing of the proposed solutions above did it for me. And, honestly, I really don't like, what Microsoft is doing for some time now: each time there's another surprise when installing an update. :-(
Analysis:
Error occurred while restoring NuGet packages: The local source 'C:\Microsoft\Xamarin\NuGet' doesn't exist.
Obviously NuGet is trying to restore from C:\\Microsoft\\Xamarin\\NuGet for s solution that has nothing to do with Xamarin. The solution compiled many times before and even getting it back from GIT did not change anything. So, the problem is not my solution. It is something more global.
By the way: I DONT WANT TO USE THIS FOLDER, cause I don't use Xamarin!
I found the following reference in the *.csproj.nuget.dgspec.json files in any obj folder of my solution. All these json files pointed to this Xamarin folder:
"fallbackFolders": {
"C:\\Microsoft\\Xamarin\\NuGet": {},
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
Question: How does VS know about the fallbackFolders?
I did not find any hint in any of my Visual Studio / NuGet configuration settings, as proposed above. I deleted all objfolder but each time when trying to compile or restore my packages, the reference came back.
The SOLUTION for me was :
delete all objfolders in your solution (as mentioned above)
delete c:\Program Files (x86)\NuGet\Config\Xamarin.Offline.config
There you will find the following:
<fallbackPackageFolders>
<add key="Xamarin Offline Packages" value="C:\Microsoft\Xamarin\NuGet\"/>
</fallbackPackageFolders>
This file was introduced on my machine one week ago, and I think it came with the latest VS update (16.2). I did not go away with the latest update (16.2.1) from today.
You will encounter the same issue in Visual Studio 2019. I have the same issue, I thought it was a conflict on Visual Studio 2017 but it wasn't.
The fix is you have to remove the source that is causing the issue.
You can remove it via Tools -> NuGet Package Manager -> Package Manager Settings
In my case i have getting this error:
Error occurred while restoring NuGet packages: The local source 'C:\Microsoft\Xamarin\NuGet' doesn't exist.
I have accidentally deleted 'C:\Microsoft\Xamarin\' folder.
After this VS2019 was not able to restore packages.
I just manually created 'C:\Microsoft\Xamarin\NuGet' folder.
Restarted VS, cleaned solution and everything goes well after this.
All answers are relevant however they are not complete, wasted 30 mins on this.
Below worked fine:
1 . Clear cache & remove issue folders:
Tools > NuGet Package Manager > Package Manager Settings
> NuGet Package Manager > General > Clear All NuGet Cache(s)
Tools > NuGet Package Manager > Package Manager Settings
> NuGet Package Manager > Package Sources
> Available package sources
> {uncheck the folder that is giving error}
> OK
2 . Restore NuGet Packages on VS Solution
Right-click on Visual studio solution (not project)
> Restore NuGet Packages
3 . Build & run project
Hope that helps.
I had this error in different situation. I checked contents of Nuget.config in my solution folder and under <packageSources> node it had an entry pointing to ./nuget. This folder didn't exist in the solution directory, so I just created an empty folder with this name and the solution compiled without any problems.
In My case the only way to solve it was to clean up all the NuGet cache(s).
You can find it at Tools -> NuGet Package Manager -> General
Clear All NuGet Cache(s) print
but I don’t know where this original path is configured and how I would go about changing it to the new location.
To resolve this error, please search the file NuGet.config in the given solution, then edit it with notepad, you will find following setting in that file:
<packageSources>
<add key="LocalServerName" value="\\network-location\" />
</packageSources>
You could change the value to the new location.
Same situation:
Error occurred while restoring NuGet packages: The local source 'C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages' doesn't exist.
My solution is re-creating the folder.

Can't install local NuGet package

Details
Using Visual Studio 2017
Built NuGet package with NuGet Package Explorer
Placed .nupkg file in local folder on disk
Added folder to Package Sources in Visual Studio
I attempt to install the package using the Visual Studio GUI (Tools > NuGet Package Manager > Manage NuGet Packages for Solution...).
Problem
My package shows up in the list in the GUI, but when I click install, an error message says it can't find the package in the folder I put the .nupkg file in:
Package 'TDDeviceIntegration 1.0.0' is not found in the following primary source(s): 'C:\Users\j.smith\Documents\Visual Studio 2017\LocalNugetRepository\'. Please verify all your online package sources are available (OR) package id, version are specified correctly.
What I've tried
Putting the NuGet Package I've built in several different local folder locations and adding those to the Package Sources, all with the same result (it can't find the package I JUST put there).
I've restarted Visual Studio several times.
I've restarted my computer.
I've cleared my NuGet cache(s) from Visual Studio
Question
How do I diagnose this? How do I fix this? I just want to make sure that the NuGet package works locally before I give it to the rest of the team.
Thanks in advance!
How do I diagnose this? How do I fix this? I just want to make sure that the NuGet package works locally before I give it to the rest of the team.
Just as #orhtej2 comment, you should:
you rename it to TDDeviceIntegration.1.0.0.nupkg? (dot instead of
space between package name and version).
Additional, some info about why dot is really the only allowed package name-version separator.
That because namespace of nuget package follows a pattern similar to namespaces in .NET, using dot notation instead of hyphens.
You can get the source from following document:
Choosing a unique package identifier and setting the version number
Hope this helps.
According to this link from the NuGet GitHub Repository, you can possibly encounter this error when the version of your package is not "normalized", i.e. it's not made up of 4 digits.
Yours has 3 digits, so...
(and the last digit might have to be 0).

Can the Visual Studio 2012 C++ Redistributable be installed automatically with a TFS Build?

I have an ASP.Net project that's using the ClearScript.V8 library which relies on the Visual Studio 2012 C++ Redistributable to work. I'm faced with the prospect of having to install this on 50+ machines and I'm curious if I can get TFS to do it for me.
I realize this straddles somewhere between Stack Overflow and Server Fault so I'm asking on both sites.
Assuming that the binaries for ClearScript.V8 is included in your project (either as files copied to the bin folder or as a NuGet package) you should be able to also include the necessary Visual C++ redistributables files as binary files in your project. They will then be stored in TFS.
If you prefer NuGet packages you can to set up a local NuGet repository within your organization and create a NuGet package with the necessary DLLs which you then can reference from your project. If NuGet package restore is configured for your solution the package will be downloaded before the solution is built. In VS2012 you will have to make some customizations for this to happen on a Team Foundation Build server.
If you are unsure which DLLs you need to include you can use a tool like Dependency Walker to discover which DLL's are required, or you can debug the application and see which DLLs are loaded in the modules window. Microsoft has some information which DLLs to redistribute.
If you are having problems with conflicting versions of a redistributable DLL getting loaded you can create a side-by-side manifest to ensure that the correct DLL version is loaded into your application. However, as your application is hosted within ASP.NET this is somewhat tricky because w3wp.exe is executed by IIS outside your direct control. You will have to create an activation context to configure the manifest used by your application.
If you need to have things installed on your server for a build I would recommend that you use Chocolatey.
chocolatey.org/
This is an implementation of NuGet in PowerShell to explicitly solve the issue that you face. You can create a powershell file that installs all of the bits that you need and easily include it as a pre build step in TFS 2013. You can use the PowerShell activity from the TFS Community Build Tools to call the PowerShell in TFS 2010 or 2012.