From 8d24b61bef4839104db7f05c23400e598a8915e4 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 13 Jan 2020 15:50:34 +0100 Subject: [PATCH] Tweak and add comments to the working dir fix with global exec, refs #8515 --- src/Composer/Command/ExecCommand.php | 7 +++++-- src/Composer/Console/Application.php | 14 +++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Composer/Command/ExecCommand.php b/src/Composer/Command/ExecCommand.php index d6e49b4f9..d530def66 100644 --- a/src/Composer/Command/ExecCommand.php +++ b/src/Composer/Command/ExecCommand.php @@ -92,9 +92,12 @@ EOT $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); } - if (getcwd() !== $this->getApplication()->getWorkingDirectory()) { + // If the CWD was modified, we restore it to what it was initially, as it was + // most likely modified by the global command, and we want exec to run in the local working directory + // not the global one + if (getcwd() !== $this->getApplication()->getInitialWorkingDirectory()) { try { - chdir($this->getApplication()->getWorkingDirectory()); + chdir($this->getApplication()->getInitialWorkingDirectory()); } catch (\Exception $e) { throw new \RuntimeException('Could not switch back to working directory "'.$this->getApplication()->getWorkingDirectory().'"', 0, $e); } diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 65bb07f62..6de2ee5d4 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -63,10 +63,9 @@ class Application extends BaseApplication private $disablePluginsByDefault = false; /** - * @var string Store the working directory so Composer - * can switch back to it if there are issues + * @var string Store the initial working directory at startup time */ - private $workingDirectory = ''; + private $initialWorkingDirectory = ''; public function __construct() { @@ -97,7 +96,7 @@ class Application extends BaseApplication $this->io = new NullIO(); - $this->workingDirectory = getcwd(); + $this->initialWorkingDirectory = getcwd(); parent::__construct('Composer', Composer::getVersion()); } @@ -139,6 +138,7 @@ class Application extends BaseApplication if ($newWorkDir = $this->getNewWorkingDir($input)) { $oldWorkingDir = getcwd(); chdir($newWorkDir); + $this->initialWorkingDirectory = $newWorkDir; $io->writeError('Changed CWD to ' . getcwd(), true, IOInterface::DEBUG); } @@ -501,12 +501,12 @@ class Application extends BaseApplication } /** - * Get the working directoy + * Get the working directoy at startup time * * @return string */ - public function getWorkingDirectory() + public function getInitialWorkingDirectory() { - return $this->workingDirectory; + return $this->initialWorkingDirectory; } }