Skip to content

Commit

Permalink
Made RemoveUnnecessaryPackages consistent (#696)
Browse files Browse the repository at this point in the history
In the old code it would look for nuspec files on the file system and then
remove folders that are not in packages.config. But installedPackages
already contain all found nuspec files. The issue with old code is that
InstalledPackages contained data about packages that were also
potentially modified by the plugins while this method didn't take that
into account leading to inconsistencies.
  • Loading branch information
igor84 authored Feb 1, 2025
1 parent 54c6f94 commit 46e771d
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions src/NuGetForUnity/Editor/InstalledPackagesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ internal static void UpdateInstalledPackages()
}

/// <summary>
/// Checks if there are any packages inside the package install directory that are not listed inside the packages.config.
/// Finds and removes any installed packages detected that are not listed inside the packages.config.
/// </summary>
/// <returns>True if some packages are deleted.</returns>
internal static bool RemoveUnnecessaryPackages()
Expand All @@ -256,38 +256,17 @@ internal static bool RemoveUnnecessaryPackages()
return false;
}

var directories = Directory.GetDirectories(ConfigurationManager.NugetConfigFile.RepositoryPath, "*", SearchOption.TopDirectoryOnly);
var somethingDeleted = false;
foreach (var folder in directories)
foreach (var installedPackage in InstalledPackages)
{
var folderName = Path.GetFileName(folder);
if (folderName.StartsWith(".", StringComparison.Ordinal))
{
// ignore folders whose name starts with a dot because they are considered hidden
continue;
}

var nuspecPath = Directory.GetFiles(folder, "*.nuspec").FirstOrDefault();
if (!File.Exists(nuspecPath))
{
// ignore folder not containing a nuspec file
continue;
}

var package = NugetPackageLocal.FromNuspecFile(
nuspecPath,
new NugetPackageSourceLocal(
"Nuspec file already installed",
Path.GetDirectoryName(nuspecPath) ?? throw new InvalidOperationException($"Failed to get directory from '{nuspecPath}'")));

var installed = PackagesConfigFile.Packages.Exists(packageId => packageId.Equals(package));
var shouldBeInstalled = PackagesConfigFile.Packages.Exists(packageId => packageId.Equals(installedPackage));

if (!installed)
if (!shouldBeInstalled)
{
somethingDeleted = true;
NugetLogger.LogVerbose("---DELETE unnecessary package {0}", folder);
NugetLogger.LogVerbose("---DELETE unnecessary package {0}", installedPackage.Id);

PackageContentManager.DeletePackageContentPackage(package);
PackageContentManager.DeletePackageContentPackage(installedPackage);
}
}

Expand Down

0 comments on commit 46e771d

Please sign in to comment.