Skip to content

Commit 7527a94

Browse files
committed
fix: proc exec error on windows OS
1 parent 6780581 commit 7527a94

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/Proc/ProcWrapper.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class ProcWrapper
3131
private $command;
3232

3333
/**
34-
* @var string
34+
* @var string|null
3535
*/
36-
private $workDir = '';
36+
private $workDir;
3737

3838
/**
3939
* @var array
@@ -100,13 +100,18 @@ public static function new(string $command = '', array $descriptors = []): self
100100
*/
101101
public static function runCmd(string $command, string $workDir = ''): array
102102
{
103+
$isWindowsOS = '\\' === DIRECTORY_SEPARATOR;
103104
$descriptors = [
104105
0 => ['pipe', 'r'], // stdin - read channel
105106
1 => ['pipe', 'w'], // stdout - write channel
106107
2 => ['pipe', 'w'], // stdout - error channel
107108
3 => ['pipe', 'r'], // stdin - This is the pipe we can feed the password into
108109
];
109110

111+
if ($isWindowsOS) {
112+
unset($descriptors[3]);
113+
}
114+
110115
$proc = new ProcWrapper($command, $descriptors);
111116
$proc->run($workDir);
112117

@@ -121,12 +126,13 @@ public static function runCmd(string $command, string $workDir = ''): array
121126
$error = stream_get_contents($pipes[2]);
122127
fclose($pipes[2]);
123128

124-
// TODO: Write passphrase in pipes[3].
125-
fclose($pipes[3]);
129+
if (!$isWindowsOS) {
130+
// TODO: Write passphrase in pipes[3].
131+
fclose($pipes[3]);
132+
}
126133

127134
// Close all pipes before proc_close! $code === 0 is success.
128135
$code = $proc->close();
129-
130136
return [$code, $output, $error];
131137
}
132138

@@ -220,14 +226,22 @@ public function open(): self
220226
throw new InvalidArgumentException('The want execute command is cannot be empty');
221227
}
222228

223-
$workDir = $this->workDir;
229+
$workDir = $this->workDir ?: null;
224230
$options = $this->options;
225231

226232
$options['suppress_errors'] = true;
227233
if ('\\' === DIRECTORY_SEPARATOR) { // windows
228234
$options['bypass_shell'] = true;
229235
}
230236

237+
// ERROR on windows
238+
// proc_open(): CreateProcess failed, error code - 123
239+
//
240+
// https://docs.microsoft.com/zh-cn/windows/win32/debug/system-error-codes--0-499-
241+
// The filename, directory name, or volume label syntax is incorrect.
242+
// FIX:
243+
// 1. runCmd() not set $descriptors[3] on windows
244+
// 2. $workDir set as null when is empty.
231245
$process = proc_open($command, $this->descriptors, $this->pipes, $workDir, $this->runENV, $options);
232246

233247
if (!is_resource($process)) {
@@ -357,9 +371,9 @@ public function getPipes(): array
357371
}
358372

359373
/**
360-
* @return string
374+
* @return string|null
361375
*/
362-
public function getWorkDir(): string
376+
public function getWorkDir(): ?string
363377
{
364378
return $this->workDir;
365379
}

0 commit comments

Comments
 (0)