DevOps Nuget Restore vs VisDev Nuget Restore - visual-studio

I'm having trouble getting DevOps to build my project. When I do a package restore is Visual Studio I get messages such as the following:
warning NU1701: Package 'Newtonsoft.Json 6.0.5' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.1'.
But doing a Nuget restore in DevOps... This is reports as an error and it doesn't restore or do the build.
Package Newtonsoft.Json 6.0.5 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1)
It feels like I'm missing a command option someone on the DevOps restore.
Any pointers would be much appreciated.

I found a solution by using the PackageTargetFallback feature of the csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>4.8.7</Version>
<PackageTargetFallback>netstandard2.1;net472;net462</PackageTargetFallback>
</PropertyGroup>
After adding this, nuget allowed packages from the specified frameworks to be restored.

Related

Nuget problems with Azure Function 1.x and VS17

I'm having some problems with my project, I don't know where it went wrong exactly but now my Azure Function 1.x project won't build. It's critical I get it building again :-/.
This is a .NET Framework 4.6.2 project with Microsoft.NET.Sdk.Functions 1.0.35 (latest before 2.x).
I've got a couple of usings that it does not seem to able to find, and Function itself has this error:
Error CS0246 The type or namespace name 'FunctionNameAttribute' could not be found (are you missing a using directive or an assembly reference?)
So my [FunctionName] has red squiggly lines and I don't understand why, it should be in Microsoft.NET.Sdk.Functions? I've tried to clean and rebuild the project.
I've tried to reinstall my packages by command Update-Package -reinstall -Project MyVeryCriticalProject
PM> Update-Package -reinstall -Project MyVeryCriticalProject
No package updates are available from the current package source for project 'MyVeryCriticalProject'.
Executing nuget actions took 0 ms
Time Elapsed: 00:00:10.4211309
It also complains about missing namespace:
But my csproj file has these included as I have installed them in nuget:
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
I have VS2019 installed and I am going to move this project over to Azure Functions 3.x and .NET Core but right now I need this to compile :-/ Any clue?
Edit: with VS17 I've just a new Function 1.x project and right from the get-go its missing stuff:
I have .NET Framework 4.6.2 installed, and just reinstalled it.
It should be enough to just install the Microsoft.NET.Sdk.Functions package. From your error message, the problem lies with the Microsoft.NET.Sdk.Functions package. It seems that the build of the software package has failed. Please make sure you are using the latest version of the nuget package management tool.
Nuget problems with Azure Function 1.x and VS17
I think there are something broken in your VS2017 environment.
And in my side, when I created Azure Function project with Azure Function v1, it works well without any build errors. So l suggest you could try these suggestions:
Note: Update-Package -reinstall -Project MyVeryCriticalProject does not support PackageReference nuget format.
1) make sure that VS2017 has installed Azure development workload with those option tools( on the right part).
2) change your itemgroup xml node and keep the unique nuget package Microsoft.NET.Sdk.Functions and delete the others which are the dependencies of Microsoft.NET.Sdk.Functions. Like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
</ItemGroup>
Then try to uninstall Microsoft.NET.Sdk.Functions nuget package under Nuget Package Management UI and then reinstall it.
3) Then click Restore Nuget Packages by Right-click on your solution
4) disable any third party extensions under Tools-->Updates and Extensions in case some extensions causes this behavior. From your code editor, I found you used some extensions like Resharper.
5) close VS Instance, delete .vs hidden folder(a hidden folder which is in the same directory as the xxx.sln), bin ,obj folder and then restart your project again.
6) repair VS in VS Installer
In addition, you could try to create a new Azure Function in VS2017 to test whether it is the problem of your own current project.

Does nuget still require these entries in Visual Studio projects?

I've noticed that new .csproj projects without these nuget directives appear to work fine.
Are these a relic of the past that can be removed from projects?
I scanned the nuget FAQ but did not find anything related to this.
I'm running Visual Studio 2013 and also a recent build of Xamarin Studio.
<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>
Those particular targets are added if you have used the Enable NuGet Package Restore menu option. In this case these targets can be removed. Visual Studio will automatically restore NuGet packages, if you have a recent version of NuGet installed, without these extra targets. This menu option has been deprecated by the NuGet team and I believe it will be removed at some point.
Note that if you are building your project on a build server you will need to create an extra step to restore the NuGet packages, using NuGet.exe restore, that would have automatically been restored when the solution was built on the build server by MSBuild.
Also note that NuGet will add very similar targets if you add a NuGet package that includes its own MSBuild .targets file, such as Microsoft.Bcl.Build. The project will work without the EnsureNuGetPackageBuildImports Target in this case, but would need the Import element that imports the MSBuild .targets file. Leaving the EnsureNuGetPackageBuildImports target in the project does give you a perhaps more useful error message as to why a build may be failing if the NuGet package is not restored.

Restore nuget packages defined in .nuget folder

