Search code examples
.netnugetdotnet-cli

Determine which nuget.config file is used by nuget


Nuget has rules for locating the nuget.config file to be used.

In a large/complex monorepo the file will not necessarily be at the workspace/solution "root".

Is there a shell command to determine which nuget.config file nuget has selected and will use for nuget operations?

(The closest thing I've found is dotnet nuget list source, but that doesn't tell me which file was selected.)


Solution

  • I didn't find a single command to answer your question directly. But when running dotnet restore -f -v n, it gives me the following output:

    Build started 22.08.2022 08:58:38.
         1>Project "C:\source\temp\NuGetTest2\NuGetTest2.sln" on node 1 (Restore target(s)).
         1>ValidateSolutionConfiguration:
             Building solution configuration "Debug|Any CPU".
           _GetAllRestoreProjectPathItems:
             Determining projects to restore...
           Restore:
             Restoring packages for C:\source\temp\NuGetTest2\src\Test\Test.csproj...
             Assets file has not changed. Skipping assets file writing. Path: C:\source\temp\NuGetTest2\src\Test\obj\project.assets.json
             Restored C:\source\temp\NuGetTest2\src\Test\Test.csproj (in 373 ms).
    
             NuGet Config files used:
                 C:\source\temp\NuGetTest2\src\Test\NuGet.Config
                 C:\source\temp\NuGetTest2\src\NuGet.Config
                 C:\source\temp\NuGetTest2\NuGet.Config
                 C:\Users\myUser\AppData\Roaming\NuGet\NuGet.Config
                 C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
    
             Feeds used:
                 https://apiC.nuget.org/v3/index.json
         1>Done Building Project "C:\source\temp\NuGetTest2\NuGetTest2.sln" (Restore target(s)).
    

    Since apiC gets used, I'd assume that the order of the output is equivalent to the precedence. These are my NuGet.Config files:

    • NuGet.Config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="NuGetA" value="https://apiA.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    
    • src\NuGet.Config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="NuGetB" value="https://apiB.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    
    • src\Test\NuGet.Config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <clear />
        <add key="NuGetC" value="https://apiC.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>