phpro / grumphp

A PHP code-quality tool
MIT License
4.14k stars 430 forks source link

Env variables don't get expanded in task definitions #1146

Open indykoning opened 2 months ago

indykoning commented 2 months ago
Q A
Version GrumPHP 2.6.0
Bug? yes
New feature? no
Question? no
Documentation? yes

Following the docs on using environment variables and parameters doesn't actually function as expected. The example seems to work, however setting the variable to false still has the config enabled.

i've debugged it by using it in a task and dumping the task definition in the code. Which results in:

GrumPHP\Task\Config\Metadata^ {#659
  -metadata: array:5 [
    "blocking" => true
    "task" => ""
    "label" => ""
    "priority" => 0
    "enabled" => "env_19eeff9b12d8f08c_bool_GRUM_COMPOSER_NORMALIZE_ENABLED_b9f45633a4808b98cb74867bcd84c333"
  ]
}

instead of the expected bool value, it is a string identifying the variable to be resolved. This feels similar to https://github.com/symfony/symfony/issues/45868

My configuration

# grumphp.yml
parameters:
    env(GRUM_COMPOSER_NORMALIZE_ENABLED): 'false'
    env(GRUM_COMPOSER_NORMALIZE_VERBOSE): 'false'

grumphp:
    tasks:
        composer_normalize:
            metadata:
                enabled: '%env(bool:GRUM_COMPOSER_NORMALIZE_ENABLED)%'
            indent_size: 4
            indent_style: "space"
            no_update_lock: false
            verbose: '%env(bool:GRUM_COMPOSER_NORMALIZE_VERBOSE)%'

Steps to reproduce:

# Generate empty folder
mkdir tmp
cd tmp
git init
echo "vendor" > .gitignore
pbpaste > grumphp.yml
composer require --dev phpro/grumphp

# Run GrumPHP:
git add -A && git commit -m"Test"
# or
./vendor/bin/grumphp run

Result:

Composer normalize is still excecuted instead of not excecuting at all
veewee commented 1 month ago

Thanks for reporting, I can confirm this is a bug but haven't found a solution yet. Feel free to dive into the specifics to figure out what is going wrong here.

What I've found so far is that the \GrumPHP\Configuration\Compiler\TaskCompilerPass is being executed before the env vars are being replaced inside \Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass when executing $container->compile(true); inside \GrumPHP\Configuration\ContainerBuilder::buildFromConfiguration().

This results in the task instances that are registered inside the container to have this env_19eeff9b12d8f08c_bool_GRUM_COMPOSER_NORMALIZE_ENABLED_b9f45633a4808b98cb74867bcd84c333 placeholder instead of the actual replaced value.