NuGet package refuses to uninstall in .NET Core library - c#

I'm creating a .NET Core xUnit test project library in VS2017. I accidentally installed the System.Collections.Immutable NuGet package, and now I want to uninstall it.
First, I tried editing the csproj file and removing this line:
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
That didn't work, since the types from that package were still highlighted in the editor. So I tried running Uninstall-Package System.Collections.Immutable in the Package Manager Console.
The console said it couldn't find the package, so I tried putting the <PackageReference... line above back and running Uninstall-Package again. The console said the uninstall succeeded, but VS still recognized the types.
I tried closing VS, deleting .vs/, and reopening. It still recognized the types.
I tried git stashing my changes, running git clean -xdf, and running dotnet restore from the command line. Somehow, it still outputs
$ dotnet restore
Restoring packages for C:\cygwin64\home\james\Code\cs\BlockList\src\BlockList\BlockList.csproj...
Restoring packages for C:\cygwin64\home\james\Code\cs\BlockList\src\BlockList.Tests\BlockList.Tests.csproj...
...
Installing System.Collections.Immutable 1.2.0.
Installing System.Collections.Immutable 1.3.0.
...
I also tried restoring from Visual Studio instead of the command line. Still no luck.
When I searched all the files in my repo for the word Immutable, the only thing popping up is project.assets.json in the obj directory. Not a single source file contains the word Immutable. I'm confused, then, as to how it's still being referenced. How can I uninstall it?
A few other things:
I checked the 'Dependencies' drop-down for my project in Solution Explorer, and it's not listed there.
I don't want to delete my local copy of the repo and re-clone it, since I have other work stashed.
System.Collections.Immutable.dll doesn't show up at all in the bin/ directory, yet when I use one of the types in my library and run it, it works fine.
Thanks!
edit: Adding this line to the library:
System.Diagnostics.Debug.WriteLine(typeof(ImmutableArrayExtensions).GetTypeInfo().Assembly.Location);
And running it says that the assembly is located in this location:
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.2\System.Collections.Immutable.dll

After about an hour, I found out what the problem was. My .NET Core library referenced Microsoft.NETCore.App, which referenced System.Collections.Immutable. I looked at the dependencies for the former, but overlooked System.Collections.Immutable because that package references maybe 50 other packages. I ended up finding this out by creating a brand-new xUnit test project, and trying to see if ImmutableArray<> was present without installing anything. Sure enough, I could use it out of the box.

I had a similar issue with VS2017 and a different nuget package that wouldn't fully uninstall. I ultimately had to create a new project and cut and paste all my code into it. Kind of a sledge hammer approach, but faster than crawling through dependencies.

Related

Microsoft.NET.Sdk.Web and packages.config

I have a web project that I'm trying to convert to Microsoft.NET.Sdk.Web. When I make the change to the .csproj file, though, it seems to force it to be interpreted as a <PackageReference>-based project. Of course, <PackageReference> isn't supported in web projects, because it doesn't support persisting the results of installing a package. Web projects must use packages.config in order to support NuGet packages that inject static content into the site's space (such as NewRelic). But, after switching to Microsoft.NET.Sdk.Web, the build system seems absolutely stuck on the project being <PackageReference>-based. How do I resolve this? Surely the very existence of Microsoft.NET.Sdk.Web implies that it is possible to use packages.config with an SDK project. But, even if I set <RestoreProjectStyle>Packages.config</RestoreProjectStyle>, it still thinks the project is <PackageReference>-based. I've seen advice to try running Update-Package -Reinstall after clearing out obj, bin and .vs, but nothing I've tried has convinced it that this is not a <PackageReference>-based project:
In Visual Studio, all of the NuGet-inserted references are lumped in with other references under Dependencies\Assemblies, and the "Installed" tab of the NuGet Package Manager is empty.
What do I do?

How to achieve full NuGet reinstall for dozens of solutions?

