diff --git a/features/package-install.feature b/features/package-install.feature index f417a155..9abb2faf 100644 --- a/features/package-install.feature +++ b/features/package-install.feature @@ -1027,7 +1027,15 @@ Feature: Install WP-CLI packages "type": "wp-cli-package" } """ - And I run `php -r '$zip = new ZipArchive(); $zip->open( "remote-zip-test-command.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFromString( "remote-zip-test-command/composer.json", file_get_contents( "remote-zip-test-command/composer.json" ) ); $zip->close();'` + And a create-zip.php file: + """ + open( 'remote-zip-test-command.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); + $zip->addFromString( 'remote-zip-test-command/composer.json', file_get_contents( 'remote-zip-test-command/composer.json' ) ); + $zip->close(); + """ + And I run `php create-zip.php` And a PHP built-in web server When I run `wp package install http://localhost:8080/remote-zip-test-command.zip` @@ -1055,7 +1063,15 @@ Feature: Install WP-CLI packages "type": "wp-cli-package" } """ - And I run `php -r '$zip = new ZipArchive(); $zip->open( "remote-zip-flat-command.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFromString( "composer.json", file_get_contents( "composer-flat.json" ) ); $zip->close();'` + And a create-zip.php file: + """ + open( 'remote-zip-flat-command.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); + $zip->addFromString( 'composer.json', file_get_contents( 'composer-flat.json' ) ); + $zip->close(); + """ + And I run `php create-zip.php` And a PHP built-in web server When I run `wp package install http://localhost:8080/remote-zip-flat-command.zip` diff --git a/src/Package_Command.php b/src/Package_Command.php index d4642647..e01a2cb5 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -1305,6 +1305,14 @@ private function create_default_composer_json( $composer_path ) { $composer_path = Path::trailingslashit( $composer_dir ) . Path::basename( $composer_path ); + // Make sure the file exists before writing to it, as Composer's + // atomic file replacement otherwise triggers a (silenced) `stat()` + // warning for the missing file. + $created = false; + if ( ! file_exists( $composer_path ) ) { + $created = @touch( $composer_path ); // @codingStandardsIgnoreLine + } + $json_file = new JsonFile( $composer_path ); $repositories = (object) [ @@ -1328,6 +1336,10 @@ private function create_default_composer_json( $composer_path ) { try { $json_file->write( $options ); } catch ( Exception $e ) { + // Don't leave an empty composer.json behind, so that a later run can retry. + if ( $created ) { + @unlink( $composer_path ); // @codingStandardsIgnoreLine + } WP_CLI::error( $e->getMessage() ); }