Skip to content

Commit 28f1797

Browse files
swissspidyclaude
andcommitted
Make the cached Composer Phar fallback and cache publish robust
When getcomposer.org is unreachable and only a stale version list is cached, the version it names may not be cached any more. Fall back to the newest unexpired cached Phar in that case instead of trying to download. Publish a downloaded Phar under a temporary name and rename it into place, so a concurrent process never runs a partially copied file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pLgrx5zJn6U8rsynH
1 parent 82da1af commit 28f1797

2 files changed

Lines changed: 148 additions & 21 deletions

File tree

‎src/WP_CLI/Package/ComposerPhar.php‎

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,40 @@ public function locate( $quiet = false ) {
3636
return $this->path;
3737
}
3838

39-
$cache = WP_CLI::get_cache();
40-
$version = null;
41-
foreach ( $this->versions( $cache )['stable'] ?? [] as $release ) {
39+
$cache = $this->cache();
40+
$version = null;
41+
list( $versions, $fresh ) = $this->versions( $cache );
42+
foreach ( $versions['stable'] ?? [] as $release ) {
4243
if ( preg_match( '/^2\.\d+\.\d+$/D', $release['version'] ) && $release['min-php'] <= PHP_VERSION_ID ) {
4344
$version = $release['version'];
4445
break;
4546
}
4647
}
47-
if ( null === $version ) {
48+
49+
if ( null !== $version ) {
50+
$path = $cache->has( "composer/composer-{$version}.phar" );
51+
if ( $path ) {
52+
$this->path = $path;
53+
return $path;
54+
}
55+
}
56+
57+
if ( ! $fresh ) {
4858
// Offline or getcomposer.org unreachable: reuse the newest Composer already in the cache.
4959
foreach ( self::cached_versions( $cache ) as $cached ) {
5060
$path = $cache->has( "composer/composer-{$cached}.phar" ); // False once the cache's expiry has passed.
5161
if ( $path ) {
5262
$this->path = $path;
53-
WP_CLI::debug( "Using cached Composer {$cached}; version list unavailable.", 'packages' );
63+
WP_CLI::debug( "Using cached Composer {$cached}; version list unavailable or stale.", 'packages' );
5464
return $path;
5565
}
5666
}
57-
$version = 'latest-stable';
5867
}
5968

60-
$key = "composer/composer-{$version}.phar";
61-
$path = $cache->has( $key );
62-
if ( $path ) {
63-
$this->path = $path;
64-
return $path;
69+
if ( null === $version ) {
70+
$version = 'latest-stable';
6571
}
72+
$key = "composer/composer-{$version}.phar";
6673

6774
$temp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-composer-', true );
6875
if ( ! mkdir( $temp_dir, 0700 ) ) {
@@ -89,10 +96,7 @@ static function () use ( $temp, $temp_dir ) {
8996
}
9097
chmod( $temp, 0755 );
9198
if ( $cache->is_enabled() ) {
92-
if ( ! $cache->import( $key, $temp ) ) {
93-
throw new RuntimeException( 'Could not cache Composer.' );
94-
}
95-
chmod( $path, 0755 );
99+
$this->publish( $temp, $path );
96100
}
97101
$this->path = $path;
98102
return $path;
@@ -102,14 +106,15 @@ static function () use ( $temp, $temp_dir ) {
102106
* The release list from getcomposer.org. A copy lives in the cache for a day, so a run of package
103107
* commands costs one request; when the network is down, a stale copy still names the version to use.
104108
*
105-
* @return array Decoded list, empty when neither the network nor the cache has one.
109+
* @return array{0:array,1:bool} Decoded list (empty when neither the network nor the cache has one),
110+
* and whether it is current rather than a stale copy.
106111
*/
107112
private function versions( $cache ) {
108113
$key = 'composer/versions.json';
109114
if ( $cache->is_enabled() ) {
110115
$fresh = $cache->read( $key, self::VERSIONS_TTL );
111116
if ( false !== $fresh && is_array( json_decode( $fresh, true ) ) ) {
112-
return json_decode( $fresh, true );
117+
return [ json_decode( $fresh, true ), true ];
113118
}
114119
}
115120
try {
@@ -121,18 +126,43 @@ private function versions( $cache ) {
121126
if ( $cache->is_enabled() ) {
122127
$cache->write( $key, $body );
123128
}
124-
return $versions;
129+
return [ $versions, true ];
125130
} catch ( \Exception $e ) {
126131
WP_CLI::debug( $e->getMessage(), 'packages' );
127132
}
128133
if ( $cache->is_enabled() ) {
129134
$stale = $cache->read( $key );
130135
if ( false !== $stale && is_array( json_decode( $stale, true ) ) ) {
131136
WP_CLI::debug( 'Using the cached Composer version list; getcomposer.org is unreachable.', 'packages' );
132-
return json_decode( $stale, true );
137+
return [ json_decode( $stale, true ), false ];
138+
}
139+
}
140+
return [ [], false ];
141+
}
142+
143+
/**
144+
* Moves a verified download into the cache under a temporary name first, so that a concurrent
145+
* process never sees a partially copied Phar at the final path.
146+
*/
147+
private function publish( $source, $destination ) {
148+
$dir = dirname( $destination );
149+
if ( ! is_dir( $dir ) && ! @mkdir( $dir, 0777, true ) && ! is_dir( $dir ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
150+
throw new RuntimeException( 'Could not cache Composer.' );
151+
}
152+
$partial = $destination . '.' . uniqid( '', true ) . '.part';
153+
if ( ! copy( $source, $partial ) ) {
154+
throw new RuntimeException( 'Could not cache Composer.' );
155+
}
156+
chmod( $partial, 0755 );
157+
if ( ! @rename( $partial, $destination ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
158+
if ( file_exists( $partial ) ) {
159+
unlink( $partial );
160+
}
161+
// On Windows the rename fails while another process runs a copy it just published.
162+
if ( ! is_file( $destination ) ) {
163+
throw new RuntimeException( 'Could not cache Composer.' );
133164
}
134165
}
135-
return [];
136166
}
137167

138168
/**
@@ -154,7 +184,14 @@ private static function cached_versions( $cache ) {
154184
return array_reverse( $versions );
155185
}
156186

157-
private function request( $url, $options = [] ) {
187+
/**
188+
* @return \WP_CLI\FileCache
189+
*/
190+
protected function cache() {
191+
return WP_CLI::get_cache();
192+
}
193+
194+
protected function request( $url, $options = [] ) {
158195
$response = Utils\http_request(
159196
'GET',
160197
$url,

‎tests/phpunit/ComposerPharTest.php‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,94 @@ public function test_nonzero_json_exit() {
109109
$this->expectException( RuntimeException::class );
110110
( new ComposerPhar() )->run_json( [ 'outdated' ], $this->directory );
111111
}
112+
113+
public function test_stale_version_list_falls_back_to_cached_phar() {
114+
putenv( 'WP_CLI_COMPOSER_BINARY' );
115+
$cache_dir = $this->directory . '/cache';
116+
$requests = [];
117+
$composer = $this->composer_phar(
118+
$cache_dir,
119+
static function ( $url ) use ( &$requests ) {
120+
$requests[] = $url;
121+
throw new RuntimeException( "Could not download {$url}." );
122+
}
123+
);
124+
mkdir( $cache_dir . '/composer', 0777, true );
125+
file_put_contents( $cache_dir . '/composer/versions.json', self::versions_json() );
126+
touch( $cache_dir . '/composer/versions.json', time() - 2 * ComposerPhar::VERSIONS_TTL );
127+
file_put_contents( $cache_dir . '/composer/composer-2.8.0.phar', '' );
128+
try {
129+
$this->assertSame( $cache_dir . '/composer/composer-2.8.0.phar', $composer->locate( true ) );
130+
$this->assertSame( [ ComposerPhar::VERSIONS_URL ], $requests );
131+
} finally {
132+
$this->remove_cache( $cache_dir );
133+
}
134+
}
135+
136+
public function test_download_is_published_to_the_cache() {
137+
putenv( 'WP_CLI_COMPOSER_BINARY' );
138+
$cache_dir = $this->directory . '/cache';
139+
$phar = '<?php echo "composer";';
140+
$composer = $this->composer_phar(
141+
$cache_dir,
142+
static function ( $url, $options ) use ( $phar ) {
143+
if ( ComposerPhar::VERSIONS_URL === $url ) {
144+
return (object) [ 'body' => self::versions_json() ];
145+
}
146+
if ( isset( $options['filename'] ) ) {
147+
file_put_contents( $options['filename'], $phar );
148+
return (object) [ 'body' => '' ];
149+
}
150+
return (object) [ 'body' => hash( 'sha256', $phar ) . ' composer.phar' ];
151+
}
152+
);
153+
try {
154+
$path = $composer->locate( true );
155+
$this->assertSame( $cache_dir . '/composer/composer-2.9.0.phar', $path );
156+
$this->assertSame( $phar, file_get_contents( $path ) );
157+
$this->assertSame( [ $path, $cache_dir . '/composer/versions.json' ], glob( $cache_dir . '/composer/*' ) );
158+
} finally {
159+
$this->remove_cache( $cache_dir );
160+
}
161+
}
162+
163+
private static function versions_json() {
164+
return json_encode(
165+
[
166+
'stable' => [
167+
[
168+
'version' => '2.9.0',
169+
'min-php' => 70205,
170+
],
171+
],
172+
]
173+
);
174+
}
175+
176+
private function composer_phar( $cache_dir, callable $request ) {
177+
return new class( new WP_CLI\FileCache( $cache_dir, 15552000, 314572800 ), $request ) extends ComposerPhar {
178+
private $file_cache;
179+
private $request;
180+
181+
public function __construct( $file_cache, $request ) {
182+
parent::__construct();
183+
$this->file_cache = $file_cache;
184+
$this->request = $request;
185+
}
186+
187+
protected function cache() {
188+
return $this->file_cache;
189+
}
190+
191+
protected function request( $url, $options = [] ) {
192+
return ( $this->request )( $url, $options );
193+
}
194+
};
195+
}
196+
197+
private function remove_cache( $cache_dir ) {
198+
array_map( 'unlink', glob( $cache_dir . '/composer/*' ) ?: [] );
199+
rmdir( $cache_dir . '/composer' );
200+
rmdir( $cache_dir );
201+
}
112202
}

0 commit comments

Comments
 (0)