10000 PHP ConfigBuilders of DEV-only bundles not working in PROD environment · Issue #48891 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

PHP ConfigBuilders of DEV-only bundles not working in PROD environment #48891

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

Closed
ThomasLandauer opened this issue Jan 5, 2023 · 13 comments
Closed

Comments

@ThomasLandauer
Copy link
Contributor

Symfony version(s) affected

6.2.4

Description

Since some bundles are not loaded in PROD (e.g. DebugBundle or WebProfilerBundle), the dependency injection for their PHP config files is not working (since their *Config class is not generated).

How to reproduce

config/packages/debug.php:

<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Config\DebugConfig;
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;

return static function (DebugConfig $debugConfig, ContainerConfigurator $containerConfigurator): void {
    if ('dev' === $containerConfigurator->env()) {
        $debugConfig->dumpDestination('tcp://' . env('VAR_DUMPER_SERVER')->string());
    }
};

config/bundles.php:

<?php declare(strict_types = 1);

return [
    // ...
    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
];

Then running APP_ENV=prod APP_DEBUG=0 php bin/console cache:clear results in this error:

Could not resolve argument "Symfony\Config\DebugConfig $debugConfig"
There is no extension able to load the configuration for "Symfony\Config\DebugConfig". Looked for namespace "debug", found "framework", "doctrine", ...

Possible Solution

No response

Additional Context

No response

@xabbuh
Copy link
Member
xabbuh commented Jan 5, 2023

Not sure I completely understand what you are trying to achieve. Why are you trying to configure a bundle that is not enabled?

@ThomasLandauer
Copy link
Contributor Author

Cause it's enabled in DEV environment ;-)
To me it looks like there is no PHP equivalent for YAML's when@dev:

@xabbuh
Copy link
Member
xabbuh commented Jan 5, 2023

Well, you can store the file in config/packages/dev.

@MatTheCat
Copy link
Contributor

@ThomasLandauer
Copy link
Contributor Author

@MatTheCat
Yeah, thanks, I wasn't aware of this!
Let's see if I can find a way to document this on https://symfony.com/doc/current/configuration.html

Is this the right syntax for multiple environments?:

return #[When(env: 'dev')] #[When(env: 'test')] static function (WebProfilerConfig $webProfilerConfig,...)

@xabbuh
IMO, The old system with the separate directories is confusing; the new way with conditions inside the file (e.g. if ('test' === $containerConfigurator->env()) { }) is so much nicer. What about recommending this as the preferred way on the docs page?

@derrabus
Copy link
Member
derrabus commented Jan 6, 2023

This is indeed a problem. If we want people to use generated config builders single config files for all environments, we need a solution for bundles that are missing in prod.

@derrabus derrabus added Status: Reviewed Help wanted Issues and PRs which are looking for volunteers to complete them. and removed Status: Needs Review Status: Waiting feedback labels Jan 6, 2023
@MatTheCat
Copy link
Contributor
MatTheCat commented Jan 6, 2023

@derrabus I believe #41182 is the solution, but it’s not (AFAICT) documented.

@ThomasLandauer I just tested and it seems you’re right about the multiple env syntax.

@derrabus derrabus added Status: Works for me and removed Status: Reviewed Help wanted Issues and PRs which are looking for volunteers to complete them. labels Jan 6, 2023
@derrabus
Copy link
Member
derrabus commented Jan 6, 2023

symfony/symfony-docs#15333 was the corresponding docs issue, but it was closed although the PR linked there did not document that attributes can be attached to closures.

Can you open a new issue (or PR if you want) on the docs repo?

@derrabus derrabus closed this as completed Jan 6, 2023
@ThomasLandauer
Copy link
Contributor Author

Well, the #[When] attribute in fact is documented - just not where everybody(?) was expecting it, but on https://symfony.com/doc/current/service_container.html#limiting-services-to-a-specific-symfony-environment ;-)

According to symfony/symfony-docs#16313 (comment) this was done on purpose, and is about to be reorganized. So I'm not doing anything in the docs.

@derrabus
Copy link
Member
derrabus commented Jan 6, 2023

That comment is > 1 year old. Don't if that should stop you from contributing to the docs.

cc @wouterj

@wouterj
Copy link
Member
wouterj commented Jan 6, 2023

The trick with #[When] in configuration files was unknown to me (you can see the example only shows using #[When] on a class instead).

I'm +1 on using #[When] in configuration examples (at the place were we also document the when@ syntax) and write one or two sentences about it (that it should be used instead of an if statement to avoid issues with unloaded bundles + that you can also use it on services, linking to the other section).

Are you ok to make those changes, @ThomasLandauer?

@MatTheCat
Copy link
Contributor

FYI #41182 also injected an $env var in config files for people using PHP < 8.

@ThomasLandauer
Copy link
Contributor Author

As a start, I went through https://symfony.com/doc/current/configuration.html and created some minor PR's:

For now, I stopped at this sentence:

Take the framework package, installed by default, as an example:

Question: The when@prod: syntax was introduced in v5.3:

The ability to defined different environments in a single file was introduced in Symfony 5.3.

But since when is it created by default? I.e. since which version is config/packages/prod/framework.yaml not created anymore upon installation?

My suggestion for the following paragraph would be: Show the when syntax as the way to do it. And only mention the old config/packages/prod/... system in some note box (like "In previous versions of Symfony there was another system for dealing with environments...")

Minor question: Can I delete versionadded:: 5.3 (since 5.4 is now the lowest supported)? Or how long are such version notes usually kept?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0