Problem/Motivation

Steps to reproduce

Working with @travis.carden to get his tests running locally we figure the problem was that \Drupal\package_manager\Validator\XdebugValidator was failing because he had xdebug on

In Build tests this caused a failure at \Drupal\Tests\automatic_updates\Build\CoreUpdateTest::testCron because $assert_session->pageTextContains('Your site is ready for automatic updates.'); because there was message about xdebug error on the status report

for build tests #3320792: Make build tests fail 1) more explicitly, 2) earlier when possible (failing StatusCheckEvent subscribers) will display the reasons "Your site is ready for automatic updates" is not displayed

In kernel tests it failed because of \Drupal\Tests\package_manager\Traits\ValidationTestTrait::assertValidationResultsEqual

failed at $this->assertCount(count($expected_results), $actual_results); where we expected only 1 result but we got 2. We still got the expect result but got an additional result for xdebug.
This is not true any more since #3320815: Make validation result comparison test messages more helpful you would get a clear indication in the error that extra error message is dealing with xdebug. See the summary of that issue for an example

Originally Neither 1 of these gave you any indication what the error was really. Now #3320815 gives a clear indication for kernel tests and #3320792 will give a clear indication for build tests.

Proposed resolution

If xdebug is on automatically fail the test early. This can be done by calling new method in \Drupal\Tests\package_manager\Traits\AssertPreconditionsTrait::setUpBeforeClass() this will then apply to all our tests.

Add an environmental var PACKAGE_MANAGER_ALLOW_XDEBUG_TESTING to turn to not do this. In our message to the user we would mention setting PACKAGE_MANAGER_ALLOW_XDEBUG_TESTING

we will update XdebugValidator to return early if PACKAGE_MANAGER_ALLOW_XDEBUG_TESTING was set to true.

We deciding against having a setting in settings.php like PACKAGE_MANAGER_ALLOW_XDEBUG_TESTING because this could accidently be pushed to production and in the worst case scenario people might try to apply a critical security release when they did not know xdebug was enabled and it would timeout in the apply stage leaving them half updated.

Remaining tasks

Comments

tedbow created an issue. See original summary.

tedbow’s picture

I think we should probably do 1) disable the validator everywhere except were we test it. PackageManagerKernelTestBase and AutomaticUpdatesFunctionalTestBase both have a $disableValidators property that make this easy but I can't remember for build tests.

traviscarden’s picture

wim leers’s picture

My input:

  1. 👎 because this makes the test behave differently from real world scenarios.
  2. 🤔 maybe because it's easy to miss the fact that a test is marked as risky.
  3. 👍 this is very much preferable. In the error message, we should inform the user of how to bypass this temporarily, which should only happen when debugging a test. This must NOT involve an environment variable, because of the risks already explained in the issue summary. Valid work-arounds are:
    • "add early return to \Drupal\package_manager\Validator\XdebugValidator::checkForXdebug"
    • "comment out the package_manager.validator.xdebug and automatic_updates.validator.xdebug services"

IOW: I disagree strongly with #2, because it modifies application state in functional aka integration tests, which is precisely where we're testing how the entire application works.

P.S.: why do we have both \Drupal\automatic_updates\Validator\XdebugValidator and \Drupal\automatic_updates\Validator\XdebugValidator? Either we should add the missing docs for that here, alternatively we need a follow-up issue for that.

tedbow’s picture

P.S.: why do we have both \Drupal\automatic_updates\Validator\XdebugValidator and \Drupal\automatic_updates\Validator\XdebugValidator? Either we should add the missing docs for that here, alternatively we need a follow-up issue for that.

\Drupal\automatic_updates\Validator\XdebugValidator just subscribes to StatusCheckEvent and adds a warning. This is meant to inform the user but will not stop operations. It is in package_manager so it is not Automatic Updates specific

\Drupal\automatic_updates\Validator\XdebugValidator is automatic_updates specific it subscribes to PreCreateEvent and adds an error if the stage is an instance of CronUpdater. This is because there will be no user to see the warning when cron is run.

Related I found this issue just now #3321206: The same xdebug warning shows on the status report 2 times

wim leers’s picture

What you wrote in #5 is documentation that's missing from \Drupal\package_manager\Validator\XdebugValidator.

wim leers’s picture

Assigned: Unassigned » tedbow
Priority: Normal » Critical

This blocked @kunal.sachdev for hours 😬

