"No executable" when using dotnet add - nuget

I'm trying to add a Nuget .core package using
dotnet add package [package name]
but get "No executable found matching command dotnet-add". I've installed Nuget and can use "dotnet" to create applications, restore, build, run, etc.
What am I doing wrong? Thanks.

The dotnet add family of verbs was released in version 1.0.0 of the dotnet cli tooling and is not part of the 1.0.0-preview2-* range of versions previously used for project.json projects.
This means that dotnet add verbs are only available for msbuild based projects (=> .csproj).

Related

Cake NuGetRestore Could not locate nuget.exe

I have created simple .NET5 console application which can be successfully run using "dotnet restore" and "dotnet run". Application is using one package Newtonsoft.Json.
I want to restore NuGet packages using NuGetRestore() but in result I get "Could not locate nuget.exe".
I am using Cake .NET Tool in version 1.1.0 and default configuration.
As far as I remember, previously when I was using Cake before version 1.0.0 nuget.exe was downloaded to 'tools' folder.
What am I missing? How to make sure nuget.exe is downloaded/provided by Cake?
In order for the NuGet tool to be downloaded to the tools folder, you need to include the NuGet.CommandLine package in your Cake build script. e.g.
#tool nuget:?package=NuGet.CommandLine&version=5.9.1
Related info:
Example build.cake file referencing NuGet.CommandLine
NuGet.exe related documentation
Cake itself never did download nuget.exe. The example bootstrapper for Cake Runner for .NET Framework does this in certain conditions. Example bootstrapper for Cake .NET Tool does not download nuget.exe and you need to make yourself sure to have it available, either by updating the bootstrapper, using the tool prepocessor directive in your build script, or any other mechanism outside of Cake.

How to retrieve NuGet package with all dependencies to local folder?

In our company, we have an on-premise NuGet server. Now I'd like to add the package Fluent Assertions. I can easily get it by executing the following command:
nuget install FluentAssertions -OutputDirectory C:\temp\NuGet
But in C:\temp\NuGet, I only find FluentAssertions, but not its dependencies.
Is there any one-liner that download's them as well into C:\temp\NuGet? Or do I have to do this manually?
The dependencies are framework-specific. You'll have to chose one framework you want the depencies for using the appropriate option
Nuget-Cli-Reference (install)

Assets file project.assets.json not found. Run a NuGet package restore

