CurrentRouteMatch::getCurrentRouteMatch throws TypeError when current request is null

Created on 15 July 2025, 12 days ago

Problem/Motivation

When invoked outside an interactive request, e.g. from drush, Drupal's request is null.
\Drupal\Core\Routing\CurrentRouteMatch::getCurrentRouteMatch assumes that it is not null, resulting in a fatal TypeError.

Steps to reproduce

For my case, this came up when rebuilding cache with a views_data_export view.
The route-building process elicited this error.

Proposed resolution

Check for a request before invoking getRouteMatch, and return an dummy/empty RouteMatch object if current reqeust is null.

Remaining tasks

Tests

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.2 πŸ”₯

Component

routing system

Created by

πŸ‡ΊπŸ‡ΈUnited States AaronBauman Philadelphia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @AaronBauman
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies β†’ .

  • @aaronbauman opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States AaronBauman Philadelphia
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Ran the test-only feature

    1) Drupal\Tests\Core\Routing\CurrentRouteMatchTest::testGetCurrentRouteObject
    TypeError: Drupal\Core\Routing\CurrentRouteMatch::getRouteMatch(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, null given, called in /builds/issue/drupal-3536072/core/lib/Drupal/Core/Routing/CurrentRouteMatch.php on line 87
    /builds/issue/drupal-3536072/core/lib/Drupal/Core/Routing/CurrentRouteMatch.php:99
    /builds/issue/drupal-3536072/core/lib/Drupal/Core/Routing/CurrentRouteMatch.php:87
    /builds/issue/drupal-3536072/core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php:80
    ERRORS!
    Tests: 10, Assertions: 25, Errors: 1, PHPUnit Deprecations: 11.
    Exiting with EXIT_CODE=2
    

    IS is clear (thank you!)

    Think returning NullRouteMatch is a good compromise.

    LGTM.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Left a comment, not changing status because it's a nit.

Production build 0.71.5 2024