Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Fix container:lint and #[Autoconfigure(binds: ...)] failing #45629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

LANGERGabrielle
Copy link
Contributor

@LANGERGabrielle LANGERGabrielle commented Mar 3, 2022

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

Description

Running container:lint when a service uses #[Autoconfigure(binds: ['$myDependency' => {...})] raises an error :

[ERROR] A binding is configured for an argument named "$myDependency" under         
         "_instanceof" in file  "/{...}/MyService.php", but no    
         corresponding argument has been found. It may be unused and should be  
         removed, or it may have a typo.

How to reproduce

Given a service

#[Autoconfigure(bind: ['$myDependency' => '@app.my_dependency'])]
class MyService
{
    public function __construct(MyDependency $myDependency)
    {
    }
}

@app.my_dependency is correctly injected into MyService. But when running container:lint with the container dumped (App_KernelDevDebugContainer.xml is present and up to date), it raises an error.

This only happens with #[Autoconfigure]. A similar configuration but using services.yaml works as expected :

__instanceof:
    App\MyService:
        binds:
            $myDependency: '@app.my_dependency'

Explanation of the issue

  1. RegisterAutoconfigureAttributesPass parses the #[Autoconfigure] and registers its binding.
  2. ResolveBindingsPass reads the binding and uses it to configure the service arguments.
  3. The container is dumped, with App_KernelDevDebugContainer.xml containing
    <service id="App\MyService" class="App\MyService" autowire="true" autoconfigure="true">
      <argument type="service" id="app.my_dependency"/>
    </service>
  4. ContainerLintCommand then creates a container from the dump.
  5. The container is compiled. Since Kernel::initializeBundles() is not called, only the base passes are used.
  6. RegisterAutoconfigureAttributesPass parses the #[Autoconfigure] and registers its binding, again.
  7. ResolveBindingsPass sees that the service already has an argument (from the xml dump from the first ResolveBindingsPass). This mean that the binding is not used this time, and it is never removed from $unusedBindings.
  8. The error is then thrown.

Explanation of the fix

This fix removes the passes that already processed the dumped container.
A more future proof fix would be 209516b, but it would require changes to DependencyInjection.

Both fixes have been tested on a mid size Symfony application.

@carsonbot carsonbot added this to the 5.4 milestone Mar 3, 2022
@LANGERGabrielle LANGERGabrielle force-pushed the fix-autoconfigure-attribute-container-lint branch 2 times, most recently from b734a71 to dc961e2 Compare March 3, 2022 21:51
@LANGERGabrielle LANGERGabrielle force-pushed the fix-autoconfigure-attribute-container-lint branch from dc961e2 to 2e72612 Compare March 3, 2022 22:05
@LANGERGabrielle LANGERGabrielle marked this pull request as draft March 3, 2022 22:14
@LANGERGabrielle LANGERGabrielle force-pushed the fix-autoconfigure-attribute-container-lint branch 2 times, most recently from 002daff to 209516b Compare March 3, 2022 22:31
@LANGERGabrielle LANGERGabrielle marked this pull request as ready for review March 3, 2022 22:42
@LANGERGabrielle LANGERGabrielle changed the title [FrameworkBundle] Fix container:lint and #[Autoconfigure(binds: ...)] failing [FrameworkBundle][DependencyInjection] Fix container:lint and #[Autoconfigure(binds: ...)] failing Mar 3, 2022
@nicolas-grekas
Copy link
Member

Thanks for the description of the issue.

As a rule of thumb, introducing a new method is a hint for a new feature.
I'd really prefer not adding any new method to fix the issue, because from an API pov, it makes things more complex.

Can you give the simpler solution a try instead please?

@LANGERGabrielle LANGERGabrielle force-pushed the fix-autoconfigure-attribute-container-lint branch from 209516b to 88017ff Compare March 4, 2022 17:34
@LANGERGabrielle LANGERGabrielle changed the title [FrameworkBundle][DependencyInjection] Fix container:lint and #[Autoconfigure(binds: ...)] failing [FrameworkBundle] Fix container:lint and #[Autoconfigure(binds: ...)] failing Mar 4, 2022
@LANGERGabrielle
Copy link
Contributor Author

LANGERGabrielle commented Mar 4, 2022

@nicolas-grekas done.
If you'd like, I can open a PR for the more future proof fix against 6.1.

@LANGERGabrielle LANGERGabrielle force-pushed the fix-autoconfigure-attribute-container-lint branch from 88017ff to 309998b Compare March 4, 2022 17:48
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Do you think this would be tested somehow?

Also, we might want to make ContainerLintCommand use this trait. I'd be happy to do it if it's ok for the scope of this PR.

Not for this PR as that wouldn't qualify as a bugfix but sure on 6.1.

If you'd like, I can open a PR for the more future proof fix against 6.1.

No need to unless we have a use case. I'd keep things simple for now I think.

@LANGERGabrielle
Copy link
Contributor Author

LANGERGabrielle commented Mar 4, 2022

Currently ContainerLintCommand is not tested in any way.
I could add a test, not sure which level would be best (I like full integration tests but that might not be the best).
I tested this manually quickly on a mid size application, and it succeeds.

@LANGERGabrielle
Copy link
Contributor Author

This seems related to #30930

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently ContainerLintCommand is not tested in any way.

OK, I'm 👍 as is then.
If you want to add tests in another PR, it'll be always welcome ;)

@nicolas-grekas
Copy link
Member

Thank you @LANGERGabrielle.

@nicolas-grekas nicolas-grekas merged commit 5f1e0c8 into symfony:5.4 Mar 8, 2022
@LANGERGabrielle LANGERGabrielle deleted the fix-autoconfigure-attribute-container-lint branch March 8, 2022 16:41
@fabpot fabpot mentioned this pull request Apr 2, 2022
@fabpot fabpot mentioned this pull request Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants