NuGet / Home

Repo for NuGet Client issues
Other
1.5k stars 252 forks source link

[Bug]: MSBuild Restore RestorePackagesConfig does not respect RestoreSources #11356

Open jklawrence opened 2 years ago

jklawrence commented 2 years ago

NuGet Product Used

MSBuild.exe

Product Version

16.11.1+3e40a09f8 for .NET Framework

Worked before?

Unsure

Impact

It's more difficult to complete my work

Repro Steps & Context

  1. Add a packages.config NuGet dependency on a package that does not exist on nuget.org but does exist on some other package source.
  2. Execute msbuild Example.sln -t:restore -p:RestorePackagesConfig=true -p:RestoreSources=${CustomSource}

What I expected: Packages to restore successfully.

What I got: A warning saying the package could not be found on the default source of 'https://api.nuget.org/v3/index.json'

Nuget.exe restore of packages.config with the -Sources flag works fine. MSBuild restore of PackageReferences using the RestoreSources parameter works fine.

Verbose Logs

Determining projects to restore...
  Restoring NuGet package Foo.Bar.1.0.5.
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(131,5): warning : Unable to find version '1.0.0' of package 'Foo.Bar'. [C:\Example\Example.sln]
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(131,5): warning :   https://api.nuget.org/v3/index.json: Package 'Foo.Bar.1.0.0' is not found on source 'https://api.nuget.org/v3/index.json'. [C:\Example\Example.sln]
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(131,5): warning :  [C:\Example\Example.sln]
  Restored C:\Example\ExampleProject.csproj (in 5.08 sec).
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(131,5): error MSB4181: The "RestoreTask" task returned false but did not log an error. [C:\Example\Example.sln]
heng-liu commented 2 years ago

I could repro with the following steps: 1.Create a non-SDK project. E.g. ClassLibrary (.NETFramework) 2.Install Newtonsoft.json package on this project 3.Go to the solution folder, clear all the contents in folder "packages" 4.Edit the user-wide NuGet.Config file, remove nuget.org source. Make sure there is no other NuGet.Config file in the path contains nuget.org source. 5.Run command

msbuild .\ClassLibrary2\ClassLibrary2.sln -t:restore -p:RestorePackagesConfig=true -p:RestoreSources=https://api.nuget.org/v3/index.json -v:n

The output is:

Build FAILED.

"C:\Repos\ClassLibrary2\ClassLibrary2.sln" (Restore target) (1) ->
(Restore target) ->
  C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning : Unable to find version '13.0.1' of package 'Newtonsoft.Json'. [C:\Repos\ClassLibrary
2\ClassLibrary2.sln]
C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning :   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\: Package 'Newtonsoft.Json.13.0.1' is not found on
source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\'. [C:\Users\henli\Source\Repos\ClassLibrary2\ClassLibrary2.sln]
C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning :  [C:\Repos\ClassLibrary2\ClassLibrary2.sln]
  C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning : Unable to find version '13.0.1' of package 'Newtonsoft.Json'. [C:\Users\henli\Source\Repos\ClassLibrary
2\ClassLibrary2.sln]
C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning :   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\: Package 'Newtonsoft.Json.13.0.1' is not found on
source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\'. [C:\Users\henli\Source\Repos\ClassLibrary2\ClassLibrary2.sln]
C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): warning :  [C:\Repos\ClassLibrary2\ClassLibrary2.sln]

"C:\Repos\ClassLibrary2\ClassLibrary2.sln" (Restore target) (1) ->
(Restore target) ->
  C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(130,5): error MSB4181: The "RestoreTask" task returned false but did not log an error. [C:\Users\henli\Source\Repos\Class
Library2\ClassLibrary2.sln]

    2 Warning(s)
    1 Error(s)

While compared to an SDK project, the property RestoreSourcesworks: 1.Create an SDK project. E.g. ClassLibrary (.NET Core) 2.Install Newtonsoft.json package on this project 3.Go to the global package folder, delete newtonsoft.json folder. 4.Edit the user-wide NuGet.Config file, remove nuget.org source. Make sure there is no other NuGet.Config file in the path contains nuget.org source. 5.Run command

msbuild .\ClassLibrary3\ClassLibrary3.sln -t:restore -p:RestoreSources=https://api.nuget.org/v3/index.json -v:n

The restore is successful.

nkolev92 commented 2 years ago

Note to the implementer: Enabling RestoreSources in packages.config restore might lead to a discrepancy in VS vs MSBUild restore for the given project.

Ex. Imagine you put RestoreSources in the project file, that wouldn't be respected in VS (not saying it should).

tripleacoder commented 2 years ago

I had to use this workaround to get my few packages.config-based projects to build with the same pipeline:

Added a nuget.config file to the solution:

<configuration>
  <packageSources>
    <add key="MyFeed" value="https://MyURL/nuget/v3/index.json" />
  </packageSources>
</configuration>