dotnet / project-system

The .NET Project System for Visual Studio
MIT License
967 stars 386 forks source link

Cannot install/query NuGet packages immediately after a project has loaded #1600

Open davkean opened 7 years ago

davkean commented 7 years ago

This is a more specific version of https://github.com/dotnet/roslyn-project-system/issues/1332 and believe to be a cause of https://github.com/NuGet/Home/issues/4622.

Installing a package just after a project has loaded, is resulting in:

The operation failed as details for project Web1 could not be loaded.System.InvalidOperationException: The operation failed as details for project Web1 could not be loaded.
at NuGet.PackageManagement.VisualStudio.CpsPackageReferenceProject.GetAssetsFilePathAsync()
[..] Async logic
at NuGet.VisualStudio.VsPackageInstaller.InstallPackage(String source, Project project, String packageId, String version, Boolean ignoreDependencies)
at Microsoft.VisualStudio.Azure.Fabric.Shared.ServiceCreation.ServiceTemplateInterpreter.InstallNuGetPackages(IServiceProvider serviceProvider, Hierarchy project, ServiceTemplateServiceContentProjectPackage[] packages, String serviceId)Failed to install package 'Microsoft.ServiceFabric.Data':

This is because we haven't nominated for restore before our project factory has completed. At the moment, it's running after the factory has completed and after a design-time build has been completed. Instead, we consider doing this much earlier before we've been added to the solution and during evaluation. Make note that if we do this - NuGet might not be able to get specific information until the project has fully loaded.

Whatever we do here, is that once the factory has been completed, you should be able to install a package.

aleksander-oven commented 5 years ago

I find that this is still a problem in VS 2017.9. I have a VSIX that implements a wizard. The wizard tries to install a NuGet package inside its RunFinished method. This used to work fine with the old COM-based project system (i.e. using flavored project type), but fails after transitioning to CPS. My current workaround is to call Task.Run + Task.Delay before calling IVsPackageInstaller's methods. It gets things done, but feels really hacky.