This just cost us 30 minutes of a pairing session to figure out the root cause for! 😭 (Issue: #3312619: Ensure all validation results use translatable strings except when created from throwables.)

This very much impedes productivity. This needs to be solved ASAP.

wim leers’s picture

Issue tags: +sprint
tedbow’s picture

Update the summary to detail how 2 related issues make the problem not as bad. #3320792: Make build tests fail 1) more explicitly, 2) earlier when possible (failing StatusCheckEvent subscribers) still needs to be finished but it will make the build test problem much clearer and also apply to any other problem that we check for in status checks

Set the proposed solution

yash.rode’s picture

Assigned: Unassigned » yash.rode

Before starting to work on this, according to the proposed solution if we fail the test if xdebug is on, won't it be a problem for debugging?

yash.rode’s picture

Assigned: yash.rode » tedbow
Status: Active » Needs review

Discussed this with @wim on a call today so, we are not facing this problem anymore, we get clear message that the error in the test is caused by XDebug being on. So, this issue can be closed as outdated, @omakr.podey and @kunal.sachdev agrees with this!

wim leers’s picture

Assigned: tedbow » Unassigned
Status: Needs review » Closed (outdated)

To expand on #11: I marked this critical because while mentoring @yash.rode, @omkar.podey and @kunal.sachdev in early November — when I just got started on AU — they ran into problems with debugging of tests all the time.

Since then, I've helped them get their environments in order, and they now all use xdebug off and xdebug on commands. For the past ~6 weeks they have not run into problems around this anymore. So … closing 😊

tedbow’s picture

Ok since #3320792: Make build tests fail 1) more explicitly, 2) earlier when possible (failing StatusCheckEvent subscribers) should make the situation better for build tests I am fine closing this.

Let keep in mind that this issue is here if we other problems raised by having xdebug on it testing

wim leers’s picture

Title: xdebug being enabled causes tests to fail without clear indication that it is the problem » Xdebuxdebug being enabled causes tests to fail without clear indication that it is the problem
Assigned: Unassigned » wim leers
Priority: Critical » Normal
Status: Closed (outdated) » Needs work

Just discussed in call.

Rescoping this to instead of doing this as a precondition prior to running tests:

  1. making \Drupal\automatic_updates\Validator\XdebugValidator aware of whether it runs in a test or not, and NOT complain about xdebug if we're in a test
  2. getting rid of all the $this->disableValidators[] = 'package_manager.validator.xdebug'; cases
wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB

This implements #14.2

wim leers’s picture

Title: Xdebuxdebug being enabled causes tests to fail without clear indication that it is the problem » xdebug being enabled causes tests to fail without clear indication that it is the problem
StatusFileSize
new3.39 KB

\Drupal\Tests\automatic_updates\Kernel\StatusCheck\StatusCheckerTest::testGetResults() is an example of a kernel test that will fail with xdebug on, \Drupal\Tests\automatic_updates\Functional\StatusCheckTest::testStatusChecksOnStatusReport is an example of a functional test.

Let's find out what other tests will fail due to #15, by pretending xdebug is always enabled… 🤓

+++ b/package_manager/src/Validator/XdebugValidator.php
@@ -40,7 +40,7 @@ class XdebugValidator implements EventSubscriberInterface {
   protected function checkForXdebug(): ?array {
-    if (function_exists('xdebug_break')) {
+    if (TRUE || function_exists('xdebug_break')) {
wim leers’s picture

StatusFileSize
new1.05 KB
new3.7 KB

This makes XdebugValidator correctly detect when it's running in a test, and then it won't do anything.

The last submitted patch, 16: 3320782-16-test-only-FAIL.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 17: 3320782-17.patch, failed testing. View results

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new2.07 KB
new4.74 KB

Adjust the two XdebugValidatorTests.

Status: Needs review » Needs work

The last submitted patch, 20: 3320782-18.patch, failed testing. View results

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.52 KB
new5.47 KB

Making XdebugValidator not fail in build tests was especially tricky :O That actually already didn't work correctly in HEAD either, because #15 touched nothing on the build test side…

… but I never ever want to have to revisit this, so I spent an hour or so trying to find a way that this could reliably be detected. I think I just managed to find a way! 2🤓

wim leers’s picture

Assigned: Unassigned » phenaproxima
Issue tags: +maintainability
StatusFileSize
new726 bytes
new5.41 KB

Yay! That worked 🥳

Time to stop simulating Xdebug being on at all times now!

phenaproxima’s picture

Only a couple of minor points which, honestly, don't block commit in my opinion. But, feel free to implement if you agree.

  1. +++ b/package_manager/src/Validator/XdebugValidator.php
    @@ -40,6 +41,19 @@ class XdebugValidator implements EventSubscriberInterface {
    +    // Xdebug is allowed to be enabled while running tests, for debugging
    +    // purposes. It's just not allowed to be enabled while using Package Manager
    +    // in a real environment.
    +    // @see \Drupal\Core\CoreServiceProvider::registerTest()
    +    $in_functional_test = drupal_valid_test_ua();
    +    // @see \Drupal\Core\DependencyInjection\DependencySerializationTrait::__wakeup()
    +    $in_kernel_test = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
    +    // @see \Drupal\BuildTests\Framework\BuildTestBase::setUp()
    +    $in_build_test = str_contains(__FILE__, DrupalFilesystem::getOsTemporaryDirectory() . '/build_workspace_');
    +    if (($in_functional_test || $in_kernel_test || $in_build_test) && !function_exists('xdebug_break_TESTED')) {
    +      return NULL;
    +    }
    

    I wonder if this should be moved to a private static helper function, so as to keep a cleaner separation between production code and test code.

  2. +++ b/package_manager/tests/src/Kernel/XdebugValidatorTest.php
    @@ -21,6 +21,8 @@ class XdebugValidatorTest extends PackageManagerKernelTestBase {
    +      eval('function xdebug_break_TESTED() {}');
    

    We should probably add a @see the relevant part of XdebugValidator, since this otherwise looks kinda inexplicable.

  3. +++ b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
    @@ -44,8 +44,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
    -    // Always allow tests to run with Xdebug on.
    -    'package_manager.validator.xdebug',
    

    I love that we can finally get away from disabling the validator in tests!

wim leers’s picture

Assigned: phenaproxima » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new3.8 KB
new6.1 KB

#24:

  1. +1! Done.
  2. +1! Done.
  3. 😊

Self-RTBC'ing per #24.

tedbow’s picture

tedbow’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.