NuGet -ConfigFile option supersedes %ProgramData% config file - nuget

According to the NuGet documentation, you should be able to specify a custom config file while still using any additional config files that are located in %ProgramData%\Nuget\Config. However, that doesn't seem to be the case. If I specify a custom config file, any config file I have in %ProgramData% is unused:
C:\> nuget.exe sources
(output includes sources specified in my %ProgramData%\Nuget\Config\A.config)
C:\> nuget.exe sources -ConfigFile B.config
(output includes sources specified in B.config but not in ProgramData%\Nuget\Config\A.config)
As a matter of fact, if I specify the -ConfigFile option, I can even put xml errors in A.config and it doesn't complain at all.
Has anyone else run into this?

I re-posted this at the NuGet site, and it looks as if there was an intentional change in behavior in version 3.4.
https://github.com/NuGet/Home/issues/2812

Related

nuget is not a recognized as an internal or external command

While running a batch file, I get the below error:
nuget is not a recognized as an internal or external command
It's not working from command prompt. Have a look at the below screenshot.
This happens when either you are not in the directory where the nuget.exe is located or your environment variables are not set for it.
Make sure you have downloaded NuGet.exe.
Navigate to it and right click, then click on properties.
Copy the location of it.
Navigate to Control Panel > System > Advanced System Settings > Environment Variables.
Windows 10
Double click the variable Path click add new, paste in the location of the nuget.exe.
Restart Command Prompt and the issue should be fixed.
Other operating Systems
Double click the variable Path, If there is no other locations simply paste it in. If there is add a semi colon to the end of it and the paste it in.
Restart Command Prompt and the issue should be fixed.
nuget is not a recognized as an internal or external command
That because NuGet is a external command, can not be recognized by Windows in an arbitrary directory, you should type the "NuGet" or "NuGet.exe" in the path where NuGet.exe exists and if you want type it in an arbitrary directory, you should download the NuGet.exe (Not VSIX) from NuGet.org and set the local path of NuGet.exe into your path environment variable.
Instead of using nuget.exe in your path, use .net version
dotnet nuget
Download the Nuget Exe, keep it in a folder you want to. For ex : D:\Software\Nuget
Open Cmd prompt with the same path.
To create this .nuspec file, execute the following command in a command prompt.
D:\Software\Nuget>Nuget.exe spec
For .NET Core / .NET 5:
Use the command dotnet pack
Instead of using a .nuspec file you set those properties in your .csproj file, for example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PackageId>MyCompany.Project</PackageId>
<Version>1.0.0.1</Version>
</PropertyGroup>
...
</Project>
With above config, running dotnet pack will create a file named
MyCompany.Project.1.0.0.1.nupkg
For more details and config options, see: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target
I follow this link and it work fine with me
How to Fix “not recognized as an internal or external command” in Windows
just add the path of the nuget in the path variable
1 - Installing Package:
I followed this Microsoft documentation. Use the nuget.exe CLI in the package console
Note:- This configuration is specific to a project and this adds dependency to the projects package.config
2 - Installing exe:
You can also follow the below documentation, again from Microsoft to download and set the path to nuget.exe folder, as others in this thread has explained.
NuGet CLI reference

How do I correct the output directory of a CI/CD NuGet package build using Visual Studio Team Services?

I've just started trying to configure a CI/CD process using Visual Studio Team Services, with a view to having a gated check-in process which then automatically packs and pushes the output to a private NuGet Feed. I'm successfully getting through the "Get Sources" task which exists natively in the build definition, however my package task is failing at this stage:
d:\a\_tool\NuGet\4.0.0\x64\nuget.exe pack
d:\a\1\s\Core\Core\Core.csproj -NonInteractive -
OutputDirectory d:\a\1\a -Properties Configuration=$Release;OutDir=$(OutDir)
-Symbols -version 1.0.0-CI-20170811-095629 -Verbosity Detailed
NuGet Version: 4.0.0.2283
Attempting to build package from 'Core.csproj'.
MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin\amd64'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
NuGet.CommandLine.CommandLineException: Unable to find 'd:\a\1\s\Core\Core\$(OutDir)\Core.dll'. Make sure the project has been built.
Essentially, the 2nd line of the log demonstrates that my project file is in d:\a\1\s, however the output is directed to d\a\1\a - not the same place. The instruction to NuGet to package my file then looks in the correct location (d:\a\1\s), but the file isn't here.
I understand that I can specify a Package Folder within the build definition, however I've not changed this from the default ($(Build.ArtifactStagingDirectory)) - I expect this to work natively, but for reasons I can't explain, it's not.
Is there a token or wildcard I can provide in an additional build property that will rectify this? I've taken the guidance of the first posted answer in here and changed $(Build.ArtifactStagingDirectory) to $(Build.Repository.LocalPath), which gets me much closer to the goal as the error now reads -OutputDirectory d:\a\1\s\... - I've tried manually putting a variety of sensible paths no the end of this, but the error persists.
Refer to this article. It has the build variables for the TFS/VSTS environment.
$(Build.ArtifactStagingDirectory) itself refers to the \1\a folder. You might want to try the $(Build.Repository.LocalPath) variable.
Not a 100% if it would work, but might as well try.
The issue is related to OutDir=$(OutDir) instead of OutputDirectory.
The OutputDirectory specifies the folder in which the created package is stored and the OutDir specifies the build output, but the project need to be built before NuGet pack task, so you need to add Visual Studio Build task or related to build project.
You can leave Additional build properties box blank to use default output per to configuration, because you just need to know the package stored path to push package to remote feed.
If you want to change build output, you can specify /p:OutDir msbuild argument to change build output location. For example:
Visual Studio Build task (MSBuild Arguments: /p:OutDir=$(Build.ArtifactStagingDirectory))
NuGet pack task (Additional build properties: OutDir=$(Build.ArtifactStagingDirectory))
BTW, to change package stored location, you need to specify OutputDirectory.

