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.
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 tonullwhen 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:
Thrown by
CheckExceptionOnInvalidReferenceBehaviorPass.phpline 116.Previously:
$profileris the realProfilerinstance,->disable()is nicely calledprofilerservice does not exist,$profilerisnull, no error.Now:
PR #63724 changed how invalid reference behavior is handled in
CheckExceptionOnInvalidReferenceBehaviorPassHow to reproduce
That you can find here.
Possible Solution
PR #63724 changed how invalid reference behavior is handled in
CheckExceptionOnInvalidReferenceBehaviorPassAdditional Context
A nullable parameter decorated with
#[Autowire(service: '...')]should resolve tonullwhen the service is absent from the container, matching the previous behavior.