There are some nuget packages (e.g. OpenCover or ReportGenerator) installed without changing packages.config in any of the project, but there is a [Solution Dir]\.nuget\packages.config created with the package reference information.
When VS builds the solution, the packages defined in that file are not downloaded at all (even I have auto restore nuget enabled).
How can I restore them automatically?
The MSBuild based package restore, that uses NuGet.targets, which is enabled in Visual Studio by selecting Enable NuGet Package Restore, does not seem to support restoring solution level packages, which are those that are defined in the [SolutionDir]\.nuget\packages.config file.
Looking at the NuGet.targets file on build it restores the packages for the project using the project's packages.config file but does not use the solution's packages.config file.
So your options are:
Stop using the MSBuild based package restore. Visual Studio, with recent versions of NuGet, will automatically restore all packages, including all solution level packages, when you build.
Run NuGet.exe restore YourSolution.sln from the command line, or PowerShell console, or as a pre-build step, to restore all packages. NuGet.exe can be used to restore all packages on a build server if you are using one.
The MSBuild based package restore has been deprecated by the NuGet team so I would use option 1) if this is possible.

NuGet restore enabled but I still get an error

Why do I get the following build output error when I already have NuGet package restore enabled?
Restoring NuGet packages...
To prevent NuGet from downloading packages during build, open the
Visual Studio Options dialog, click on the Package Manager node and
uncheck 'Allow NuGet to download missing packages'.
All packages listed in packages.config are already installed.
MyProject.csproj: error : This project references NuGet package(s)
that are missing on this computer. Enable NuGet Package Restore to
download them.
It only happens on one project.
I am using Visual Studio 2013 and NuGet 2.8.
Make sure to upgrade to the latest version of NuGet which does package restore automatically. See this post by David Ebbo for more information: http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html
You're going to want to delete the NuGet targets (delete the .nuget folder and then manually edit your .csproj files and remove the lines that import the NuGet.targets file). This is no longer needed. When you compile your solution, NuGet will restore it.

NuGet not restoring packages on build

I have just created a workspace on a new machine, got latest of our project from TFS, enabled NuGet Restore, and I get the following (skimmed-down) output:
1>------ Rebuild All started: Project: Caching, Configuration: Debug Any CPU ------
1> Restoring NuGet packages...
1> To prevent NuGet from downloading packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages'.
1> All packages listed in packages.config are already installed.
1> Caching Framework -> C:\MyProjLocation\Caching\bin\Debug\Caching.dll
2>------ Rebuild All started: Project: Library, Configuration: Debug Any CPU ------
2>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "LumenWorks.Framework.IO". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
2>C:\MyProjLocation\Library\SomeClass.cs(2,7,2,17): error CS0246: The type or namespace name 'LumenWorks' could not be found (are you missing a using directive or an assembly reference?)
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
None of the NuGet packages were restored (there are over 10 - I removed them from the above output for readability sake).
It looks like NuGet is not even trying to restore the packages for the second project (Library).
I have already tried moving the NuGet targets import below the CSharp targets import in the Library.csproj file, as mentioned here, but it's still not working:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
I am running the latest version of NuGet (2.7.41101.371) on Visual Studio 2013.
Edit: The packages.config exists and the NuGet Package Manager has the Library project ticked with the correct packages.
I had to go into Source Control and delete all of the files in the packages folder (except repositories.config) before NuGet would restore the missing packages. The idea is that you are using package restore rather than checking your packages in to source control. If it sees the packages in source control, it won't download them.
Have you deleted the NuGet.targets file from disk too?
If the NuGet.targets file is there, Visual Studio / NuGet.exe will try to do the MSBuild package restore.
See this doc for more info.
I have the same issue, also on the local machine. Although both Package Manager Console and nuget.exe restore MySolution.sln report that everything is installed, there is no packages folder to be found in the solution directory and no references to packages are being resolved.
I checked all project files and they expect packages to be placed in ..\packages folder, the same folder where the solution file itself is located.
The way I made it work is to run:
nuget.exe restore MySolution.sln -PackagesDirectory packages
This forced nuget.exe to download all packages to the specified folder and all references were restored.
Remember that from NuGet 2.7, the targets file is not supported, msbuild suppose to use some integrated way of restoring packages but it fails very often.
In fact, for my own work I prefer using Paket, which always work, when you get used to it. It also supports target files and nice way to create NuGet packages.
I'm not sure about the science behind this, but it worked for me just now after trying to build a freshly-downloaded Visual Studio project, and getting several MSB3245 warnings followed by a build failure due to missing references:
In Visual Studio, right-click on the project with the missing NuGet references, and select "Manage NuGet Packages..."
The Manage NuGet Packages dialog will open. I also saw a message quickly display and then auto-close, with text along the lines of "Restoring NuGet Packages..."
Close the Manage NuGet Packages dialog (without actually changing anything on the dialog), and retry the build.
Edit: Going back in my TimeSnapper auto-screenshot history (no affiliation with that tool -- I'm just a fan), it looks like there was also a message displayed at the top of the Manage NuGet Packages, along with a "Restore" button: "Some NuGet Packages are missing from this solution. Click to restore from your online package sources."
Although the "Restore" apparently automatically happened for me, clicking that button manually might also do the trick to resolve the missing packages issue.
Had to uninstall nuget packages and do a refresh install in order to make it work properly. This might help some of you facing the same issue
I was having the same problem. In my case, it was a NuGet.Configin the parent directory that was setting <add key="repositorypath" value="C:\CxCache" />. So the nuget restore was copying the packages to a folder I didn't know of. See NuGet Configuration Inheritance.
Deleting the NuGet.Config in the parent directory solved the problem.
On Mac; I commited the code to git and deleted everything (main folder), then downloaded it again. Worked afterwards.

Resources