How to instruct RestorePackages to use the NuGet.config file's repositoryPath for the package output path?

I am attempting to migrate a CCNET based build script to a F# Fake based one. I am having difficulty understanding how to modify the RestorePackages method to honor the NuGet.config file that instructs NuGet to place the packages in a folder named 'External' several layers deep in our repository.
NuGet.config Example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="Code\DotNet\External\" />
</config>
</configuration>
NuGET CCNET Example:
<exec executable="Tools\NuGet\nuget.exe">
<buildArgs>restore $[$CCNetWorkingDirectory]\Code\DotNet\Web\All.sln</buildArgs>
</exec>
In the above examples I am simply telling NuGet to restore packages for all the projects in the solution. This honors the settings in NuGet.config.
NuGet Fake Example:
Target "RestorePackages" (fun _ ->
RestorePackages()
)
However, calling RestorePackages() does not honor NuGet.config (as this method simply uses default parameters) and so my packages are updated in the wrong location.
Ideally I would like to instruct RestorePackages to look specifically at the Visual Studio solution file as I do with CCNET. Is this possible?
If that is not doable, I would like to instruct RestorePackages to honor the settings in the NuGet.config file.
However, if neither are doable, I'll need to be able to override the Output path. Unfortunately, I am having difficult time understanding the documentation and I am not sure how to construct this.
I have been reading over the documentation and the source code for the RestorePackagesHelper module and found that it does not support what I want to acheive, at least not directly due to two limitations.
Limitation 1: No Solution File Support - The current RestorePackage implementation uses the NuGet Install Command which does not support passing in a solution file. In order to pass in a solution file one would need to use the NuGet Restore Command. Therefore this is not a direct Fake equivalent for what the CCNET snippet listed in the question.
Limitation 2: No ConfigFile Support - Further, the RestorePackageParams type does not have a ConfigFile parameter so I am unable to specify the ConfigFile that I want to use in for the NuGet restoration.
These are current limitations of Fake. Not NuGet.
A Reasonable Solution - However, I can achieve a similar effect with Fake by scanning for all packages.config files in a given directory and calling RestorePackage for each one; each time specifying the OutputPath as shown below.
Fake NuGet RestorePackages Example
Target "RestorePackages" (fun _ ->
!! "./**/packages.config"
|> Seq.iter (RestorePackage (fun p ->
{ p with
OutputPath = "./Code/DotNet/External"}))
)

Nuget Pack Default Option Values

I'm trying to make it so that when I run nuget pack it always outputs to the same directory, rather than typing it in every time. The following link suggests that defaults are possible by setting config file key/value pairs.
http://docs.nuget.org/docs/reference/nuget-config-file
However, there doesn't seem to be a clear syntax. I've tried using keys such as:
OutputDirectory
DefaultOutputDirectory
DefaultPackOutputDirectory
None of which worked. Maybe the "DefaultPushSource" is a hard-coded key in the executable? Is it even possible to set default values for pack options using the config file approach?
here is the list of settings that can be defined in the nuget.config: http://docs.nuget.org/docs/reference/nuget-config-settings
There is no nuget.config settings for nuget.exe pack command yet.
#Chad H : There is no way to do this right now. The best way I can recommend is to create an alias for the nuget pack command which has the outputDirectory flag set to whatever path you want your output at :
https://docs.nuget.org/consume/command-line-reference#pack-command

How to make .nuget\NuGet.Config file attached to VS solution to be not ignored?

I've created new solution in Visual Studio, enable nuget package restore and got in soluition root .nuget folder which contains the following files needed to make package restore work: NuGet.exe, NuGet.config, NuGet.targets.
But I can't make nuget(as Nuget Visual Studio add-in and \.nuget\NuGet.exe as well) read setting from .\nuget\NuGet.config file - settings are still read from default %appdata%\NuGet\NuGet.config.
What have I tried.
I've tried to look where the path to NuGet.config is specified and haven't found anything like that, and nuget.exe install command that runs during build doesn't have any parameter saying like "take this nuget.config file".
Also I've removed \.nuget\NuGet.config and everything works - packages are restored during Visual Studio build and CI one.
Question.
I've got an impression that \.nuget\NuGet.config isn't used at all.
So essentially the question is: How to make .nuget\NuGet.Config file to be not ignored?
What am I doing wrong and how to do it write?
Thanks and have a nice %time_of_day% :)
Disclaimer
Everything that I've mentioned above is not a problem/issue - current nuget behavior is completely acceptable for me - I'm asking just because I'm curious and confused at the same time
I haven't tried this personally, but here is what the official Nuget 2.1 release notes say:
NuGet.config files are searched for in the following order:
.nuget\nuget.config
Recursive walk from project folder to root
Global nuget.config (%appdata%\NuGet\nuget.config)
The configurations are than applied in the reverse order, meaning that
based on the above ordering, the global nuget.config would be applied
first, followed by the discovered nuget.config files from root to
project folder, followed by .nuget
If this doesn't help I'd suggest you write more specifically what you're trying to achieve and which config settings you're changing - knowing more details about the problem may clarify the situation.
Did you point the config file that you want to use as ?
In nuget page example;
nuget config -set repositoryPath=c:\packages -configfile c:\my.config
nuget config -set repositoryPath=c:\packages -configfile .\myApp\NuGet.Config
nuget config -set repositoryPath=c:\packages -configfile %ProgramData%\NuGet\Config\VisualStudio\14.0\NuGet.Config
nuget config -set repositoryPath=c:\packages -configfile %ProgramData%\NuGet\NuGetDefaults.Config