I'm trying to use nopCommerce(Which is written in .NET Core) but when I want to run the project I face 52 Errors telling me Run a nuget package restore
Assets file ~\obj\project.assets.json' not found. Run a NuGet package restore to generate this file. Nop.Web.MVC.Testsote
when I use the right click on the solution and selecting Restore Nuget Packages I get this message:
All packages are already installed and there is nothing to restore.
but those 52 Errors are still there and in Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution there is nothing installed on the solution,also I Recently updated my VS2017 to 15.5.4
To fix this error from Tools > NuGet Package Manager > Package Manager Console simply run:
dotnet restore
The error occurs because the dotnet cli does not create the all of the required files initially. Doing dotnet restore adds the required files.
In my case the error was the GIT repository. It had spaces in the name, making my project unable to restore
If this is your issue, just rename the GIT repository when you clone
git clone http://Your%20Project%20With%20Spaces newprojectname
In case when 'dotnet restore' not works, following steps may help:
Visual Studio >> Tools >> Options >> Nuget Manager >> Package Sources
Unchecked any third party package sources.
Rebuild solution.
To those with the same issue as me in Azure DevOps / VSTS environment encountering a similar message:
C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5): Error NETSDK1004: Assets file '...\obj\project.assets.json' not found. Run a NuGet package restore to generate this file
Add /t:Restore to your MSBuild Arguments in Build Solution.
Closing and re-opening Visual Studio solved this issue for me, once I had made sure the NuGet packages had been restored as per other answers posted here.
Edit: Sometimes just trying to build again fixes the problem.
For me when i did - dotnet restore still error was occurring.
I went to
1 Tool -> NuGet Package Maneger -> Package Manager settings -> click on "Clear all NuGet Cache(s)"
2 dotnet restore
resolved issues.
In visual studio 2017 please do following steps:
1) select Tool=>Options=>NuGet Package Manager=> Package Sources then uncheck Microsoft Visual Studio Offline Packages Option.
2) now open Tool=>NuGet Package Maneger=>Package Manager Console.
3) execute command in PM>dotnet restore.
Hope its working...
It was mentioned earlier but I just wanted to re-emphasize the importance of not have space anywhere in your pathing! This is what was getting me. You've been warned.
If this error occurs as part of a build in Azure DevOps (TFS) and your build already has a NuGet restore task, this error may indicate the NuGet restore task was not able to restore all packages, especially if you use a custom package source (such as an internal NuGet server). Adding /t:Restore;Build to the MSBuild Arguments seems to be one way to resolve the error, but this asks MSBuild to perform an additional NuGet restore operation. I believe this succeeds because MSBuild uses the custom package source configured in Visual Studio. A preferable solution is to fix the NuGet restore task.
To configure a custom package source for the NuGet restore task:
Create a NuGet.config file that lists all of the package sources (Microsoft Visual Studio Offline Packages, nuget.org, and your custom package source) and add it to source control.
In the Nuget restore task under Feeds to use: select the option Feeds in my NuGet.config.
Provide the path to NuGet.config.
Remove the /t:Restore;Build option from the MSBuild task.
Additional information is available here.
For me I upgraded NuGet.exe from 3.4 to 4.9 because 3.4 doesn't understand how to restore packages for .NET Core.
For details please see dotnet restore vs. nuget restore with teamcity
You will get required packages from "https://api.nuget.org/v3/index.json". Add this in Package Resources. Also make sure other packages are unchecked for time being. And Click Restore Nuget Package on Solution Explorer
Select Tools > NuGet Package Manager > Package Manager Console
And then Run:
dotnet restore <project or solution name>
Solved by adding /t:Restore;Build to MSBuild Arguments
Nothing above worked for me. But simply deleting all 'bin' and 'obj' folders did the trick.
You can go for : Tools > NuGet Package Manager > Package Manager Console
And then Run:
dotnet restore
little late to the answer but seems this will add value. Looking at the error - it seems to occur in CI/CD pipeline.
Just running "dotnet build" will be sufficient enough.
dotnet build
dotnet build runs the "restore" by default.
I lost several hours on this error in Azure DevOps when I set the 'Visual Studio Build' task in a build pipeline to build an individual project in my solution, rather than the whole solution.
Doing that means that DevOps either doesn't build any (or possibly some, I'm not sure which) of the projects referenced by the project you've targeted for the build, and therefore those projects won't have their project.json.asset files generated, which then causes this issue.
The solution for me was to swap from using the VS Build task to the MSBuild task. Using the MSBuild task for an individual project correctly builds any projects referenced by the project you're building and eliminates this error.
This problem happening when your build tool is not set to do restore on projects set to use PackageReference vs packages.config and mostly affect Net Core and Netstandard new style projects.
When you open Visual Studio and build, it resolves this for you. But if you use automation, CLI tools, you see this issue.
Many solutions are offered here. But all you need to remember, you need to force restore. In some instances you use dotnet restore before build. If you build using MsBuild just add /t:Restore switch to your command.
Bottom line, you need to see why restoring can't be activated. Either bad nuget source or missing restore action, or outdated nuget.exe, or all of the above.
If simply restoring NuGet packages does not work make sure in Tools -> Options -> NuGet Package Manager -> General under Package Restore that the "Allow NuGet to download missing packages" is checked.
Then Restore NuGet Packages again OR just REBUILD after deleting obj and bin folders.
run your VS as administrator after that in package manager console run dotnet restore.
When using VSTS, check your global.json file. I had entered the sdk version as just "2.2" which caused a parse error (but not when building locally). Adding the full version, "2.2.104" solved the problem.
I have same problem and dotnet resotre not work for me. I Install this component:
and the run dotnet resotre and the problem has solved.
In my case, I had the following added to my *.csproj files to fully remove obj and bin folders on 'Clean'. Apparently, it was the culprit. Got rid of that and viola, all started to work again. Now I'm using the "Clean Bin" extension instead. Hope this might help anyone who is running into this issue, and none of the mentioned fixes works.
<Target Name="SuperClean" AfterTargets="Clean">
<!-- Remove obj folder -->
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
<!-- Remove bin folder -->
<RemoveDir Directories="$(BaseOutputPath)" />
</Target>
Another one, if by any chance you're using Dropbox, check for Conflicted in file names, do a search in your repo and delete all those conflicted files.
This may have happened if you have moved the files around.
Cause of this defect: you have to remove the injected Nuget in file explorer.
Solution: Once you remove that Nuget in your system, then remove from following location.
select Tool=>Options=>NuGet Package Manager=> Package Sources then uncheck Microsoft Visual Studio Offline Packages Option
This worked for me:
added this package source:
Microsoft and .net
https://www.nuget.org/api/v2/curated-feeds/microsoftdotnet/
then run "dotnet restore" in the console
In my case I had a problem with the Available Package Sources. I had move the local nuget repository folder to a new path but I did not update it in the Nuget Available Package Sources. When I've correct the path issue, update it in the Available Package Sources and after that everything (nuget restor, etc) was working fine.
Very weird experience I have encountered!
I had cloned with GIT bash and GIT cmd-Line earlier, I encountered the above issues.
Later, I cloned with Tortoise-GIT and everything worked as expected.
May be this is a crazy answer, but trying with this once may save your time!
Seen this after adding a WinForms Core 3.1 project (from project templates) on VS-2019 vs 16.4.0 and trying to run it out of the box. Clean or Rebuild the entire solution did not work.
I just reloaded my solution.. that is File/Close Solution and then reopening it and rebuilding it solved the problem.
For me it turned out to be a nuget source credentials problem. I had recently changed my password for accessing a nexus server and visual studio was still using the old password when trying to access a nuget on that server through the windows credential manager. To fix it, I had to delete the entry for those outdated credentials in the credential manager and after, when I did a nuget restore, it prompted me for a password letting me enter the new password, which got saved in the credential manager again. You can access the credential manager from the cmd line using CmdKey.exe.

