diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 83900e413..fef419a4f 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -587,6 +587,16 @@ private List InstallPackages( } } + string depPkgNameVersion = $"{depPkg.Name}{depPkg.Version.ToString()}"; + if (_packagesOnMachine.Contains(depPkgNameVersion) && !depPkg.IsPrerelease) + { + // if a dependency package is already installed, do not install it again. + // to determine if the package version is already installed, _packagesOnMachine is used but it only contains name, version info, not version with prerelease info + // if the dependency package is found to be prerelease, it is safer to install it (and worse case it reinstalls) + _cmdletPassedIn.WriteVerbose($"Dependency '{depPkg.Name}' with version '{depPkg.Version}' is already installed."); + continue; + } + packagesHash = BeginPackageInstall( searchVersionType: VersionType.SpecificVersion, specificVersion: depVersion, diff --git a/src/code/UpdatePSResource.cs b/src/code/UpdatePSResource.cs index 86e2cf1ae..6cfc4fbbe 100644 --- a/src/code/UpdatePSResource.cs +++ b/src/code/UpdatePSResource.cs @@ -25,6 +25,7 @@ public sealed class UpdatePSResource : PSCmdlet { #region Members private List _pathsToInstallPkg; + private HashSet _packagesOnMachine; private CancellationTokenSource _cancellationTokenSource; private FindHelper _findHelper; private InstallHelper _installHelper; @@ -157,6 +158,8 @@ protected override void BeginProcessing() RepositorySettings.CheckRepositoryStore(); _pathsToInstallPkg = Utils.GetAllInstallationPaths(this, Scope); + List pathsToSearch = Utils.GetAllResourcePaths(this, Scope); + _packagesOnMachine = Utils.GetInstalledPackages(pathsToSearch, this); _cancellationTokenSource = new CancellationTokenSource(); var networkCred = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null; @@ -216,7 +219,7 @@ protected override void ProcessRecord() pathsToInstallPkg: _pathsToInstallPkg, scope: Scope, tmpPath: _tmpPath, - pkgsInstalled: new HashSet(StringComparer.InvariantCultureIgnoreCase)); + pkgsInstalled: _packagesOnMachine); if (PassThru) {