From e591ef4db9aab14318157091a4e9a50fc6637c60 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 23 May 2013 14:42:07 -0700 Subject: [PATCH] Add setup checks for the availability of 'which' and 'diff' binaries Summary: Spent an hour or two helping a user figure this out. Make sure I never do that again. If the webserver is configured with an empty or bogus PATH, binaries like 'which' and 'diff' (and 'git', and 'svn', etc.) may not be available. In most cases, this is fine, because we get an error like "sh: whatever-command not found", which is obvious to diagnose. In the case of 'diff', we don't get this, because 'diff' is expected to exit with a nonzero code for differing files -- so we interpret the "sh: whatever-command not found" as "files differ" and then try to parse the empty output. Explicitly check for 'which' (on Windows, 'where') and 'diff' during setup (I plan to refine the behavior around 'git', 'svn' and 'hg' at some point, but this is less pressing since the errors are trivial to support). Test Plan: Faked failures on all modes, verified setup warnings look reasonable. Reviewers: btrahan, chad Reviewed By: btrahan CC: aran Differential Revision: https://secure.phabricator.com/D6008 --- src/__phutil_library_map__.php | 2 + .../check/PhabricatorSetupCheckBinaries.php | 95 +++++++++++++++++++ .../PhabricatorSetupCheckImagemagick.php | 9 +- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 src/applications/config/check/PhabricatorSetupCheckBinaries.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f32a1c0774..ecf48a5055 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1392,6 +1392,7 @@ phutil_register_library_map(array( 'PhabricatorSetupCheck' => 'applications/config/check/PhabricatorSetupCheck.php', 'PhabricatorSetupCheckAPC' => 'applications/config/check/PhabricatorSetupCheckAPC.php', 'PhabricatorSetupCheckBaseURI' => 'applications/config/check/PhabricatorSetupCheckBaseURI.php', + 'PhabricatorSetupCheckBinaries' => 'applications/config/check/PhabricatorSetupCheckBinaries.php', 'PhabricatorSetupCheckDatabase' => 'applications/config/check/PhabricatorSetupCheckDatabase.php', 'PhabricatorSetupCheckExtensions' => 'applications/config/check/PhabricatorSetupCheckExtensions.php', 'PhabricatorSetupCheckExtraConfig' => 'applications/config/check/PhabricatorSetupCheckExtraConfig.php', @@ -3147,6 +3148,7 @@ phutil_register_library_map(array( 'PhabricatorSettingsPanelSearchPreferences' => 'PhabricatorSettingsPanel', 'PhabricatorSetupCheckAPC' => 'PhabricatorSetupCheck', 'PhabricatorSetupCheckBaseURI' => 'PhabricatorSetupCheck', + 'PhabricatorSetupCheckBinaries' => 'PhabricatorSetupCheck', 'PhabricatorSetupCheckDatabase' => 'PhabricatorSetupCheck', 'PhabricatorSetupCheckExtensions' => 'PhabricatorSetupCheck', 'PhabricatorSetupCheckExtraConfig' => 'PhabricatorSetupCheck', diff --git a/src/applications/config/check/PhabricatorSetupCheckBinaries.php b/src/applications/config/check/PhabricatorSetupCheckBinaries.php new file mode 100644 index 0000000000..1040484948 --- /dev/null +++ b/src/applications/config/check/PhabricatorSetupCheckBinaries.php @@ -0,0 +1,95 @@ +raiseWarning($bin_name, $message); + + // We need to return here if we can't find the 'which' / 'where' binary + // because the other tests won't be valid. + return; + } + + if (!Filesystem::binaryExists('diff')) { + $message = pht( + "Without 'diff', Phabricator will not be able to generate or render ". + "diffs in multiple applications."); + $this->raiseWarning('diff', $message); + } else { + $tmp_a = new TempFile(); + $tmp_b = new TempFile(); + $tmp_c = new TempFile(); + + Filesystem::writeFile($tmp_a, 'A'); + Filesystem::writeFile($tmp_b, 'A'); + Filesystem::writeFile($tmp_c, 'B'); + + list($err) = exec_manual('diff %s %s', $tmp_a, $tmp_b); + if ($err) { + $this->newIssue('bin.diff.same') + ->setName(pht("Unexpected 'diff' Behavior")) + ->setMessage( + pht( + "The 'diff' binary on this system has unexpected behavior: ". + "it was expected to exit without an error code when passed ". + "identical files, but exited with code %d.", + $err)); + } + + list($err) = exec_manual('diff %s %s', $tmp_a, $tmp_c); + if (!$err) { + $this->newIssue('bin.diff.diff') + ->setName(pht("Unexpected 'diff' Behavior")) + ->setMessage( + pht( + "The 'diff' binary on this system has unexpected behavior: ". + "it was expected to exit with a nonzero error code when passed ". + "differing files, but did not.")); + } + } + } + + private function raiseWarning($bin, $message) { + if (phutil_is_windows()) { + $preamble = pht( + "The '%s' binary could not be found. Set the webserver's %s ". + "environmental variable to include the directory where it resides, or ". + "add that directory to '%s' in the Phabricator configuration.", + $bin, + 'PATH', + 'environment.append-paths'); + } else { + $preamble = pht( + "The '%s' binary could not be found. Symlink it into '%s', or set the ". + "webserver's %s environmental variable to include the directory where ". + "it resides, or add that directory to '%s' in the Phabricator ". + "configuration.", + $bin, + 'phabricator/support/bin/', + 'PATH', + 'environment.append-paths'); + } + + $this->newIssue('bin.'.$bin) + ->setShortName(pht("'%s' Missing", $bin)) + ->setName(pht("Missing '%s' Binary", $bin)) + ->setSummary( + pht("The '%s' binary could not be located or excuted.", $bin)) + ->setMessage($preamble.' '.$message) + ->addPhabricatorConfig('environment.append-paths'); + } + +} diff --git a/src/applications/config/check/PhabricatorSetupCheckImagemagick.php b/src/applications/config/check/PhabricatorSetupCheckImagemagick.php index ac244f3aca..be041518d2 100644 --- a/src/applications/config/check/PhabricatorSetupCheckImagemagick.php +++ b/src/applications/config/check/PhabricatorSetupCheckImagemagick.php @@ -5,12 +5,11 @@ final class PhabricatorSetupCheckImagemagick extends PhabricatorSetupCheck { protected function executeChecks() { $imagemagick = PhabricatorEnv::getEnvConfig('files.enable-imagemagick'); if ($imagemagick) { - list($err) = exec_manual('which convert'); - if ($err) { + if (!Filesystem::binaryExists('convert')) { $message = pht( - 'You have enabled Imagemagick in your config, but the \'convert\''. - ' binary is not in the webserver\'s $PATH. Disable imagemagick'. - ' or make it available to the webserver.'); + 'You have enabled Imagemagick in your config, but the \'convert\' '. + 'binary is not in the webserver\'s $PATH. Disable imagemagick '. + 'or make it available to the webserver.'); $this->newIssue('files.enable-imagemagick') ->setName(pht(