.NET Standard class library auto versioning - VSTS nuget pack & publish

I have a NET Standard class library (VS2017) and I created a CI/CD build in VSTS - it gets built, packed into nugget package and pushed to our own nugget feed. Now, I need to have an automated versioning of this library. How can I do this?
ok, how I made it to work:
I added dotnet pack build task v.2.* (preview)
set Automatic package versioning to Use the build number for the task
In Options tab I set Build number format to $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
And now when the project is built it publishes package with version in this format:
Version 2017.9.24.1
So I can now install it this with the command:
nuget install MyCommonLibrary -version 2017.9.24.1
Can't see any drawbacks for now.

How to install templates from nuget package?

I know I can install nuget packages from templates:
https://learn.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates
But I'd like to do the opposite, I'd like to install templates - provided by the nuget package - after the package was installed.
I read I could create a powershell script and run that after the package's been installed. Is that the only way? What are my options here?
Thanks.
For .NET Core it can be done using
dotnet new --install <id of your template package>
See docs for the CLI
I have the same problem, found that:
Dotnet 5.0 can't load templates from my nuget server by id, but dotnet 6.0 can, and puts .nupkg file to %USERPROFILE%.templateengine\packages
VS 2022 and new Rider view these templates.
Dotnet 5.0 can install template by .nupkg local path, and puts it to %USERPROFILE%.templateengine\dotnetcli\v5.0.404\packages
After step 3 you can use templates in Visual Studio 2019, but I also have ide.host.json in .template.config folder
https://github.com/sayedihashimi/template-sample#supporting-visual-studio-2019