Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When _drupal_log_error()
is called with a fatal error, the PHP process exits with a zero:
if (PHP_SAPI === 'cli') {
if ($fatal) {
// When called from CLI, simply output a plain text message.
// Should not translate the string to avoid errors producing more errors.
$response->setContent(html_entity_decode(strip_tags(SafeMarkup::format('%type: @message in %function (line %line of %file).', $error))) . "\n");
$response->send();
exit;
}
}
This causes CLI tools like Behat to exit 0 even though a fatal was raised, confusing CI tools that rely on the exit code.
I'd guess that just exit(1);
would be enough here.
Proposed resolution
Remaining tasks
User interface changes
None
API changes
CLI tools that cause a fatal error now exit with an error status, if they use Drupal's default error handler.
Data model changes
None
Release notes snippet
CLI tools that cause a fatal error now exit with an error status, if they use Drupal's default error handler.
Comment | File | Size | Author |
---|---|---|---|
#30 | 2927012-30.patch | 2.35 KB | deviantintegral |
#28 | 2927012-28.patch | 2.42 KB | alexpott |
#28 | 2927012-28.test-only.patch | 1.89 KB | alexpott |
#28 | 25-28-interdiff.txt | 3.15 KB | alexpott |
#25 | 2927012.24-log-error-exit-code.patch | 3.49 KB | deviantintegral |
Comments
Comment #2
arunkumarkComment #3
arunkumarkI have created the patch for returning the true value.
Comment #4
arunkumarkComment #5
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedLooks fine - any thoughts on how to add test coverage for this?
Comment #6
dawehnerI fear we would need a test CLI script which calls some code, which causes the fatal.
Comment #7
m4olivei+1 to this, I've seen two instances of Behat silently failing as a result of changes in Behat's dependencies.
@dawehner is there precedent for what you propose?
Comment #8
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedHere's a "unit" test that covers this using the Process component. Open to suggestions if
log-exit.php
should live somewhere else.Comment #10
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedI thought that test discovery only tried to inspect files ending in
Test
, but apparently not. This renames the test script tolog-error.sh
, which is annoying for editors that try to render as bash but at least matches what core does in core/scripts.Comment #12
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedFixing a namespace typo that caused run-tests to fail.
Comment #13
m4oliveiLooks great! Test makes sense, minimal POC script that ultimately calls _drupal_log_error(), then using Symfony Process component to assert the exit status.
Comment #15
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedThis bug fix would be great to get in 8.5.x and even 8.4.x if another point release is going to be made.
Comment #17
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedComment #18
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedLooks like the prior test failure was unrelated. Tests reran and passed.
Comment #19
alexpottThis test should be in the core namespace not system since the function under test is core's not system's. Ie.
Drupal\Tests\Core\Error
I think we should keep this .php - we have more and more scripts going in as .php which makes everything easy. Also let's put this in core/tests/Drupal/Tests/Core/Error/fixtures
Comment #20
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedThis moves the test and adds the SAPI check.
I can't find any examples of fixtures using the
.php
suffix - the closest is something likecore/tests/Drupal/Tests/Core/Form/fixtures/form_base_test.inc
. If the fixture is namedlog-exit.php
this ReflectionException is thrown:Comment #21
alexpottAh that's our magical test discovery assuming everything is a test :)
We have core/tests/fixtures for PHP fixtures that should not be discovered. Imo we should file an issue to improve TestDiscovery to ignore things in fixture directories. But for now we can move this file there and use php.
Needs updating to the new namespace.
Comment #22
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedGood catch on the namespace comment.
This patch fixes that along with moving the test script. As well, it explicitly tests the PHP output so errors like invalid PHP or a bad path don't give us a false pass.
Comment #23
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedComment #25
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedStraight reroll against 8.7.x.
Comment #26
volegerComment #27
mtiftLooks good to me
Comment #28
alexpottWell can make the test a bit more contained, the script a bit less complex and also less PHP code lying around.
Comment #30
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedTIL'ed about
PhpProcess
. Nice!#28 looks fine, but
$path
is now unused. It's such a minor fix that if tests pass I will bump this to RTBC.Comment #31
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedComment #33
larowlanCan we get a change record for this - even though its a bug, people might have tests that now start (correctly!) failing and it would be good if they went to list-changes if they saw this.
Comment #34
alexpottCreated https://www.drupal.org/node/3058400
Comment #35
alexpottUpdated the issue summary. I think we should consider this a bug fix that should go in 8.7.x so I've created the CR with those values and tagged the issue accordingly. However, if others feel we can only do this in 8.8.x, so be it, as getting this fixed is more important than arguing which release it belongs in.
Comment #36
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedAgreed this should go in 8.7.x, especially as this affects fatal errors.
Change record looks good to me.
Comment #39
larowlanCommitted 08879cc and pushed to 8.8.x. Thanks!
c/p as 09bfe7fa12 to 8.7
Published change record.