Skip to content

Database connection error caused by unset env vars in process #5362

@chrisRidgers

Description

@chrisRidgers

Not sure if this is intentional, or a bug, however.

Out setup is a docker container with environment variables defining (amongst other WordPress things) our database connection details.

We've run the cli command:

vuln plugin-status provided by the package at: git@github.com:10up/wp-vulnerability-scanner.git

Which was silently exiting at the following code:

                $list = WP_CLI::launch_self( $singular_type, array( 'list' ), array(
                        'format' => 'csv',
                        'fields' => 'name,version',
                ), true, true );

Which runs

WP_CLI_CONFIG_PATH='/home/app/.wp-cli/config.yml' '/usr/local/bin/php' /app/vendor/wp-cli/wp-cli/bin/../php/boot-fs.php plugin 'list'  --format='csv' --fields='name,version' --path='/app/web/wp'

And in doing so silently errors with status '1' due to an underlying error:

...
  ["stderr"]=>
  string(49) "Error: Error establishing a database connection.
"
...

The cause is that WP_CLI::launch_self goes on to invoke a new WP_CLI\Process object and doesn't pass in any environment configuration, so in our applications wp-config code the following:

define('DB_NAME', getenv('DB_NAME'));
define('DB_USER', getenv('DB_USER'));
define('DB_PASSWORD', getenv('DB_PASSWORD'));

Gives us empty database credentials and the mysqlconnection errors.

The code invoking the Process:

	public static function launch( $command, $exit_on_error = true, $return_detailed = false ) {
		Utils\check_proc_available( 'launch' );

		$proc    = Process::create( $command );
		$results = $proc->run();
                ...
	}

Which i can make work locally with the following change

	public static function launch( $command, $exit_on_error = true, $return_detailed = false ) {
		Utils\check_proc_available( 'launch' );

		$proc    = Process::create( $command, null, $_ENV );
		$results = $proc->run();
                ...
	}

Should the launch function be passing environment configuration through to the create constructor? or should the package in question not be using WP_CLI::launch_self like this?

Behaviour seems similar to #2365 however explicitly setting the env vars at the CLI doesn't fix the issue because they're never passed to Process::create

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions