Skip to content

Commit 3cd6416

Browse files
committed
Do not always ask to report bug about an internal error
1 parent 0b95634 commit 3cd6416

File tree

8 files changed

+57
-16
lines changed

8 files changed

+57
-16
lines changed

src/Analyser/InternalError.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
private string $contextDescription,
2727
private array $trace,
2828
private ?string $traceAsString,
29+
private bool $shouldReportBug,
2930
)
3031
{
3132
}
@@ -71,12 +72,17 @@ public function getTraceAsString(): ?string
7172
return $this->traceAsString;
7273
}
7374

75+
public function shouldReportBug(): bool
76+
{
77+
return $this->shouldReportBug;
78+
}
79+
7480
/**
7581
* @param mixed[] $json
7682
*/
7783
public static function decode(array $json): self
7884
{
79-
return new self($json['message'], $json['contextDescription'], $json['trace'], $json['traceAsString']);
85+
return new self($json['message'], $json['contextDescription'], $json['trace'], $json['traceAsString'], $json['shouldReportBug']);
8086
}
8187

8288
/**
@@ -90,6 +96,7 @@ public function jsonSerialize()
9096
'contextDescription' => $this->contextDescription,
9197
'trace' => $this->trace,
9298
'traceAsString' => $this->traceAsString,
99+
'shouldReportBug' => $this->shouldReportBug,
93100
];
94101
}
95102

src/Command/AnalyseCommand.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Console\Output\StreamOutput;
3030
use Throwable;
3131
use function array_intersect;
32+
use function array_key_exists;
3233
use function array_keys;
3334
use function array_map;
3435
use function array_unique;
@@ -332,10 +333,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
332333
$internalErrors = [];
333334
foreach ($analysisResult->getInternalErrorObjects() as $internalError) {
334335
$internalErrors[$internalError->getMessage()] = new InternalError(
335-
sprintf('Internal error: %s', $internalError->getMessage()),
336+
$internalError->getTraceAsString() !== null ? sprintf('Internal error: %s', $internalError->getMessage()) : $internalError->getMessage(),
336337
$internalError->getContextDescription(),
337338
$internalError->getTrace(),
338339
$internalError->getTraceAsString(),
340+
$internalError->shouldReportBug(),
339341
);
340342
}
341343
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
@@ -344,39 +346,56 @@ protected function execute(InputInterface $input, OutputInterface $output): int
344346
}
345347

346348
$message = $fileSpecificError->getMessage();
347-
if ($fileSpecificError->getIdentifier() === 'phpstan.internal') {
349+
$metadata = $fileSpecificError->getMetadata();
350+
if (
351+
$fileSpecificError->getIdentifier() === 'phpstan.internal'
352+
&& array_key_exists(InternalError::STACK_TRACE_AS_STRING_METADATA_KEY, $metadata)
353+
) {
348354
$message = sprintf('Internal error: %s', $message);
349355
}
350356

351-
$metadata = $fileSpecificError->getMetadata();
352357
$internalErrors[$fileSpecificError->getMessage()] = new InternalError(
353358
$message,
354359
sprintf('analysing file %s', $fileSpecificError->getTraitFilePath() ?? $fileSpecificError->getFilePath()),
355360
$metadata[InternalError::STACK_TRACE_METADATA_KEY] ?? [],
356361
$metadata[InternalError::STACK_TRACE_AS_STRING_METADATA_KEY] ?? null,
362+
true,
357363
);
358364
}
359365

360366
$internalErrors = array_values($internalErrors);
361367
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';
362368
foreach ($internalErrors as $i => $internalError) {
363369
$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
364-
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
365-
$firstTraceItem = $internalError->getTrace()[0] ?? null;
366-
$trace = '';
367-
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
368-
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
370+
if ($internalError->getTraceAsString() !== null) {
371+
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
372+
$firstTraceItem = $internalError->getTrace()[0] ?? null;
373+
$trace = '';
374+
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
375+
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
376+
}
377+
$trace .= $internalError->getTraceAsString();
378+
379+
if ($internalError->shouldReportBug()) {
380+
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n", $bugReportUrl, "\n", $trace);
381+
} else {
382+
$message .= sprintf('%s%s', "\n\n", $trace);
383+
}
384+
} else {
385+
if ($internalError->shouldReportBug()) {
386+
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s%s', "\n\n", "\n", $bugReportUrl, "\n");
387+
} else {
388+
$message .= sprintf('%sRun PHPStan with -v option to see the stack trace', "\n");
389+
}
369390
}
370-
$trace .= $internalError->getTraceAsString();
371-
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n\n", $bugReportUrl, "\n", $trace);
372-
} else {
373-
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s', "\n", "\n", $bugReportUrl);
374391
}
392+
375393
$internalErrors[$i] = new InternalError(
376394
$message,
377395
$internalError->getContextDescription(),
378396
$internalError->getTrace(),
379397
$internalError->getTraceAsString(),
398+
$internalError->shouldReportBug(),
380399
);
381400
}
382401

src/Command/FixerApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ private function analyse(
485485
'running PHPStan Pro worker',
486486
InternalError::prepareTrace($e),
487487
$e->getTraceAsString(),
488+
false,
488489
)],
489490
]]);
490491
throw $e;

src/Command/FixerWorkerCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use
242242
'running analyser in PHPStan Pro worker',
243243
[],
244244
null,
245+
false,
245246
),
246247
],
247248
]]);

src/Command/WorkerCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private function runWorker(
170170
'communicating with main process in parallel worker',
171171
InternalError::prepareTrace($error),
172172
$error->getTraceAsString(),
173+
true,
173174
),
174175
],
175176
'filteredPhpErrors' => [],
@@ -235,6 +236,7 @@ private function runWorker(
235236
sprintf('analysing file %s', $file),
236237
InternalError::prepareTrace($t),
237238
$t->getTraceAsString(),
239+
true,
238240
);
239241
}
240242
}

src/Parallel/ParallelAnalyser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function analyse(
9595
'running parallel worker',
9696
[],
9797
null,
98+
true,
9899
);
99100
$internalErrorsCount++;
100101
}
@@ -149,6 +150,7 @@ public function analyse(
149150
'communicating with parallel worker',
150151
InternalError::prepareTrace($error),
151152
$error->getTraceAsString(),
153+
!$error instanceof ProcessTimedOutException,
152154
);
153155
$internalErrorsCount++;
154156
$reachedInternalErrorsCountLimit = true;
@@ -299,7 +301,7 @@ public function analyse(
299301
$memoryLimitMessage,
300302
ini_get('memory_limit'),
301303
'Increase your memory limit in php.ini or run PHPStan with --memory-limit CLI option.',
302-
), 'running parallel worker', [], null);
304+
), 'running parallel worker', [], null, false);
303305
$internalErrorsCount++;
304306
return;
305307
}

src/Parallel/Process.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Parallel;
44

5-
use Exception;
65
use PHPStan\ShouldNotHappenException;
76
use React\EventLoop\LoopInterface;
87
use React\EventLoop\TimerInterface;
@@ -112,7 +111,7 @@ public function request(array $data): void
112111
$this->in->write($data);
113112
$this->timer = $this->loop->addTimer($this->timeoutSeconds, function (): void {
114113
$onError = $this->onError;
115-
$onError(new Exception(sprintf('Child process timed out after %.1f seconds. Try making it longer with parallel.processTimeout setting.', $this->timeoutSeconds)));
114+
$onError(new ProcessTimedOutException(sprintf('Child process timed out after %.1f seconds. Try making it longer with parallel.processTimeout setting.', $this->timeoutSeconds)));
116115
});
117116
}
118117

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Parallel;
4+
5+
use Exception;
6+
7+
class ProcessTimedOutException extends Exception
8+
{
9+
10+
}

0 commit comments

Comments
 (0)