Skip to content

Support restoring packages.config with restore task #2917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

jeffkl
Copy link
Contributor

@jeffkl jeffkl commented Jul 3, 2019

This enable msbuild /t:Restore to restore projects with packages.config as well as PackageReference.

There are two project types that still don't support PackageReference, Service Fabric (.sfproj) and Visual C++ (.vcxproj). When you have a code base with one or more of these project types, you must either restore twice (once with msbuild and once with nuget.exe) or restore with nuget.exe against a Visual Studio solution file. It does not work well for large code bases to do either of those.

It is fully understood that packages.config is deprecated, however there are large code bases that cannot move away from .vcxproj or .sfproj. Until those project types support PackageReference, there needs to be a way to restore them.

Another point is that everything works as expected in Visual Studio. Packages are restored for all projects regardless of whether or not they are using packages.config or PackageReference. So this really just brings parity back.

I've only made this work for .NET Framework and the feature is disabled for .NET Core. The only scenario that won't work as expected is a .csproj still using packages.config (the other project types, .vcxproj and .sfproj do not build in .NET Core anyway). But I don't want to enable restoring packages.config for scenarios where people could just migrate to PackageReference.

This enable msbuild /t:Restore to restore projects with packages.config as well as PackageReference.
@@ -435,6 +435,14 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- If this is not a PackageReference project check if project.json or projectName.project.json exists. -->
<RestoreProjectStyle Condition=" '$(RestoreProjectStyle)' == '' AND '$(_CurrentProjectJsonPath)' != '' ">ProjectJson</RestoreProjectStyle>
<!-- This project is either a packages.config project or one that does not use NuGet at all. -->
<RestoreProjectStyle Condition=" '$(RestoreProjectStyle)' == '' AND
('$(MSBuildProjectExtension)' == '.csproj'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you think of this list, is it too greedy or am I missing any?

var v2RestoreResult = await PerformNuGetV2RestoreAsync(log, dgFile);
restoreSummaries.Add(v2RestoreResult);

// TODO: Message if no packages needed to be restored?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some resources are only in nuget.exe, should I move them or replicate them?

@@ -242,5 +314,243 @@ public void Dispose()
{
_cts.Dispose();
}

#if IS_DESKTOP
private async Task<RestoreSummary> PerformNuGetV2RestoreAsync(Common.ILogger log, DependencyGraphSpec dgFile)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up copy/pasting most of this method. Another option is to move it to NuGet.PackageManagement so its shared between nuget.exe and RestoreTask. But it would have to be public so I wasn't sure what you'd prefer

var nuGetPackageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, repositoryPath);

// TODO: different default? Allow user to specify?
var packageSaveMode = Packaging.PackageSaveMode.Defaultv2;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't specify a PackageSaveMode when using the RestoreTask, should I add a parameter? And what should the default be?

cacheContext.NoCache = RestoreNoCache;

// TODO: Direct download?
// //cacheContext.DirectDownload = DirectDownload;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no DirectDownload parameter to RestoreTask so I figured I'd not allow it for this scenario. Just making sure you agree.

}

#if IS_DESKTOP
public class CommandLineSourceRepositoryProvider : ISourceRepositoryProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another copy/paste that I could just move to NuGet.PackageManagement instead. Let me know your preference


namespace NuGet.CommandLine
namespace NuGet.ProjectManagement
Copy link
Contributor Author

@jeffkl jeffkl Jul 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this class from nuget.exe to NuGet.PackageManagement because it is already public, is that ok?

@jeffkl
Copy link
Contributor Author

jeffkl commented Jul 3, 2019

I will write unit tests once some details are figured out. Also, here's some sample output:

Build started 7/3/2019 8:33:35 AM.
Project "D:\Temp\TestPackagesConfigRestore\Solution1.sln" on node 1 (Restore target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Restore:
  Restoring NuGet package log4net.2.0.8.
  Restoring NuGet package Newtonsoft.Json.12.0.2.
    GET https://api.nuget.org/v3-flatcontainer/log4net/2.0.8/log4net.2.0.8.nupkg
    GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.2/newtonsoft.json.12.0.2.nupkg
    OK https://api.nuget.org/v3-flatcontainer/log4net/2.0.8/log4net.2.0.8.nupkg 6ms
  Installing log4net 2.0.8.
    OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.2/newtonsoft.json.12.0.2.nupkg 33ms
  Installing Newtonsoft.Json 12.0.2.
  Adding package 'log4net.2.0.8' to folder 'D:\Temp\TestPackagesConfigRestore\packages'
  Added package 'log4net.2.0.8' to folder 'D:\Temp\TestPackagesConfigRestore\packages'
  Adding package 'Newtonsoft.Json.12.0.2' to folder 'D:\Temp\TestPackagesConfigRestore\packages'
  Added package 'Newtonsoft.Json.12.0.2' to folder 'D:\Temp\TestPackagesConfigRestore\packages'
  Restoring packages for D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\ClassLibrary2.csproj...
    GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
    OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json 77ms
    GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.1/newtonsoft.json.12.0.1.nupkg
    OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.1/newtonsoft.json.12.0.1.nupkg 8ms
  Installing Newtonsoft.Json 12.0.1.
  Committing restore...
  Generating MSBuild file D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\obj\ClassLibrary2.csproj.nuget.g.props.
  Generating MSBuild file D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\obj\ClassLibrary2.csproj.nuget.g.targets.
  Writing assets file to disk. Path: D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\obj\project.assets.json
  Restore completed in 894 ms for D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\ClassLibrary2.csproj.

  NuGet Config files used:
      D:\Temp\TestPackagesConfigRestore\NuGet.Config
      C:\Users\jeffkl\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

  Installed:
      2 package(s) to packages.config projects
      1 package(s) to D:\Temp\TestPackagesConfigRestore\src\foo\bar\ClassLibrary2\ClassLibrary2.csproj
Done Building Project "D:\Temp\TestPackagesConfigRestore\Solution1.sln" (Restore target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.59

@jeffkl
Copy link
Contributor Author

jeffkl commented Jul 3, 2019

FYI @AndyGerlicher @kingerja

@nkolev92
Copy link
Member

nkolev92 commented Jul 8, 2019

I believe there's a discussion to be had about whether we want this into the product before reviewing this PR :)
//cc
@rrelyea

@zkat zkat self-assigned this Aug 28, 2019
@zkat
Copy link
Contributor

zkat commented Aug 28, 2019

Hi @jeffkl! I've created a corresponding issue over at NuGet/Home#8506 and I'm gonna be helping make sure we get all your questions answered and get this landed -- the NuGet team has decided this is a good change to make that addresses previous concerns about backporting P.C. support, and this is baaaasically what I'll be working on for the next couple of weeks!

Thanks a lot for this PR! It's a HUGE help in getting things across the board. I haven't started reviewing it yet (I've got a P0 on my plate right now), but I'll do that soon, and then help come up with a good testing strategy, since my understanding is this isn't a very well tested part of NuGet (I'm new here, so please bear with me while I get up to speed!).

I look forward to working with you~

@jeffkl
Copy link
Contributor Author

jeffkl commented Aug 29, 2019

@zkat awesome, thanks for taking this on. Feel free to schedule a meeting with me to discuss it. I can't wait to get this change in.

@zkat
Copy link
Contributor

zkat commented Sep 23, 2019

Closing in favor of #3071

@zkat zkat closed this Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants