fix: #2701575 Guard RequestContext::fromRequestStack against an empty request stack

Resolves #2701575

Problem/Motivation

RequestContext::fromRequestStack() unconditionally calls $this->fromRequest($request_stack->getCurrentRequest()). When the request stack is empty (getCurrentRequest() returns NULL) this throws:

TypeError: Drupal\Core\Routing\RequestContext::fromRequest(): Argument #1
($request) must be of type Symfony\Component\HttpFoundation\Request, null
given

This happens during request-less CLI execution -- for example a route rebuild triggered from a module's install hook or event subscriber while running drush site:install -- where the request stack has no current request yet.

Superseded/related work: MR !3646 (closed) on this issue took a different approach (building a Request via Request::createFromGlobals() as a fallback). This MR instead leaves the context at its constructor defaults when there is no current request, which avoids constructing a synthetic request out of CLI SAPI globals and keeps the change to a minimal, defensible null guard.

Proposed resolution

Only call fromRequest() when $request_stack->getCurrentRequest() is non-NULL; otherwise leave RequestContext at its constructor defaults.

Remaining tasks

  • Unit test coverage for both the empty-stack and populated-stack cases (core/tests/Drupal/Tests/Core/Routing/RequestContextTest.php)
  • Review by a core maintainer

AI-Generated: Yes (Used Claude Code to identify the TypeError root cause, write the null-guard fix, and add unit test coverage)

Checkpoints

  • File an issue about this project
  • Addition/Change/Update/Fix to this project
  • Testing to ensure no regression
  • Automated unit/functional testing coverage
  • Developer Documentation support on feature change/addition
  • User Guide Documentation support on feature change/addition
  • UX/UI designer responsibilities
  • Accessibility and Readability
  • Reviewed by a human
  • Code review by maintainers
  • Full testing and approval
  • Credit contributors
  • Review with the product owner
  • Update Release Notes
  • Release
Edited by Rajab Natshah

Merge request reports

Loading
Loading