Repository navigation
Forward environment variables to spawned processes in WP_CLI::launch() - #6166
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
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