-
Notifications
You must be signed in to change notification settings - Fork 1k
Forward environment variables to spawned processes in WP_CLI::launch() #6166
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @manhphuc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a crucial enhancement to Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses a critical issue where environment variables were not being correctly forwarded to child processes spawned by WP_CLI::launch(). The change correctly captures the parent process's $_ENV variables and passes them to the child process. However, there's a logical flaw in the fallback mechanism when $_ENV is empty, which could still prevent environment variables from being inherited, thus not fully resolving the underlying problem.
php/class-wp-cli.php
Outdated
| // Fallback to previous behavior if no environment array is available. | ||
| $proc = Process::create( $command ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The else block currently falls back to Process::create( $command );. In Process::create, the $env parameter defaults to an empty array ([]). When an empty array is passed to proc_open (via Utils\proc_open_compat), the child process starts with an empty environment, meaning it will not inherit any environment variables from the parent process's actual shell environment.
This behavior would reintroduce the problem described in issue #5362 if $_ENV happens to be empty (e.g., due to variables_order settings in php.ini), as the child process would still not receive necessary environment variables like DB_NAME.
To ensure proper environment variable propagation (inheritance from the parent's actual environment) when $_ENV is empty, proc_open needs to be called with null for the $env argument. Therefore, the Process::create call in this else block should explicitly pass null for the environment.
This is a high-severity issue because it prevents the complete resolution of the problem this PR aims to fix, potentially leading to continued database connection errors in certain setups.
// If $_ENV is empty, explicitly pass null to inherit the parent's environment.
// This ensures environment variables are propagated even if $_ENV is not populated.
$proc = Process::create( $command, null, null );There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — thanks!
You're right that when $_ENV is empty, Process::create() needs to receive
null for the env argument so the child process inherits the parent
environment. Passing an empty array would wipe the environment entirely
and could reintroduce the issue from #5362.
I've updated the fallback branch to call:
$proc = Process::create( $command, null, null );
so that env vars are still propagated when $_ENV is not populated.
Thanks again for the detailed explanation!
|
Thanks a lot for your PR! I know you've already tested this manually but would it be possible to add some Behat tests for this as well? |
|
Thanks a lot for the review and the kind words! 🙌 Yes, adding Behat coverage makes sense here. My plan is to:
I’ll update the PR with these Behat tests and ping you once they’re pushed. |
294d238 to
784e373
Compare
|
Hi @swissspidy! 👋 Quick update — I’ve just pushed the Behat coverage we discussed. 😊 The new feature file is: It includes two scenarios that directly exercise the behaviour of
Both scenarios are passing locally with: Here is a screenshot of the passing test run:
Please let me know if you'd like any adjustments to the wording, structure, or coverage — happy to update anything! 🙌 |

Summary
This PR ensures that
WP_CLI::launch()forwards environment variables to the spawned process so that child processes can still access configuration defined viagetenv()/$_ENV(for exampleDB_NAME,DB_USER, andDB_PASSWORDinsidewp-config.php).Previously,
WP_CLI::launch()always calledProcess::create( $command )without passing an environment array. In environments where WordPress DB credentials (and other settings) are provided exclusively through environment variables (common in Docker setups), a child process spawned viaWP_CLI::launch()would not see those variables, leading to database connection errors similar to issue #5362.What this change does
$_ENV.$_ENVis not empty, calls:Process::create( $command, null, $env );so the spawned process inherits the parent environment.
$_ENVis empty, falls back to the previous behaviour:Process::create( $command );to avoid affecting setups that rely on default environment propagation.
Rationale and backward compatibility
WP_CLI::launch_self()call (and other usage oflaunch()) would fail with “Error establishing a database connection” because DB_* variables were not passed to child processes.$_ENVis non-empty.$_ENVis empty.Testing
Manual testing performed inside a Docker-based environment:
Installed WordPress using env-based DB configuration (
DB_NAME,DB_USER,DB_PASSWORD, etc.) andwp-config.phpreading them viagetenv().Confirmed core commands behave normally:
export HTTP_HOST=localhost /wp-cli-src/bin/wp core version --allow-root /wp-cli-src/bin/wp plugin list --allow-root /wp-cli-src/bin/wp db tables --allow-rootAll commands executed successfully.
Verified that child processes now propagate the environment correctly, resolving the issue outlined in Database connection error caused by unset env vars in process #5362.
Related issue