Skip to content

Commit

Permalink
(chocolatey#3487) Prevent package success on dependency fail
Browse files Browse the repository at this point in the history
When a package dependency fails to install, we will now fail the
package as well.
  • Loading branch information
corbob committed Aug 15, 2024
1 parent 554ed07 commit 346d008
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/StringResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static class ErrorMessages
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
internal const string UnableToDowngrade = "A newer version of {0} (v{1}) is already installed.{2} Use --allow-downgrade or --force to attempt to install older versions.";
internal const string DependencyFailedToInstall = "Failed to install {0} because a previous dependency failed.";
}
}
}
68 changes: 68 additions & 0 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ public virtual ConcurrentDictionary<string, PackageResult> Install(ChocolateyCon

foreach (var packageName in packageNames.OrEmpty())
{
if (packageResultsToReturn.ContainsKey(packageName))
{
// We have already processed this package (likely during dependency resolution of another package).
continue;
}

// We need to ensure we are using a clean configuration file
// before we start reading it.
config.RevertChanges();
Expand Down Expand Up @@ -793,6 +799,23 @@ Version was specified as '{0}'. It is possible that version

foreach (SourcePackageDependencyInfo packageDependencyInfo in resolvedPackages)
{
// Don't attempt to action this package if dependencies failed.
if (packageDependencyInfo != null && packageResultsToReturn.Any(r => r.Value.Success != true && packageDependencyInfo.Dependencies.Any(d => d.Id == r.Value.Identity.Id)))
{
var logMessage = StringResources.ErrorMessages.DependencyFailedToInstall.FormatWith(packageDependencyInfo.Id);
var x = new PackageResult(packageDependencyInfo.Id, packageDependencyInfo.Version.ToStringSafe(), string.Empty);
var nullResult = packageResultsToReturn.GetOrAdd(packageDependencyInfo.Id, x);
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);

if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed install.".FormatWith(packageDependencyInfo.Id));
}

continue;
}

var packageRemoteMetadata = packagesToInstall.FirstOrDefault(p => p.Identity.Equals(packageDependencyInfo));

if (packageRemoteMetadata is null)
Expand All @@ -816,6 +839,12 @@ Version was specified as '{0}'. It is possible that version
var nullResult = packageResultsToReturn.GetOrAdd(packageToUninstall.Name, packageToUninstall);
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);

if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed install.".FormatWith(packageToUninstall.Identity.Id));
}

continue;
}

Expand Down Expand Up @@ -1061,6 +1090,12 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon

foreach (var packageName in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).OrEmpty())
{
if (packageResultsToReturn.ContainsKey(packageName))
{
// We have already processed this package (likely during dependency resolution of another package).
continue;
}

// We need to ensure we are using a clean configuration file
// before we start reading it.
config.RevertChanges();
Expand Down Expand Up @@ -1569,6 +1604,23 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
break;
}

// Don't attempt to action this package if dependencies failed.
if (packageResultsToReturn.Any(r => r.Value.Success != true && packageDependencyInfo.Dependencies.Any(d => d.Id == r.Value.Identity.Id)))
{
var logMessage = StringResources.ErrorMessages.DependencyFailedToInstall.FormatWith(packageDependencyInfo.Id, packageDependencyInfo.Version);
var x = new PackageResult(packageDependencyInfo.Id, packageDependencyInfo.Version.ToStringSafe(), string.Empty);
var nullResult = packageResultsToReturn.GetOrAdd(packageDependencyInfo.Id, x);
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);

if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed.".FormatWith(packageDependencyInfo.Id));
}

continue;
}

var packageRemoteMetadata = packagesToInstall.FirstOrDefault(p => p.Identity.Equals(packageDependencyInfo));

if (packageRemoteMetadata is null)
Expand All @@ -1593,9 +1645,25 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
var nullResult = packageResultsToReturn.GetOrAdd(packageToUninstall.Name, packageToUninstall);
nullResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
this.Log().Error(ChocolateyLoggers.Important, logMessage);

if (config.Features.StopOnFirstPackageFailure)
{
throw new ApplicationException("Stopping further execution as {0} has failed.".FormatWith(packageDependencyInfo.Id));
}

continue;
}

// Package was previously marked inconclusive (not upgraded). We need remove the message since we are now upgrading it.
// Packages that are inconclusive but successfull are not labeled as successful at the end of the run.
var checkResult = packageResultsToReturn.GetOrAdd(packageToUninstall.Name, packageToUninstall);

while (checkResult.Inconclusive)
{
checkResult.Messages.Remove(checkResult.Messages.FirstOrDefault((m) => m.MessageType == ResultType.Inconclusive));
}


var oldPkgInfo = _packageInfoService.Get(packageToUninstall.PackageMetadata);

BackupAndRunBeforeModify(packageToUninstall, oldPkgInfo, config, beforeUpgradeAction);
Expand Down

0 comments on commit 346d008

Please sign in to comment.