Skip to content

Commit

Permalink
(chocolatey#3489) Return early when folder doesn't exist
Browse files Browse the repository at this point in the history
The MarkPackagePending method is used to create a .chocolateyPending
file in the package folder to indicate that there is a pending
operation that needs to be completed.

However, during an upgrade operation, the package folder is moved to
the lib-bkp folder, and as a result, doesn't exist anymore in the lib
folder.

If during the upgrade operation, something fails, for example, an
exception when trying to download the nupkg file, a process of
returning the package folder to the correct place begins.  The first
thing that is attempted is to create the .chocolateyPending file in the
package folder.  However, as mentioned, this folder no longer exists in
the expected location.  As a result, it is possible for an exception to
be thrown, as the file cannot be created in the package folder.

This problem can lead to another related problem in that the package is
now "lost" from lib folder, and will not show up when doing for example
choco list.

Imagine you were attempting the following command, and an exception
happened during the downloading of the nupkg:

choco upgrade audacity

Chocolatey CLI would start the upgrade operation, and then it would
fail downloading the package.  It would attempt to create the
.chocolateyPending file, and another exception would be thrown.  The
package folder is not returned to the lib folder, and now the package
is "lost".  If you attempt to run choco upgrade audacity again, the
operation would succeed, as it would see that the package is not
installed and it would be installed, however, if this happened during
the process of running choco upgrade all, then the underlying audacity
application would not be upgraded, as Chocolatey CLI is no longer
managing the package.

This commit addresses the issue by first checking to see if the package
folder exists.  If it doesn't, then it doesn't attempt to create the
.chocolateyPending file, and simply returns early.  This then allows
the remaining operations, i.e. returning the package folder from the
lib-bkp folder, back to the lib folder.
  • Loading branch information
gep13 committed Aug 13, 2024
1 parent 45e0383 commit 86c88e0
Showing 1 changed file with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,11 @@ public virtual void MarkPackagePending(PackageResult packageResult, ChocolateyCo
return;
}

if (!_fileSystem.DirectoryExists(packageDirectory))
{
return;
}

if (packageDirectory.IsEqualTo(ApplicationParameters.InstallLocation) || packageDirectory.IsEqualTo(ApplicationParameters.PackagesLocation))
{
packageResult.Messages.Add(
Expand Down

0 comments on commit 86c88e0

Please sign in to comment.