From f48b490d9c380ec83bc29b2f65d7440b294e3deb Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Fri, 29 Jan 2016 08:50:21 -0600 Subject: [PATCH] (GH-479) Remove NuGet cache of package Chocolatey doesn't use the NuGet cache at all, but NuGet always caches the package in the NuGet cache (and there is not a way to shut it off with NuGet calls unfortunately). This can cause issues when later using NuGet and trying to install a package from NuGet.org that is different, but carries the same name. The cached file will be served to the user causing hard to detect issues. Therefore we need to clean up after NuGet as there is no way to tell it NOT to cache the nupkg during install/upgrade. --- .../services/NugetService.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index b37f76c4ae..19ed1d4b23 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -449,6 +449,7 @@ public ConcurrentDictionary install_run(ChocolateyConfigu { packageManager.InstallPackage(availablePackage, ignoreDependencies: config.IgnoreDependencies, allowPrereleaseVersions: config.Prerelease); //packageManager.InstallPackage(packageName, version, configuration.IgnoreDependencies, configuration.Prerelease); + remove_nuget_cache_for_package(availablePackage); } } catch (Exception ex) @@ -721,6 +722,7 @@ public ConcurrentDictionary upgrade_run(ChocolateyConfigu { packageManager.UpdatePackage(availablePackage, updateDependencies: !config.IgnoreDependencies, allowPrereleaseVersions: config.Prerelease); } + remove_nuget_cache_for_package(availablePackage); } } catch (Exception ex) @@ -911,6 +913,25 @@ private void remove_cache_for_package( ChocolateyConfiguration config, IPackage "Unable to removed cached files"); } + /// + /// Remove NuGet cache of the package. + /// Whether we use the cached file or not, NuGet always caches the package. + /// This is annoying with choco, but if you use both choco and NuGet, it can + /// cause hard to detect issues in NuGet when there is a NuGet package of the + /// same name with different contents. + /// + /// The installed package. + private void remove_nuget_cache_for_package(IPackage installedPackage) + { + var nugetCachedFile = _fileSystem.combine_paths(Environment.GetEnvironmentVariable("LocalAppData"), "NuGet", "Cache", "{0}.{1}.nupkg".format_with(installedPackage.Id, installedPackage.Version.to_string())); + if (_fileSystem.file_exists(nugetCachedFile)) + { + FaultTolerance.try_catch_with_logging_exception( + () => _fileSystem.delete_file(nugetCachedFile), + "Unable to removed cached NuGet package file"); + } + } + public void uninstall_noop(ChocolateyConfiguration config, Action continueAction) { var results = uninstall_run(config, continueAction, performAction: false);