Skip to content

Commit 4f5fa2e

Browse files
authored
Merge pull request #351 from asgrim/limit-failed-make-output
Restrict failed make output in exceptions
2 parents 149a051 + 055a0fc commit 4f5fa2e

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/Building/UnixBuild.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Php\Pie\Platform\TargetPhp\PhpizePath;
1111
use Php\Pie\Platform\TargetPlatform;
1212
use Php\Pie\Util\Process;
13+
use Php\Pie\Util\ProcessFailedWithLimitedOutput;
1314
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Process\Exception\ProcessFailedException;
1416
use Symfony\Component\Process\Process as SymfonyProcess;
1517

1618
use function count;
@@ -78,7 +80,11 @@ public function __invoke(
7880
$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
7981
$output->writeln('<info>Configure complete</info>' . $optionsOutput);
8082

81-
$this->make($targetPlatform, $downloadedPackage, $output, $outputCallback);
83+
try {
84+
$this->make($targetPlatform, $downloadedPackage, $output, $outputCallback);
85+
} catch (ProcessFailedException $p) {
86+
throw ProcessFailedWithLimitedOutput::fromProcessFailedException($p);
87+
}
8288

8389
$expectedSoFile = $downloadedPackage->extractedSourcePath . '/modules/' . $downloadedPackage->package->extensionName()->name() . '.so';
8490

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Pie\Util;
6+
7+
use Symfony\Component\Process\Exception\ProcessFailedException;
8+
use Symfony\Component\Process\Process;
9+
10+
use function file_put_contents;
11+
use function sprintf;
12+
use function strlen;
13+
use function sys_get_temp_dir;
14+
use function uniqid;
15+
16+
use const DIRECTORY_SEPARATOR;
17+
18+
class ProcessFailedWithLimitedOutput extends ProcessFailedException
19+
{
20+
public static function fromProcessFailedException(ProcessFailedException $previous): self|ProcessFailedException
21+
{
22+
$process = $previous->getProcess();
23+
24+
if (strlen($process->getOutput()) > 5000) {
25+
$tempOutputFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_make_output_', true);
26+
file_put_contents(
27+
$tempOutputFile,
28+
sprintf(
29+
"Output:\n================\n%s\n\nError Output:\n================\n%s",
30+
$process->getOutput(),
31+
$process->getErrorOutput(),
32+
),
33+
);
34+
35+
return new self($process, $tempOutputFile);
36+
}
37+
38+
return $previous;
39+
}
40+
41+
public function __construct(Process $process, string $fileWithOutput)
42+
{
43+
parent::__construct($process);
44+
45+
$error = sprintf(
46+
'The command "%s" failed. Output was saved in "%s" as it was too long' . "\n\nExit Code: %s(%s)\n\nWorking directory: %s",
47+
$process->getCommandLine(),
48+
$fileWithOutput,
49+
$process->getExitCode() ?? '?',
50+
$process->getExitCodeText() ?? '?',
51+
$process->getWorkingDirectory() ?? '?',
52+
);
53+
54+
$this->message = $error;
55+
}
56+
}

0 commit comments

Comments
 (0)