We have dozens of solutions in a repository and we're retargeting every project to net472 from net462. Currently our best bet is to open each and every solution in Visual Studio and execute the following command in the Package Manager Console.
Update-Package -Reinstall -IgnoreDependencies
As far as I'm aware, the PM console cannot be used outside Visual Studio, so this method of course is not very efficient, so what I was thinking about is using the nuget.exe tool for this. However at first glance I could not find any equivalent operation or argument set.
The documentation at this moment says the following
For all packages, delete the package folder, then run nuget install.
For a single package, delete the package folder and use nuget install
to reinstall the same one.
So based on this I tried to delete the packages folder and run nuget install for a project, so I expected it to do a re-install. However, while it installed the package indeed (to packages), it does not touch the packages.config (for retargeting).
Is anyone aware of any kind of possible way to automate this process?
How to achieve full NuGet reinstall using nuget.exe CLI?
That command cannot get what you want.
As far as I know, nuget install should be with packages.config file and it will not update the nuget framework version of packages.config file automatically.
So whenever you change the target framework version of your project, using that command will not update the target framework version of the nuget package.
So only update-package -reinstall command under Package Manager Console will update the target framework version of packages.config file.
And also Package Manager Console cannot access multiple solutions so you have to open each solution to run that command.
Although it may be possible to achieve your expectations with PowerShell scripts, but it is too complex so that it is easier to open each solution and then run the command.
As a suggestion,
1) open each solution on VS to run update-package -reinstall command.
2) And migrating from packages.config to PackageReference may be a good choice. In this case, the nuget packages will automatically adapt to the corresponding project target framework version.
Before doing this,you can make a backup of your project.
3) If these all do not meet your requirements, you could suggest a feature on our User Voice Forum to report your desire for automation. After that, you can share the link here and anyone who is interested in it will vote it so that it will get more attention from Microsoft.

Could not load file or assembly 'System.Security.Principal.Windows'

Solution has compiled successfully, but after I added an existing class file to the project, this error appeared:
The specified task executable "csc.exe" could not be run. Could not
load file or assembly 'System.Security.Principal.Windows,
Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file
specified. MvcApplicationRegister
I installed System.Security.Principal.Windows package By NuGet, but error still appears.
None of the previous answers worked for me.
The problem was that I didn't have the .NET Compiler Platform SDK installed.
To solve, open Visual Studio Installer, choose "Modify", and under the "Invididual Component" tab, check the .NET Compiler Platform SDK, and confirm your changes by clicking "Modify".
After I installed it and reopened Visual Studio, the problem is gone.
I had the same issue after I upgraded Microsoft.Net.Compiler from 2.8.2 to 2.9.0.
After I downgraded to 2.8.2 projects compiled without any errors.
Had this same issue and resolved it.
In a 3 project solution MVC controller (Web,Business,Data)
Caused by the Microsoft.Net.Compiler 2.9.0 being installed on the Web project but not the other projects.
To resolve:
Right click the solution.
Manage NuGet Packages.
Installed > Search for the compiler
Ensure it is the same version and it is installed on all projects in your solution
Once installed my solution built successfully
If you are using Microsoft.CodeDom.Providers.DotNetCompilerPlatform you can upgrade to 2.x and then remove Microsoft.Net.Compilers as it's no longer needed. That solved it for me, however I couldn't even build the solution in the first place. It was still complaining about System.Security.Principal.Windows though, I could also solve it by referencing System.Security as an assembly. It's not recommended though.
Close down all instances of Visual Studio. Then reopen the solution and rebuild.
I uninstalled both Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers and everything now works.
In my case I could go from Microsoft.Net.Compilers 2.4.0 to Microsoft.Net.Compilers 2.10.0. No need to use Microsoft.Net.Compilers 2.8.2.
I do not like removing packages without first understanding what I'm removing. I faced the same problem with my solution.
I discovered that 1 of the many projects was using the Microsoft.Net.Compilers NuGet package - let's call it Project ABC. A Unit Test project was referencing that Project ABC, but not the Microsoft.Net.Compilers NuGet package.
I simply referenced the Microsoft.Net.Compilers NuGet package from my Unit Test project, and the problem has now gone away.
Updating Microsoft.CodeDom.Providers.DotNetCompilerPlatform and deleting Microsoft.Net.Compilers worked for me.
I had the same issue here using 'System.Security.Principal.Windows' version 4.7.0. For some reason when I installed the nuget package it was referecing the dll from the folder: C:\Users\{your-user}\.nuget\packages\system.security.principal.windows\4.7.0\ref\netstandard2.0\System.Security.Principal.Windows.dll then I changed to reference the dll from the folder C:\Users\{your-user}\.nuget\packages\system.security.principal.windows\4.7.0\lib\netstandard2.0\System.Security.Principal.Windows.dll and everything worked just fine! So I copied the dll from the lib folder to my project and made a direct reference.

