Problem/Motivation

DrupalKernel::bootEnvironment is responsible for setting up some important values in global scope before the kernel is booted. This pre-boot setup must only be performed once throughout a request. However, the application kernel is instantiated and booted repeatedly during tests. In order to protect against reinitialization, bookEnvironment maintains a static flag on DrupalKernel.

Closer inspection of bootEnvironment suggests that a large part of the code only makes sense when running as web application. It looks like delegating the responsibility of bootEnvironment might make it easier to adapt DrupalKernel to execution environments other than web servers.

Proposed resolution

Extract DrupalKernel::bootEnvironment into a set of classes under the namespace \Drupal\Core\SAPI and separate code paths for CLI and web SAPIs.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork drupal-2690035

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

znerol created an issue. See original summary.

znerol’s picture

StatusFileSize
new27.49 KB
znerol’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: extract-2690035-2-0.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new27.48 KB
new424 bytes

Status: Needs review » Needs work

The last submitted patch, 5: extract-2690035-5.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new28.07 KB
new2.62 KB
znerol’s picture

StatusFileSize
new28.14 KB
new1.95 KB

The last submitted patch, 7: extract-2690035-7.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 8: extract-2690035-8.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new27.49 KB
new1.68 KB
dawehner’s picture

Just some quick feedback.

  1. +++ b/core/lib/Drupal/Core/SAPI/HttpSAPIAdapter.php
    @@ -0,0 +1,74 @@
    +        // Web tests are to be conducted with runtime assertions active.
    +        assert_options(ASSERT_ACTIVE, TRUE);
    +        // Now synchronize PHP 5 and 7's handling of assertions as much as
    +        // possible.
    +        \Drupal\Component\Assertion\Handle::register();
    
    +++ b/core/lib/Drupal/Core/SAPI/SAPIAdapter.php
    @@ -0,0 +1,46 @@
    +  public function prepareEnvironment($root, $environment) {
    

    Is there a reason why we don't add assertions here?

  2. +++ b/core/modules/simpletest/src/WebTestBase.php
    @@ -234,12 +235,20 @@
    +   * The SAPI adapter.
    +   *
    +   * @var \Drupal\Core\SAPI\SAPIAdapterInterface.
    +   */
    +  protected $sapiAdapter;
    

    This variable isn't used

Status: Needs review » Needs work

The last submitted patch, 11: extract-2690035-2-0.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new30.36 KB
new2.45 KB

#11 was the wrong patch :/, interdiff is against #8.

Is there a reason why we don't add assertions here?

Because we only turn assertions on at that early stage when running inside a web-test child site.

This variable isn't used

Right, let's use it.

Status: Needs review » Needs work

The last submitted patch, 14: extract-2690035-14.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new30.36 KB
new812 bytes

Status: Needs review » Needs work

The last submitted patch, 16: extract-2690035-16.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new33.69 KB
new5.44 KB
znerol’s picture

StatusFileSize
new33.65 KB
new3.76 KB

#18 contains changes I did not intend to publish... this should be better, interdiff is against #16.

The last submitted patch, 18: extract-2690035-18.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 19: extract-2690035-19.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new561 bytes
new33.65 KB

Status: Needs review » Needs work

The last submitted patch, 22: extract-2690035-22.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new2.25 KB
new32.83 KB

PHPUnit tests are executed using the phpunit command line, hence TestRunnerKernel must not use NullSAPIAdapter.

Status: Needs review » Needs work

The last submitted patch, 24: extract-2690035-24.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new646 bytes
new32.83 KB

More Python than PHP work lately :)

mile23’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Needs a reroll.

