Skip to content

Add a warning to imports order #12318

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

Merged
merged 1 commit into from
Sep 26, 2019
Merged

Conversation

tristanbes
Copy link
Contributor

@tristanbes tristanbes commented Sep 13, 2019

After spending few hours narrowing this down, I've added a note in the documentation that potentially will save time for developper;

Here's what happened to me: I got a service that didn't worked, it's because the imports actually imports the file before, so the service definition got overriden with no warning by the Glob rule (App\:)

This behaviour is not mentionned on the documentation;

// services.yaml

imports:
    - { resource: actions_services.yaml }

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true     
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude:
          - '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
// actions_services.yaml
services:
    App\Actions\Company\CreateCompany:
        autowire: true
        autoconfigure: false
        parent: 'App\Handler\AbstractHandler'
        calls:
            - [setClient, ['@App\GraphQL\Client']]
            - [setEndpointUrlResolver, ['@App\Resolver\EndpointResolver']]
            - [setSuccessor, ['@App\Actions\User\CreateUser']]

tbessoussa_MacBook-Pro-dex-Tristan____workspace_importer-function

Note that the Calls section is missing

Working version:

// services.yaml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true     
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude:
          - '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Actions\Company\CreateCompany:
        autowire: true
        autoconfigure: false
        parent: 'App\Handler\AbstractHandler'
        calls:
            - [setClient, ['@App\GraphQL\Client']]
            - [setEndpointUrlResolver, ['@App\Resolver\EndpointResolver']]
            - [setSuccessor, ['@App\Actions\User\CreateUser']]

tbessoussa_MacBook-Pro-de-Tristan____workspace_importer-function

Calls is here, so the service is good to go.

@tristanbes tristanbes changed the title Add a note to imports order Add a warning to imports order Sep 13, 2019
Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

Thanks for the very detailed PR description! Looks like a perfect caution to me.

@javiereguiluz
Copy link
Member

@wouterj don't you think that this is a Symfony bug?

In the docs we always explain that "_defaults apply to services created in this file". If you import some config file ... you should accept the services as they are created in the other files ... and your "_defaults" shouldn't apply to them, right? Am I missing something?

@wouterj
Copy link
Member

wouterj commented Sep 26, 2019

@javiereguiluz that's exactly what's happening here. The _defaults is not the thing here, it's the App\ glob service creation. It overwrites the App\Actions\Company\CreateCompany service defined in the imported file. This can be considered a bug, maybe a more expected behavior is to not overwrite explicitly defined services @nicolas-grekas. (I did assume it was expected behavior, given that Fabien reviewed this PR).

@nicolas-grekas
Copy link
Member

Lastly defined services override previous one. That's the rule since 2.0, it applies here too.

@javiereguiluz
Copy link
Member

Now I see my mistake. I completely misunderstood. I think it makes sense because of the App\* config, which "destroys" anything previously configured, created or imported. It makes sense to me. Let's merge this. Thanks.

@nicolas-grekas
Copy link
Member

I'd suggest to remove the "FUD" part of the message and keep it strictly factual.

@javiereguiluz
Copy link
Member

javiereguiluz commented Sep 26, 2019

In fact, I think the real problem is that this is an old article. At that time, Symfony forced you to write a 3,000-line config file for services, so splitting into multiple files may make sense. But that's no longer the case. The services file is short even for complex apps (when using autowiring/autoconfiguration) so you don't really need imports most of the times.

@javiereguiluz javiereguiluz changed the base branch from master to 4.3 September 26, 2019 07:19
javiereguiluz added a commit that referenced this pull request Sep 26, 2019
This PR was submitted for the master branch but it was squashed and merged into the 4.3 branch instead (closes #12318).

Discussion
----------

Add a warning to imports order

After spending few hours narrowing this down, I've added a note in the documentation that potentially will save time for developper;

Here's what happened to me: I got a service that didn't worked, it's because the `imports` actually imports the file *before*, so the service definition got overriden with no warning by the Glob rule (`App\:`)

This behaviour is not mentionned on the documentation;

```yaml
// services.yaml

imports:
    - { resource: actions_services.yaml }

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude:
          - '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
```

```yaml
// actions_services.yaml
services:
    App\Actions\Company\CreateCompany:
        autowire: true
        autoconfigure: false
        parent: 'App\Handler\AbstractHandler'
        calls:
            - [setClient, ['@app\GraphQL\Client']]
            - [setEndpointUrlResolver, ['@app\Resolver\EndpointResolver']]
            - [setSuccessor, ['@app\Actions\User\CreateUser']]
```

<img width="549" alt="tbessoussa_MacBook-Pro-dex-Tristan____workspace_importer-function" src="https://user-images.githubusercontent.com/346010/64860934-eaf0cc80-d62e-11e9-94e9-a0da67d1b366.png">

**Note that the `Calls` section is missing**

Working version:

```yaml

// services.yaml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/*'
        exclude:
          - '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Actions\Company\CreateCompany:
        autowire: true
        autoconfigure: false
        parent: 'App\Handler\AbstractHandler'
        calls:
            - [setClient, ['@app\GraphQL\Client']]
            - [setEndpointUrlResolver, ['@app\Resolver\EndpointResolver']]
            - [setSuccessor, ['@app\Actions\User\CreateUser']]
```

<img width="712" alt="tbessoussa_MacBook-Pro-de-Tristan____workspace_importer-function" src="https://user-images.githubusercontent.com/346010/64860840-a9602180-d62e-11e9-9b3a-40e344f18e9f.png">

`Calls` is here, so the service is good to go.

Commits
-------

175b05a Add a warning to imports order
@javiereguiluz javiereguiluz merged commit 175b05a into symfony:4.3 Sep 26, 2019
@javiereguiluz
Copy link
Member

Thanks Tristan! This is now merged. I reworded it a bit to remove the caution and tried to explain why this is normal and expected. Thanks.

@tristanbes
Copy link
Contributor Author

Thanks;

Note that splitting services in external files are still required when you use parents because it cannot inherit from _defaults and force you to redeclare all the defaults on you X services with parent keyword @javiereguiluz

@tristanbes tristanbes deleted the patch-7 branch September 26, 2019 08:32
@javiereguiluz
Copy link
Member

If you ask me ... I'd remove parent and imports too ... and many other "features" 😃 But don't worry, we won't do that because some people need them sometimes. Cheers!

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.

6 participants