Team Foundation Server won't build because nuget packages dont exist

So i created a new solution. Added all my projects to it. Downloaded all the nuget packages.
I checked it all in. On another pc I pulled the solution down. I hit build and I get a tonne of errors relating to not being able to find the dll of the packages.
So I set automatically download packages if they're missing. Still nothing works. Why is this?
When I Enabled it, it came back with an error saying that it could not modify the projects because they were locked.... This is why I want to re-enable the context menu item that allows me to enable nuget to automatically download missing packages.
I think the problem is I checked in all the packages. They all got download onto the other machine, that's why NuGet was saying that I already had the packages.
I solved this by going into the solution directory (physical path) and deleted the packages folder.
Then I built the project, and true to NuGets word it downloaded all the packages again. It also succeeded in building the application, no errors!
I would advise that if you are going to enable the NuGet package restore that you check out the solution first, once you're done check in the solution!

All projects referencing sub-project must install NuGet package Microsoft.Bcl.Build (C#/Windows Phone 7)?

I'm having a particularly difficult refactoring session involving a C# solution with multiple projects in Visual Studio 2012. I needed to pull out a bunch of code into their own assemblies so that code could be shared across several projects, all in the same solution. However, no matter what I try, I get warnings for the projects that reference the new shared projects that "All projects referencing {shared project name} must install nuget package Microsoft.Bcl.Build".
I have been over the dependent projects and the shared projects with a fine-tooth comb, verifying in detail that they all use the same version and exact same DLL for the Microsoft.Bcl version 1.0.1.19 and Microsoft.Bcl.Async version 1.0.16 packages:
System.Runtime
System.Threading.Tasks
Microsoft.Threading.Task
Microsoft.Threading.Tasks.Extensions
Microsoft.Threading.Tasks.Extensions.Phone
The DLL paths are all resolved and identical. The XAP file does build but I still get that warning telling me that Microsoft.Bcl.Build is not referenced in the dependent projects, despite the fact that I can see that it is.
If I try instead to uninstall and then reinstall those two packages using NuGet for each project involved, I get references with empty paths and the warning icon for the 5 DLL references involved. For some reason NuGet adds the references but can't find the DLLs. Also, if I do this, I find myself with the problem frequently of having projects where I get the "Can't add reference" error when trying to add a reference. Then I have close and re-open the solution, and that leads to a "project failed to load" error. So I have to edit the project file manually, remove the faulty package import statements, and reload the project.
How can I fix this problem and what is the general technique for avoiding this headache in the future? Letting NuGet manage missing packages didn't help at al.
In case anyone else comes across this and #Swell's solution made you go "wtf":
I recently went through an older MVC project and updated it (updated razor, asp, http, etc. nuget packages). The project, independent of itself, built fine, but when i went to publish it failed with the OP's errors.
It turns out it's because I didn't update the *.Tests project associated with it (should have figured, though not sure why Tests is that closely tied to the project). So, to fix:
Right-click the Solution and manage nuget packages.
Go through all the packages that were updated in the web project and apply them to the other projects as well (each "Update" will display a tree with the applicable projects, I was fine just OKAY-clicking through).
Rebuild.
You should now be good and it shouldn't bark at you. Hope that helps others.
I just came throught the same issue and a bug is opened here: http://nuget.codeplex.com/workitem/3268
What I've done is the following, I added to the solution level the package Microsoft.Bcl.Build
In my dev env if you don't have the package loaded, just right click the solution and select manage nuget packages, you see a yellow bar with a restore button, just click it and you will be fine.
In my build script before compiling the project I run this command:
.\myproject\.nuget\NuGet.exe install .\myproject\.nuget\packages.config -OutputDirectory .\myproject\packages
This will restore solution level packages and you will be fine.
This should be fixed by the end of this summer in version 2.7 according to the issue report

Categories