瀏覽代碼

Tweak and add comments to the working dir fix with global exec, refs #8515

Jordi Boggiano 5 年之前
父節點
當前提交
8d24b61bef
共有 2 個文件被更改,包括 12 次插入9 次删除
  1. 5 2
      src/Composer/Command/ExecCommand.php
  2. 7 7
      src/Composer/Console/Application.php

+ 5 - 2
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);
             }

+ 7 - 7
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;
     }
 }