NuGet.exe parameter -ConfigFile does not work as advertised

Topics: General
Apr 27, 2013 at 12:19 AM
Edited Apr 27, 2013 at 12:20 AM


There is a problem with the existing implementation of the -ConfigFile parameter to nuget.exe in that it does not get applied universally throughout the run of the executable.

Why is this relevant? We are running nuget.exe automated in our build system to restore the packages referenced by our projects. Each build agent runs more than a single build at a time. We've found out that NuGet.exe does not gracefully handle the case where the Nuget.config in %APPDATA% is open by another process. This essentially mean we cannot run more than one build at a time. We thought that creating a temporary NuGet.config file during the build and pointing to it with the -ConfigFile would solve the issue but quickly learned that the file in %APPDATA% was still being used, as builds continued to fail with the same error when attempting to read the file.

Ask: We have worked around the issue by building a private nuget.exe with the following changes. We would be really interested in running an official build of nuget.exe. What is the proper process to get this issue looked at and fixed?


For example, consider the following command: __nuget.exe install "mypackages.config" -source "\my\share" -solutionDir "\path\to\sln" -configFile "C:\Temp\Nuget.config"__

There are 2 places where %APPDATA%\Nuget\Nuget.config will still be opened:
  • InstallCommand.ResolveInstallPath (InstallCommand.cs, line 135) calls NuGet.Settings.LoadDefaultSettings(IFileSystem) w/o the configFileName parameter.
  • ProxyCache._instance (ProxyCache.cs, line 33) calls Settings.LoadDefaultSettings(IFileSystem) during its lazy initialization.
For reference, this is the workaround we've implemented:
  • Expose a static string property in Settings.cs
public static string DefaultConfigFileName { get; set; }
  • Add a static constructor to Settings that initializes DefaultConfigFileName
static Settings()
    // %AppData%\NuGet\NuGet.config
    string appDataPath = 

    if (!String.IsNullOrEmpty(appDataPath))
        DefaultConfigFileName = 
  • Change Settings.LoadDefault(IFileSystem, string) to read from DefaultConfigFileName instead of calculating the path to %APPDATA%\Nuget\Nuget.config when the configFileName parameter is null
if (configFileName == null)
    if (DefaultConfigFileName != null)
        string directory = 
        string filename = 
        appDataSettings = 
                new PhysicalFileSystem(directory), 
  • Change Command.Execute() to set Settings.DefaultConfigFileName when the -ConfigFile option is specified
if (String.IsNullOrEmpty(ConfigFile))
    /* ... */
    // Overwrite the default filename used by the Settings class
    // so that every load operation goes to it
    if (File.Exists(ConfigFile))
        NuGet.Settings.DefaultConfigFileName = ConfigFile;
    /* ... */