Skip to content

DATABASE_URL gets corrupted when the container is compiled #36510

Closed
@mnapoli

Description

@mnapoli

Symfony version(s) affected: 4.4.* or 5.0.* -> I upgraded from 4.3 and this bug appeared.

Description
Given an environment variable like this:

DATABASE_URL=mysql://root:password@host.com:3306/mydb

The connection to the database fails. After investigation, I can see that the string gets transformed into the compiled container to something like this:

'mysql:/'.dirname(__DIR__,6).':password@host.com:3306/mydb'

I suspect this is somehow related to #12784 (which is 6 years old, so not directly related).

How to reproduce

What is interesting about this project

  • Have an environment variable like this:
DATABASE_URL=mysql://root:password@host.com:3306/mydb
  • Compile the container in a subdirectory of /root (or you could replace root with anything I guess)

The Kernel class has this addition that inlines the value of environment variables:

class Kernel extends BaseKernel
{
    ...
    protected function build(ContainerBuilder $container): void
    {
        if ('prod' === $this->environment) {
            $container->addCompilerPass(new ResolveEnvPlaceholdersPass(), PassConfig::TYPE_AFTER_REMOVING, -1000);
        }
    }
}

Possible Solution

Additional context

Doctrine config:

doctrine:
    dbal:
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci
        # Initially this was `env(resolve:DATABASE_URL)`, I tried without resolve: but with the same result
        url: '%env(DATABASE_URL)%'

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