From 643163a281f17ad17d5c71a98a85dc86d23bb3b2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 17 Apr 2024 18:23:48 +0200 Subject: [PATCH] Try renaming to catch and rethrow with permissions hint See https://github.com/hydephp/cli/issues/60 --- app/Commands/SelfUpdateCommand.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/Commands/SelfUpdateCommand.php b/app/Commands/SelfUpdateCommand.php index b3bc0639..2b51e6fc 100644 --- a/app/Commands/SelfUpdateCommand.php +++ b/app/Commands/SelfUpdateCommand.php @@ -274,7 +274,18 @@ protected function replaceApplication(string $downloadedFile): void $this->debug("Moving file $downloadedFile to $applicationPath"); // Replace the current application with the downloaded one - rename($downloadedFile, $applicationPath); + try { + // This might give Permission denied if we can't write to the bin path (might need sudo) + rename($downloadedFile, $applicationPath); + } catch (Throwable $exception) { + // Check if it is a permission issue + if (Str::containsAll($exception->getMessage(), ['rename', 'Permission denied'])) { + throw new RuntimeException('The application path is not writable. Please rerun the command with elevated privileges (e.g. using sudo).', 126, $exception); + } + + // Unknown error, rethrow the exception + throw $exception; + } } protected function updateViaComposer(): void