Skip to content

Commit 035668f

Browse files
authored
Merge pull request #417 from wp-cli/fix/php-tuning-opcache
Keep the shared opcode cache from changing what the code does
2 parents e4e99fc + 87d1bf1 commit 035668f

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎features/testing.feature‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,14 @@ Feature: Test that WP-CLI loads.
208208
C:\Windows
209209
"""
210210

211+
212+
@skip-windows
213+
Scenario: Run WP-CLI with a disabled function after it ran without
214+
Given an empty directory
215+
When I run `wp cli version`
216+
And I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=ini_set} cli info`
217+
Then the return code should be 0
218+
And STDOUT should contain:
219+
"""
220+
PHP binary
221+
"""

‎src/Context/FeatureContext.php‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,12 @@ private static function get_process_env_variables(): array {
508508
$env['WP_CLI_REQUIRE'] = $updated;
509509
}
510510

511-
$env = array_merge( $env, self::get_php_tuning_env_variables() );
511+
$env = array_filter(
512+
array_merge( $env, self::get_php_tuning_env_variables() ),
513+
static function ( $value ) {
514+
return null !== $value;
515+
}
516+
);
512517

513518
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
514519
if ( false !== $config_path ) {
@@ -569,7 +574,7 @@ private static function get_process_env_variables(): array {
569574
*
570575
* Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
571576
*
572-
* @return array<string, string>
577+
* @return array<string, string|null> Null for variables to leave out.
573578
*/
574579
private static function get_php_tuning_env_variables(): array {
575580
if ( 'off' === getenv( 'WP_CLI_TEST_PHP_TUNING' ) || self::running_with_code_coverage() ) {
@@ -587,9 +592,13 @@ private static function get_php_tuning_env_variables(): array {
587592
return $env;
588593
}
589594

595+
// The directories are named after the settings, so that a cache written with other settings,
596+
// such as by an older version of this package, is never used, nor its configuration file.
597+
$settings = [ 'opcache.enable_cli=1', 'opcache.file_cache_only=1', 'opcache.optimization_level=0' ];
598+
$key = substr( md5( implode( "\n", $settings ) ), 0, 8 );
590599
$temp_dir = rtrim( sys_get_temp_dir(), '/' );
591-
$config_dir = "{$temp_dir}/wp-cli-test-php-config";
592-
$cache_dir = "{$temp_dir}/wp-cli-test-opcache";
600+
$config_dir = "{$temp_dir}/wp-cli-test-php-config-{$key}";
601+
$cache_dir = "{$temp_dir}/wp-cli-test-opcache-{$key}";
593602

594603
foreach ( [ $config_dir, $cache_dir ] as $dir ) {
595604
if ( ! is_dir( $dir ) && ! @mkdir( $dir, 0777, true ) && ! is_dir( $dir ) ) {
@@ -600,16 +609,32 @@ private static function get_php_tuning_env_variables(): array {
600609
$excluded = array_unique( array_filter( [ $temp_dir, realpath( $temp_dir ) ] ) );
601610
file_put_contents( "{$config_dir}/opcache-blacklist.txt", implode( "/\n", $excluded ) . "/\n" );
602611

603-
$ini = [
604-
'opcache.enable_cli=1',
605-
"opcache.file_cache={$cache_dir}",
606-
'opcache.file_cache_only=1',
607-
"opcache.blacklist_filename={$config_dir}/opcache-blacklist.txt",
608-
];
612+
// The optimizer bakes in what it finds at compile time, such as whether a function exists,
613+
// which differs between processes with other settings, such as disabled functions.
614+
$ini = array_merge(
615+
$settings,
616+
[
617+
"opcache.file_cache={$cache_dir}",
618+
"opcache.blacklist_filename={$config_dir}/opcache-blacklist.txt",
619+
]
620+
);
609621
file_put_contents( "{$config_dir}/wp-cli-tests.ini", implode( "\n", $ini ) . "\n" );
610622

611623
// Add to the directories PHP scans for configuration files rather than replacing them.
612624
$scan_dirs = getenv( 'PHP_INI_SCAN_DIR' );
625+
626+
// Behat and Composer restart without Xdebug through composer/xdebug-handler, which clears
627+
// the directories, and keeps the original ones in its settings, with "*" if there were none.
628+
// The configuration file it points PHPRC to holds the scanned files already, so restore that too.
629+
$restart_settings = getenv( 'XDEBUG_HANDLER_SETTINGS' );
630+
if ( '' === $scan_dirs && is_string( $restart_settings ) ) {
631+
$restart = explode( '|', $restart_settings );
632+
if ( isset( $restart[2], $restart[3] ) ) {
633+
$scan_dirs = '*' === $restart[2] ? false : $restart[2];
634+
$env['PHPRC'] = '*' === $restart[3] ? null : $restart[3];
635+
}
636+
}
637+
613638
if ( false === $scan_dirs ) {
614639
$scan_dirs = PHP_CONFIG_FILE_SCAN_DIR;
615640
}

‎utils/docker/Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ RUN if dpkg --compare-versions "$(dpkg-query -W -f '${Version}' libsqlite3-0)" l
4040
&& [ "$(openssl dgst -sha3-256 -r sqlite.tar.gz | cut -d' ' -f1)" = "${SQLITE_SHA3_256}" ] \
4141
&& tar xzf sqlite.tar.gz \
4242
&& cd "sqlite-autoconf-${SQLITE_VERSION}" \
43-
&& CFLAGS="-O2 -DSQLITE_ALLOW_ROWID_IN_VIEW -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS3_TOKENIZER -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_MAX_SCHEMA_RETRY=25 -DSQLITE_SECURE_DELETE -DSQLITE_SOUNDEX -DSQLITE_USE_URI=1" \
43+
&& CFLAGS="-O2 -DSQLITE_ALLOW_ROWID_IN_VIEW -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS3_TOKENIZER -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_MAX_SCHEMA_RETRY=25 -DSQLITE_SECURE_DELETE -DSQLITE_SOUNDEX -DSQLITE_USE_URI=1" \
4444
./configure --prefix=/usr/local --disable-static > /dev/null \
4545
&& make -j"$(nproc)" > /dev/null \
4646
&& make install > /dev/null \

0 commit comments

Comments
 (0)