Skip to content

Commit 32dfba3

Browse files
bug #58723 [Process] Properly deal with not-found executables on Windows (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Process] Properly deal with not-found executables on Windows | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT Removing the unexpected notices that we see on appveyor: Before: ![image](https://github.com/user-attachments/assets/d9c788ad-7b29-4b0c-9170-aa08953a5ca2) After: ![image](https://github.com/user-attachments/assets/a3dc1cc7-ce4c-4fd0-b422-1a6c745bc3d9) Commits ------- 73140eb44e1 [Process] Properly deal with not-found executables on Windows
2 parents e2d11b6 + 651830b commit 32dfba3

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

ExecutableFinder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
7373
}
7474
}
7575

76-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
77-
if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && @is_executable($executablePath)) {
76+
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
77+
return $default;
78+
}
79+
80+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
81+
$execResult = exec(\sprintf($command, escapeshellarg($name)));
82+
83+
if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
7884
return $executablePath;
7985
}
8086

PhpExecutableFinder.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ public function find(bool $includeArgs = true)
3535
{
3636
if ($php = getenv('PHP_BINARY')) {
3737
if (!is_executable($php)) {
38-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
39-
if (\function_exists('exec') && $php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
40-
if (!is_executable($php)) {
41-
return false;
42-
}
43-
} else {
38+
if (!\function_exists('exec') || \strlen($php) !== strcspn($php, '/'.\DIRECTORY_SEPARATOR)) {
39+
return false;
40+
}
41+
42+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
43+
$execResult = exec(\sprintf($command, escapeshellarg($php)));
44+
if (!$php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
45+
return false;
46+
}
47+
if (!is_executable($php)) {
4448
return false;
4549
}
4650
}

0 commit comments

Comments
 (0)