Skip to content

[DependencyInjection] Autowiring default gets lost when configuring multiple environments in single yaml file #45592

@rvanlaak

Description

@rvanlaak

Symfony version(s) affected

6.0.5

Description

Overriding an interface implementation (with default autowiring enabled) for a specific environment will fail when the specific implementation has dependencies that should get injected.

We use this when@test feature to wire a different service that does not connect to the database in test environments.

References

How to reproduce

The services.yaml allows you to "wire" the implementation you want to use to the respective interface:

# services.yaml

services:
    _defaults:
        autowire: true     

    # The current product repository implementation that gets wired to the domain interface by DI
    App\Domain\Product\ProductRepository:
        class: App\Infrastructure\Product\ProductDatabaseRepository

When having multiple implementations of an interface, you can for another environment wire another implementation

# services.yaml

# ... see above

when@test:
    services:
        App\Domain\Product\ProductRepository:
            class: App\Infrastructure\Product\ProductFromFixtureRepository

But, when ProductFromFixtureRepository would have autowired dependencies, where the default ProductDatabaseRepository has none, the compilation will fail (read: arguments will not get wired on test env).

Possible Solution

Enabling autowiring on the overridden service solves the error.

# services.yaml

when@test:
    services:
        App\Domain\Product\ProductRepository:
            class: App\Infrastructure\Product\ProductFromFixtureRepository
+           autowire: true

The cause might be that the _defaults might not get included for the when-parsed section?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions