Skip to content

Commit 66464aa

Browse files
committed
Prefix the Phar's Composer dependency tree
WP-CLI registers its autoloader before WordPress boots, so for any class shipped both by the Phar and by the site, the Phar's copy wins and is imposed on the site. A site using monolog/monolog against psr/log v3 gets the Phar's psr/log 1.1.4 instead and fatals on the incompatible LoggerInterface signature. Moving wp-cli/package-command to require-dev fixed this for Composer-based installations, but the Phar is still built with dev dependencies, so it continues to ship composer/composer and its tree unprefixed: symfony/console v5.4.47, psr/log 1.1.4, react/promise, seld/*. Prefix that tree with php-scoper, with two constraints: * The `Composer\` namespace itself is left alone. Third-party Composer plugins are compiled against the real `Composer\Plugin\PluginInterface`, so prefixing it would break `wp package install` for any package shipping one. References from inside `Composer\` to the prefixed vendors are still rewritten, so Composer keeps using its own psr/log. * Nothing reachable from WP-CLI's public API is touched: php-cli-tools (`Utils\make_progress_bar()`), Requests (`Utils\http_request()`, plus RequestsLibrary deliberately sharing the library with Core), and every wp-cli/* package. php-scoper needs PHP 8.2 while WP-CLI still targets 7.2.24, so the toolchain lives in utils/scoper with its own composer.json. Two things this turned up that are easy to get wrong: * php-scoper rewrites source files but not Composer's generated autoload maps. A scoped tree with a stale autoloader still advertises `Psr\Log\` and the conflict survives with nothing to show for it, so the autoloader is regenerated and then asserted on. * Excluding a namespace does not stop php-scoper prefixing string literals naming classes inside it. Composer compares `$class` against 'Composer\Package\CompletePackage', which the prefix silently breaks; a patcher restores those. Refs wp-cli/wp-cli#5920 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RHtjyXkZh8X16sBgmidSQi
1 parent 8475849 commit 66464aa

6 files changed

Lines changed: 547 additions & 0 deletions

File tree

‎.github/workflows/deployment.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ jobs:
6060
name: manifest
6161
path: vendor/wp-cli/wp-cli/manifest.json
6262

63+
# Prefixes the composer/composer dependency tree so the Phar stops
64+
# imposing its own psr/log, Symfony and React versions on the site it
65+
# runs against. See https://github.com/wp-cli/wp-cli/issues/5920
66+
- name: Prefix bundled dependencies
67+
run: php utils/scope-dependencies.php
68+
6369
- name: Build the Phar file
6470
run: php -dphar.readonly=0 utils/make-phar.php wp-cli.phar --version=$CLI_VERSION
6571

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Feature: Bundled dependencies do not conflict with the site's own
2+
3+
# WP-CLI's autoloader is registered before WordPress boots, so for any class
4+
# shipped both by the Phar and by the site, the Phar's copy wins and is
5+
# imposed on the site. Prefixing the `composer/composer` dependency tree stops
6+
# the Phar from claiming those names at all.
7+
#
8+
# These scenarios run against the built Phar, which is the only artifact the
9+
# prefixing applies to; a Composer-based installation resolves its own
10+
# dependency versions and has no conflict to avoid.
11+
#
12+
# See https://github.com/wp-cli/wp-cli/issues/5920
13+
14+
@require-mysql
15+
Scenario: A site providing its own psr/log is not broken by the bundled one
16+
Given a WP installation
17+
# Stands in for a site that ships psr/log v3 through its own vendor
18+
# directory, as anything depending on monolog/monolog does. The typed
19+
# signatures are incompatible with the psr/log v1 that composer/composer
20+
# resolves to under the Phar's PHP 7.2 platform requirement, so whichever
21+
# copy of the interface loads first decides whether this fatals.
22+
And a wp-content/mu-plugins/site-logger.php file:
23+
"""
24+
<?php
25+
26+
spl_autoload_register(
27+
function ( $class ) {
28+
if ( 'Psr\\Log\\LoggerInterface' !== $class ) {
29+
return;
30+
}
31+
32+
eval(
33+
'namespace Psr\Log;
34+
interface LoggerInterface {
35+
public function emergency( \Stringable|string $message, array $context = [] ): void;
36+
}'
37+
);
38+
}
39+
);
40+
41+
final class Site_Logger implements \Psr\Log\LoggerInterface {
42+
public function emergency( \Stringable|string $message, array $context = [] ): void {
43+
}
44+
}
45+
"""
46+
47+
When I try `wp option get siteurl`
48+
Then STDERR should not contain:
49+
"""
50+
must be compatible with
51+
"""
52+
And STDERR should not contain:
53+
"""
54+
critical error
55+
"""
56+
And the return code should be 0
57+
58+
@require-mysql
59+
Scenario: A site providing its own Symfony Console is not broken by the bundled one
60+
Given a WP installation
61+
And a wp-content/mu-plugins/site-console.php file:
62+
"""
63+
<?php
64+
65+
spl_autoload_register(
66+
function ( $class ) {
67+
if ( 'Symfony\\Component\\Console\\Output\\OutputInterface' !== $class ) {
68+
return;
69+
}
70+
71+
eval(
72+
'namespace Symfony\Component\Console\Output;
73+
interface OutputInterface {
74+
public function writeln( \Stringable|string $messages, int $options = 0 ): void;
75+
}'
76+
);
77+
}
78+
);
79+
80+
final class Site_Output implements \Symfony\Component\Console\Output\OutputInterface {
81+
public function writeln( \Stringable|string $messages, int $options = 0 ): void {
82+
}
83+
}
84+
"""
85+
86+
When I try `wp option get siteurl`
87+
Then STDERR should not contain:
88+
"""
89+
must be compatible with
90+
"""
91+
And the return code should be 0
92+
93+
Scenario: Package management still works against the prefixed tree
94+
# Exercises the paths where Composer resolves classes dynamically from
95+
# strings, which prefixing of static `use` statements does not cover.
96+
Given an empty directory
97+
98+
When I run `wp package list --format=count`
99+
Then STDERR should be empty
100+
And the return code should be 0

0 commit comments

Comments
 (0)