Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Possible BC break: nullable #[Autowire(service: ...)] now throws on missing service #64530

Copy link
Copy link
@Kocal

Description

@Kocal
Issue body actions

Symfony version(s) affected

8.1.0, and surely >6.4.36, >7.4.8, >8.0.8

Description

Spotted while investigating failed production deployments on https://ux.symfony.com after a dependency upgrade.

Previously, injecting a service by name via #[Autowire(service: '...')] on a nullable parameter silently resolved to null when the service did not exist in the current environment. Now, container compilation fails with an exception instead.

I believe this is a BC break. The nullable type on the parameter signals that the absence of the service is acceptable. The previous behavior respected that contract. The current behavior does not.

The error:

The service "App\Controller\Toolkit\ComponentsController::previewComponent()" has a dependency on a non-existent service "profiler".

Thrown by CheckExceptionOnInvalidReferenceBehaviorPass.php line 116.

Previously:

  • dev: $profiler is the real Profiler instance, ->disable() is nicely called
  • prod: profiler service does not exist, $profiler is null, no error.

Now:

  • prod: container compilation fails. The service is missing, and the nullable type is no longer enough to suppress the error. Production deploys of ux.symfony.com broke on this.

PR #63724 changed how invalid reference behavior is handled in CheckExceptionOnInvalidReferenceBehaviorPass

How to reproduce

class ComponentsController
{
    #[Route('/toolkit/component_preview', name: 'app_toolkit_component_preview')]
    public function previewComponent(
        /* ... */
        #[Autowire(service: 'profiler')]
        ?Profiler $profiler,
    ) {
        $profiler?->disable();
        // ...
    }
}

That you can find here.

Possible Solution

PR #63724 changed how invalid reference behavior is handled in CheckExceptionOnInvalidReferenceBehaviorPass

Additional Context

A nullable parameter decorated with #[Autowire(service: '...')] should resolve to null when the service is absent from the container, matching the previous behavior.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.