diff --git a/features/package-update.feature b/features/package-update.feature index 4e98de3b..b2f72295 100644 --- a/features/package-update.feature +++ b/features/package-update.feature @@ -219,3 +219,177 @@ Feature: Update WP-CLI packages Packages updated. """ And STDERR should be empty + + Scenario: Updating packages syncs stale wp-cli/wp-cli root version in composer.json + Given an empty directory + And a local-v3-pkg/composer.json file: + """ + { + "name": "wp-cli-test/local-v3-pkg", + "type": "wp-cli-package", + "require": { + "wp-cli/wp-cli": "^3.0" + }, + "autoload": { + "files": [ "command.php" ] + } + } + """ + And a local-v3-pkg/command.php file: + """ + addMainKey( 'name', 'wp-cli/wp-cli' ); - $json_manipulator->addMainKey( 'version', self::get_wp_cli_version_composer() ); + $json_manipulator = new JsonManipulator( $composer_backup ); + $required_packages = array_keys( is_array( $composer_backup_decoded['require'] ?? null ) ? $composer_backup_decoded['require'] : [] ); + $required_packages[] = $package_name; + $this->sync_root_metadata( $json_manipulator, $required_packages, $bundled_packages ); $json_manipulator->addLink( 'require', $package_name, $version, false /*sortPackages*/, true /*caseInsensitive*/ ); $json_manipulator->addConfigSetting( 'secure-http', true ); @@ -693,10 +705,43 @@ public function update( $args, $assoc_args = [] ) { } $this->get_installed_packages(); // Validate composer.json before starting the child. - $packages_dir = dirname( $this->get_composer_json_path() ); - $installed_path = $packages_dir . '/vendor/composer/installed.json'; - $before = InstalledPackages::read( $installed_path ); - $updated_packages = []; + list( $json_path, $composer_backup, $composer_backup_decoded ) = $this->get_composer_json_path_backup_decoded(); + + $packages_dir = dirname( $json_path ); + $installed_path = $packages_dir . '/vendor/composer/installed.json'; + $before = InstalledPackages::read( $installed_path ); + $updated_packages = []; + $previous_version = isset( $composer_backup_decoded['version'] ) && is_string( $composer_backup_decoded['version'] ) ? $composer_backup_decoded['version'] : ''; + $current_version = self::get_wp_cli_version_composer(); + $bundled_packages = self::get_bundled_packages(); + $required_packages = array_keys( is_array( $composer_backup_decoded['require'] ?? null ) ? $composer_backup_decoded['require'] : [] ); + + foreach ( $required_packages as $required_package ) { + $lc_name = strtolower( $required_package ); + if ( isset( $bundled_packages[ $lc_name ] ) ) { + WP_CLI::warning( + sprintf( + "Package '%s' is already bundled with WP-CLI (%s); the bundled version takes precedence over installed packages. Consider running 'wp package uninstall %s'.", + $required_package, + $bundled_packages[ $lc_name ], + $required_package + ) + ); + } + } + + // Revert composer.json on shutdown if `$revert` is true (set to false on success). + $revert = false; + $json_manipulator = new JsonManipulator( $composer_backup ); + $this->sync_root_metadata( $json_manipulator, $required_packages, $bundled_packages ); + $updated_json = $json_manipulator->getContents(); + if ( $updated_json !== $composer_backup ) { + $revert = true; + $this->register_revert_shutdown_function( $json_path, $composer_backup, $revert ); + if ( strlen( $updated_json ) !== file_put_contents( $json_path, $updated_json ) ) { + WP_CLI::error( sprintf( 'Failed to write %s.', Path::normalize( $json_path ) ) ); + } + } WP_CLI::log( 'Using Composer to update packages...' ); WP_CLI::log( '---' ); @@ -711,6 +756,7 @@ public function update( $args, $assoc_args = [] ) { // Composer succeeded; what it changed is a report, and an unreadable report does not undo the update. $report = true; if ( 0 === $res ) { + $revert = false; try { foreach ( InstalledPackages::read( $installed_path ) as $name => $package ) { if ( isset( $before[ $name ] ) && ( $before[ $name ]['version'] !== $package['version'] || $before[ $name ]['source_reference'] !== $package['source_reference'] ) ) { @@ -749,6 +795,15 @@ public function update( $args, $assoc_args = [] ) { WP_CLI::success( 'Packages updated.' ); } } else { + if ( '' !== $previous_version && $previous_version !== $current_version ) { + WP_CLI::warning( + sprintf( + "Some installed packages may not be compatible with WP-CLI %s (packages composer.json was last updated for %s). Run 'wp package list --skip-update-check' and uninstall or reinstall incompatible packages.", + $current_version, + $previous_version + ) + ); + } $res_msg = false === $res ? '' : " (Composer return code {$res})"; // False: Composer never started. WP_CLI::error( "Failed to update packages{$res_msg}." ); } @@ -1149,6 +1204,42 @@ private static function get_wp_cli_version_composer() { return $version; } + /** + * Returns bundled WP-CLI command packages present in the running WP-CLI installation. + * + * @return array Map of lowercase package name to Composer version. + */ + private static function get_bundled_packages() { + $vendor_dir = defined( 'WP_CLI_VENDOR_DIR' ) ? WP_CLI_VENDOR_DIR : ''; + + return InstalledPackages::bundled( $vendor_dir ); + } + + /** + * Syncs the root package name, WP-CLI version, and bundled command `replace` map + * in `~/.wp-cli/packages/composer.json`. + * + * Packages already listed in `require` are excluded from `replace` so existing + * installations do not fail with a Composer self-conflict. + * + * @param JsonManipulator $json_manipulator Manipulator for packages composer.json. + * @param array $required_packages Package names present in `require`. + * @param array $bundled_packages Bundled WP-CLI command packages. + */ + private function sync_root_metadata( JsonManipulator $json_manipulator, array $required_packages, array $bundled_packages ) { + $json_manipulator->addMainKey( 'name', 'wp-cli/wp-cli' ); + $json_manipulator->addMainKey( 'version', self::get_wp_cli_version_composer() ); + + $required_lookup = array_flip( array_map( 'strtolower', $required_packages ) ); + $replace = array_diff_key( $bundled_packages, $required_lookup ); + + if ( ! empty( $replace ) ) { + $json_manipulator->addMainKey( 'replace', $replace ); + } else { + $json_manipulator->removeMainKey( 'replace' ); + } + } + /** * Creates a default WP-CLI packages composer.json. * diff --git a/src/WP_CLI/Package/InstalledPackages.php b/src/WP_CLI/Package/InstalledPackages.php index e17cea58..3e33d29c 100644 --- a/src/WP_CLI/Package/InstalledPackages.php +++ b/src/WP_CLI/Package/InstalledPackages.php @@ -33,6 +33,81 @@ public static function read( $path ) { return $packages; } + /** + * Reads bundled WP-CLI command packages from WP-CLI's own vendor/composer/installed.php. + * + * Reads the file directly instead of calling Composer\InstalledVersions so the + * result cannot be polluted by an already-loaded packages autoloader. + * + * @param string $vendor_dir Path to WP-CLI's vendor directory (WP_CLI_VENDOR_DIR). + * @return array Map of lowercase package name to Composer version. + */ + public static function bundled( $vendor_dir ) { + if ( '' === $vendor_dir ) { + return []; + } + + $installed_php = rtrim( $vendor_dir, '/\\' ) . '/composer/installed.php'; + if ( ! is_readable( $installed_php ) ) { + return []; + } + + $data = require $installed_php; + if ( ! is_array( $data ) || empty( $data['versions'] ) || ! is_array( $data['versions'] ) ) { + return []; + } + + $bundled = []; + foreach ( $data['versions'] as $name => $info ) { + if ( ! is_string( $name ) || ! is_array( $info ) ) { + continue; + } + if ( 'wp-cli-package' !== ( $info['type'] ?? '' ) ) { + continue; + } + if ( empty( $info['install_path'] ) || ! is_string( $info['install_path'] ) || ! is_dir( $info['install_path'] ) ) { + continue; + } + + $version = self::resolve_replace_version( $info ); + if ( null !== $version ) { + $bundled[ strtolower( $name ) ] = $version; + } + } + + ksort( $bundled ); + + return $bundled; + } + + /** + * Resolves a Composer-compatible version string for a bundled package's `replace` entry. + * + * @param array $info Entry from Composer's installed.php. + * @return string|null Version string suitable for `replace`, or null if unversioned. + */ + private static function resolve_replace_version( array $info ) { + $pretty = isset( $info['pretty_version'] ) && is_string( $info['pretty_version'] ) ? $info['pretty_version'] : ''; + if ( '' !== $pretty && preg_match( '/^v?(\d+\.\d+.*)$/i', $pretty, $matches ) ) { + return $matches[1]; + } + + if ( isset( $info['aliases'] ) && is_array( $info['aliases'] ) ) { + foreach ( $info['aliases'] as $alias ) { + if ( is_string( $alias ) && preg_match( '/^\d+\.(?:\d+\.)?x-dev$/', $alias ) ) { + return $alias; + } + } + } + + $version = isset( $info['version'] ) && is_string( $info['version'] ) ? $info['version'] : ''; + if ( '' !== $version && preg_match( '/^\d+\.\d+\.\d+/', $version ) ) { + return $version; + } + + return null; + } + public static function has_naming_error( $name ) { if ( ! preg_match( '{^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$}iD', $name ) ) { return 'The package name is invalid, it should have a vendor name, a forward slash, and a package name.'; diff --git a/tests/phpunit/ComposerJsonTest.php b/tests/phpunit/ComposerJsonTest.php index 7a386c1d..a1e3eab5 100644 --- a/tests/phpunit/ComposerJsonTest.php +++ b/tests/phpunit/ComposerJsonTest.php @@ -233,6 +233,43 @@ public function test_installed_packages_filters_dependencies_and_accepts_legacy_ } } + public function test_sync_root_metadata_updates_version_and_excludes_required_from_replace() { + $method = new \ReflectionMethod( 'Package_Command', 'sync_root_metadata' ); + if ( PHP_VERSION_ID < 80100 ) { + $method->setAccessible( true ); + } + + $manipulator = new \WP_CLI\JsonManipulator( + "{\n \"name\": \"wp-cli/wp-cli\",\n \"version\": \"2.12.0\",\n \"require\": {\n \"WP-CLI/Export-Command\": \"dev-main\"\n }\n}\n" + ); + + $method->invoke( + new Package_Command(), + $manipulator, + [ 'WP-CLI/Export-Command' ], + [ + 'wp-cli/entity-command' => '3.0.6', + 'wp-cli/export-command' => '3.0.3', + ] + ); + + $decoded = json_decode( $manipulator->getContents(), true ); + $this->assertSame( 'wp-cli/wp-cli', $decoded['name'] ); + $this->assertNotSame( '2.12.0', $decoded['version'] ); + $this->assertSame( [ 'wp-cli/entity-command' => '3.0.6' ], $decoded['replace'] ); + + // When no bundled packages remain after excluding required ones, `replace` is removed. + $method->invoke( + new Package_Command(), + $manipulator, + [ 'wp-cli/export-command' ], + [ 'wp-cli/export-command' => '3.0.3' ] + ); + + $decoded = json_decode( $manipulator->getContents(), true ); + $this->assertArrayNotHasKey( 'replace', $decoded ); + } + private function mac_safe_path( $path ) { $path = \WP_CLI\Path::normalize( $path ); $path = preg_replace( '#^/private/(var|tmp)/#i', '/$1/', $path ); diff --git a/tests/phpunit/PackageMetadataTest.php b/tests/phpunit/PackageMetadataTest.php index a336f4f0..38f0e05f 100644 --- a/tests/phpunit/PackageMetadataTest.php +++ b/tests/phpunit/PackageMetadataTest.php @@ -102,4 +102,85 @@ public function test_outdated_mapping() { $updates['installed'][0]['latest-status'] = 'up-to-date'; $this->assertSame( 'none', InstalledPackages::with_update( $dev, $updates )['update'] ); } + + public function test_bundled_packages_reads_installed_php() { + $tmp_dir = sys_get_temp_dir() . '/wp-cli-bundled-' . bin2hex( random_bytes( 4 ) ); + mkdir( $tmp_dir . '/composer', 0755, true ); + mkdir( $tmp_dir . '/wp-cli/entity-command', 0755, true ); + mkdir( $tmp_dir . '/wp-cli/package-command', 0755, true ); + mkdir( $tmp_dir . '/wp-cli/unaliased-command', 0755, true ); + mkdir( $tmp_dir . '/wp-cli/wp-cli', 0755, true ); + + $installed = [ + 'versions' => [ + 'wp-cli/entity-command' => [ + 'pretty_version' => 'v3.0.6', + 'version' => '3.0.6.0', + 'type' => 'wp-cli-package', + 'install_path' => $tmp_dir . '/wp-cli/entity-command', + 'aliases' => [], + ], + 'wp-cli/package-command' => [ + 'pretty_version' => 'dev-main', + 'version' => 'dev-main', + 'type' => 'wp-cli-package', + 'install_path' => $tmp_dir . '/wp-cli/package-command', + 'aliases' => [ '3.x-dev' ], + ], + 'wp-cli/unaliased-command' => [ + 'pretty_version' => 'dev-main', + 'version' => 'dev-main', + 'type' => 'wp-cli-package', + 'install_path' => $tmp_dir . '/wp-cli/unaliased-command', + 'aliases' => [], + ], + 'wp-cli/missing-command' => [ + 'pretty_version' => 'v3.0.0', + 'version' => '3.0.0.0', + 'type' => 'wp-cli-package', + 'install_path' => $tmp_dir . '/wp-cli/does-not-exist', + 'aliases' => [], + ], + 'wp-cli/wp-cli' => [ + 'pretty_version' => 'v3.0.0', + 'version' => '3.0.0.0', + 'type' => 'library', + 'install_path' => $tmp_dir . '/wp-cli/wp-cli', + 'aliases' => [], + ], + ], + ]; + + file_put_contents( + $tmp_dir . '/composer/installed.php', + 'assertSame( + [ + 'wp-cli/entity-command' => '3.0.6', + 'wp-cli/package-command' => '3.x-dev', + ], + InstalledPackages::bundled( $tmp_dir ) + ); + $this->assertSame( [], InstalledPackages::bundled( '' ) ); + $this->assertSame( [], InstalledPackages::bundled( $tmp_dir . '/missing' ) ); + } finally { + unlink( $tmp_dir . '/composer/installed.php' ); + foreach ( [ + $tmp_dir . '/composer', + $tmp_dir . '/wp-cli/entity-command', + $tmp_dir . '/wp-cli/package-command', + $tmp_dir . '/wp-cli/unaliased-command', + $tmp_dir . '/wp-cli/wp-cli', + $tmp_dir . '/wp-cli', + $tmp_dir, + ] as $dir ) { + if ( is_dir( $dir ) ) { + rmdir( $dir ); + } + } + } + } }