Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions features/package-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
WP_CLI::add_command(
'local-v3-pkg',
static function () {
WP_CLI::success( 'local-v3-pkg ok' );
}
);
"""

When I run `wp package install ./local-v3-pkg`
Then STDOUT should contain:
"""
Success: Package installed.
"""

# Simulate a composer.json left behind by WP-CLI 2.12.0 before upgrading to 3.x.
When I run `wp eval '$p = "{PACKAGE_PATH}composer.json"; file_put_contents( $p, preg_replace( "/\"version\": \"[^\"]+\"/", "\"version\": \"2.12.0\"", file_get_contents( $p ), 1 ) );' --skip-wordpress`
Then the return code should be 0

When I run `wp package update`
Then STDOUT should contain:
"""
Success: Packages updated.
"""
And STDERR should be empty

When I run `cat {PACKAGE_PATH}composer.json`
Then STDOUT should not contain:
"""
"version": "2.12.0"
"""

Scenario: Updating packages warns and reverts composer.json when a package is incompatible after upgrade
Given an empty directory
And a local-v2-only-pkg/composer.json file:
"""
{
"name": "wp-cli-test/local-v2-only-pkg",
"type": "wp-cli-package",
"require": {
"wp-cli/wp-cli": "^3.0"
},
"autoload": {
"files": [ "command.php" ]
}
}
"""
And a local-v2-only-pkg/command.php file:
"""
<?php
"""

When I run `wp package install ./local-v2-only-pkg`
Then STDOUT should contain:
"""
Success: Package installed.
"""

# Simulate an older 2.x package constraint alongside a stale 2.12.0 root version.
When I run `wp eval 'file_put_contents( "local-v2-only-pkg/composer.json", str_replace( "^3.0", "^2.0", file_get_contents( "local-v2-only-pkg/composer.json" ) ) ); $p = "{PACKAGE_PATH}composer.json"; file_put_contents( $p, preg_replace( "/\"version\": \"[^\"]+\"/", "\"version\": \"2.12.0\"", file_get_contents( $p ), 1 ) );' --skip-wordpress`
Then the return code should be 0

When I try `wp package update`
Then the return code should be 1
And STDERR should contain:
"""
Warning: Some installed packages may not be compatible with WP-CLI
"""
And STDERR should contain:
"""
Reverted composer.json.
"""

When I run `cat {PACKAGE_PATH}composer.json`
Then STDOUT should contain:
"""
"version": "2.12.0"
"""

Scenario: Installing a package depending on a bundled command does not vendor duplicate bundled commands
Given an empty directory
And a local-dep-pkg/composer.json file:
"""
{
"name": "wp-cli-test/local-dep-pkg",
"type": "wp-cli-package",
"require": {
"wp-cli/wp-cli": "^3.0",
"wp-cli/core-command": "^2 || ^3"
},
"autoload": {
"files": [ "command.php" ]
}
}
"""
And a local-dep-pkg/command.php file:
"""
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
WP_CLI::add_command(
'local-dep-pkg',
static function () {
WP_CLI::success( 'local-dep-pkg ok' );
}
);
"""

When I run `wp package install ./local-dep-pkg`
Then STDOUT should contain:
"""
Success: Package installed.
"""
And STDERR should be empty

When I run `cat {PACKAGE_PATH}composer.json`
Then STDOUT should contain:
"""
"replace":
"""
And STDOUT should contain:
"""
"wp-cli/core-command":
"""
And the {PACKAGE_PATH}vendor/wp-cli/core-command directory should not exist

Scenario: Installing a package that is already bundled with WP-CLI emits a warning
Given an empty directory
And a local-bundled-dup/composer.json file:
"""
{
"name": "wp-cli/core-command",
"type": "wp-cli-package",
"autoload": {
"files": [ "command.php" ]
}
}
"""
And a local-bundled-dup/command.php file:
"""
<?php
"""

When I try `wp package install ./local-bundled-dup`
Then STDERR should contain:
"""
Warning: Package 'wp-cli/core-command' is already bundled with WP-CLI
"""
And STDOUT should contain:
"""
Success: Package installed.
"""
2 changes: 1 addition & 1 deletion features/package.feature
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Feature: Manage WP-CLI packages
Error: Failed to update packages (Composer return code 1).
"""
And STDOUT should match /Repository not found|Could not read from remote repository/
And STDERR should not contain:
And STDERR should contain:
"""
Reverted composer.json.
"""
Expand Down
105 changes: 98 additions & 7 deletions src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,17 @@ public function install( $args, $assoc_args ) {
$version = self::DEFAULT_DEV_BRANCH_CONSTRAINTS;
}

$bundled_packages = self::get_bundled_packages();
if ( isset( $bundled_packages[ $package_name ] ) ) {
WP_CLI::warning(
sprintf(
"Package '%s' is already bundled with WP-CLI (%s); the bundled version takes precedence over installed packages. To test a custom copy of a bundled command, use '--require' or 'wp-cli.yml' instead.",
$package_name,
$bundled_packages[ $package_name ]
)
);
}

WP_CLI::log( sprintf( 'Installing package %s (%s)', $package_name, $version ) );

// Read the WP-CLI packages composer.json and do some initial error checking.
Expand All @@ -374,9 +385,10 @@ public function install( $args, $assoc_args ) {

// Add the 'require' to composer.json
WP_CLI::log( sprintf( 'Updating %s to require the package...', Path::normalize( $json_path ) ) );
$json_manipulator = new JsonManipulator( $composer_backup );
$json_manipulator->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 );

Expand Down Expand Up @@ -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( '---' );
Expand All @@ -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'] ) ) {
Expand Down Expand Up @@ -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}." );
}
Expand Down Expand Up @@ -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<string, string> 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<int, string> $required_packages Package names present in `require`.
* @param array<string, string> $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.
*
Expand Down
Loading