+1 on the idea because separation of concerns is good. Some review stuff:

  1. +++ b/core/lib/Drupal/Core/SAPI/CliSAPIAdapter.php
    @@ -0,0 +1,32 @@
    +class CliSAPIAdapter extends SAPIAdapter {
    
    +++ b/core/lib/Drupal/Core/SAPI/HttpSAPIAdapter.php
    @@ -0,0 +1,74 @@
    +class HttpSAPIAdapter extends SAPIAdapter {
    
    +++ b/core/lib/Drupal/Core/SAPI/NullSAPIAdapter.php
    @@ -0,0 +1,30 @@
    +class NullSAPIAdapter implements SAPIAdapterInterface {
    
    +++ b/core/lib/Drupal/Core/SAPI/SAPIAdapter.php
    @@ -0,0 +1,46 @@
    +class SAPIAdapter implements SAPIAdapterInterface {
    
    +++ b/core/lib/Drupal/Core/SAPI/SAPIAdapterInterface.php
    @@ -0,0 +1,34 @@
    +interface SAPIAdapterInterface {
    

    Drupal coding standards say we should CamelCase acronyms: https://www.drupal.org/node/608152#naming

  2. +++ b/core/lib/Drupal/Core/SAPI/CliSAPIAdapter.php
    @@ -0,0 +1,32 @@
    +/**
    + * @file
    + * Contains \Drupal\Core\SAPI\CliSAPIAdapter.
    + */
    
    +++ b/core/lib/Drupal/Core/SAPI/HttpSAPIAdapter.php
    @@ -0,0 +1,74 @@
    +/**
    + * @file
    + * Contains \Drupal\Core\SAPI\HttpSAPIAdapter.
    + */
    
    +++ b/core/lib/Drupal/Core/SAPI/NullSAPIAdapter.php
    @@ -0,0 +1,30 @@
    +/**
    + * @file
    + * Contains \Drupal\Core\NullSAPIAdapter.
    + */
    
    +++ b/core/lib/Drupal/Core/SAPI/SAPIAdapter.php
    @@ -0,0 +1,46 @@
    +/**
    + * @file
    + * Contains \Drupal\Core\SAPI\SAPIAdapter.
    + */
    
    +++ b/core/lib/Drupal/Core/SAPI/SAPIAdapterInterface.php
    @@ -0,0 +1,34 @@
    +/**
    + * @file
    + * Contains \Drupal\Core\SAPI\SAPIAdapterInterface.
    + */
    

    We don't use @file tags just for Contains... any more.

  3. +++ b/core/rebuild.php
    @@ -25,8 +26,8 @@
     // Manually resemble early bootstrap of DrupalKernel::boot().
    -require_once __DIR__ . '/includes/bootstrap.inc';
    -DrupalKernel::bootEnvironment();
    +$sapi_adapter = SAPIAdapter::createFromGlobals();
    +$sapi_adapter->prepareEnvironment(__DIR__ . '/../', 'prod');
    

    Removing require_once for .inc files makes Mile23 happy. :-)

    (Just to be clear: This isn't something you need to change.)

kostyashupenko’s picture

Status: Needs work » Needs review
StatusFileSize
new80.07 KB

Looks like core/modules/simpletest/src/BrowserTestBase.php file is almost empty in 8.2.x for now. That's why this reroll is so massive

Status: Needs review » Needs work

The last submitted patch, 28: extract-2690035-28.patch, failed testing.

deepakaryan1988’s picture

Assigned: Unassigned » deepakaryan1988
deepakaryan1988’s picture

Assigned: deepakaryan1988 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new72.87 KB

Rerolled the patch #28

Status: Needs review » Needs work

The last submitted patch, 31: extract-2690035-31.patch, failed testing.

deepakaryan1988’s picture

Why is it not showing error? :(

deepakaryan1988’s picture

Status: Needs work » Needs review
StatusFileSize
new79.64 KB

Sorry forget to put newly created files in a last patch.

Status: Needs review » Needs work

The last submitted patch, 34: extract-2690035-34.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

billywardrop’s picture

We are working on this at DrupalCon Dublin

billywardrop’s picture

StatusFileSize
new80.85 KB

Patch attached

billywardrop’s picture

Status: Needs work » Needs review

This patch needs reviewed

billywardrop’s picture

Issue tags: -Needs reroll +Needs Review

Status: Needs review » Needs work

The last submitted patch, 38: extract-2690035-37.patch, failed testing.

mile23’s picture

Issue tags: -Needs Review

There's no such thing as 'needs review' tag. Setting the status is fine.

Unfortunately there are quite a few failing tests still, so we're back to 'needs work.'

mile23’s picture

+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -280,18 +281,21 @@ public static function createFromRequest(Request $request, $class_loader, $envir
-    $this->root = $app_root;
+    $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));

I'm pretty sure this is a bad re-roll, since we did a bunch of work to inject $app_root.

Are you re-rolling from 8.3.x?

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mile23’s picture

Effort to isolate the test fixture part of bootEnvironment(): #2969741: Deprecate simpletest within DrupalKernel (DRUPAL_TEST_IN_CHILD_SITE)

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bradjones1’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs work » Closed (duplicate)
kingdutch’s picture

Title: Extract DrupalKernel::bootEnvironment into SAPI adapter » [PP-1] Extract DrupalKernel::bootEnvironment into SAPI adapter
Assigned: Unassigned » kingdutch
Status: Closed (duplicate) » Postponed

Reopening this but postponing on #3313404: Use symfony/runtime for less bespoke bootstrap/compatibility with varied runtime environments so that that issue can remain small and focused on adding the runtime to Drupal but this issue moves over the actual environment set-up.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

dww’s picture

Coming here from #3453474: CLI entry point in Drupal Core. Ran head-first into this weirdness since the new CLI was trying to call bootEnvironment() but that created a world of hurt for running commands inside tests since bootEnvironment() does the set_error_handler() call yet tests don't want their own error handler to be forgotten. But since bootEnvironment() happens only once ever (due to the static flag), it's hard/impossible for the new DrupalApplication entrypoint to responsibly restore the error handler after it completes. Ideally, we'd have a way to get most of bootEnvironment() without messing with the error handler, so the CLI could get the goods without the headache of swapping error handlers. Or we move the error handler stuff to something that is tied to each DrupalKernel so at least we can safely restore once DrupalKernel is done. Or something. 😅

mradcliffe’s picture

Title: [PP-1] Extract DrupalKernel::bootEnvironment into SAPI adapter » Extract DrupalKernel::bootEnvironment into SAPI adapter
Status: Postponed » Needs work
andypost’s picture

dww’s picture

Started some of the work from here at #3597692: [regression] The `dr` command doesn't set DRUPAL_TEST_IN_CHILD_SITE to deal with a test-only regression from the new dr CLI entry point.