Skip to content

Commit 2519e02

Browse files
committed
Prefix the Phar's third-party dependencies into third_party/
Replaces the earlier approach on this branch, which rewrote vendor/ in place from a separate build step. WP-CLI registers its autoloader before WordPress boots, so for any class shipped both by the Phar and by the site's own vendor/ the Phar's copy wins. A site depending on psr/log v3 therefore loads the Phar's psr/log v1 and fatals on the incompatible LoggerInterface signature. Composer's dependency tree (composer/composer and everything it pulls in, resolved from installed.json) is now rewritten under the WP_CLI\Vendor namespace into third_party/, which gets its own classmap autoloader that php/boot-phar.php registers. vendor/ is left untouched; utils/make-phar.php bundles third_party/ instead of the original packages and drops them from the main autoload maps, so the Phar stops advertising the unprefixed names. A Composer-based installation of the bundle is unaffected. The prefixing runs as Composer's post-install-cmd/post-update-cmd hook (`composer prefix-dependencies`) rather than as a separate build step. The php-scoper toolchain needs PHP 8.2+ while the bundle targets 7.2, so it lives in utils/scoper/composer.json with a committed lock file; on an older PHP the hook skips with a notice and a Phar built from that checkout bundles the unprefixed tree. CI that must test on older PHP installs the toolchain with a recent interpreter first and hands it over through WP_CLI_SCOPER_PHP, as the deployment test matrix now does. The build job refuses to build a Phar without third_party/. `Composer\` itself stays unprefixed so that third-party Composer plugins run by `wp package install` keep implementing the real interfaces, and the Symfony polyfills are copied verbatim since they only work under their global names. The hook verifies that the generated classmap advertises nothing else under an original name, and that no bundled WP-CLI package depends on a prefixed namespace. See wp-cli/wp-cli#5920 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BrV8jGVsfiypo6buRmZfug
1 parent 58dfeae commit 2519e02

15 files changed

Lines changed: 2696 additions & 545 deletions

‎.github/workflows/deployment.yml‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ 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
63+
# third_party/ is generated by the post-install-cmd hook (utils/prefix-dependencies.php).
64+
# Without it the Phar would bundle unprefixed dependencies and impose them on the site,
65+
# see https://github.com/wp-cli/wp-cli/issues/5920
66+
- name: Verify the third-party dependencies were prefixed
67+
run: |
68+
if [ ! -f third_party/vendor/autoload.php ]; then
69+
echo '::error::third_party/ is missing: `composer install` did not prefix the dependencies.'
70+
exit 1
71+
fi
6872
6973
- name: Build the Phar file
7074
run: php -dphar.readonly=0 utils/make-phar.php wp-cli.phar --version=$CLI_VERSION
@@ -124,6 +128,27 @@ jobs:
124128
sudo apt-get update
125129
sudo apt-get install ghostscript -y
126130
131+
# The `composer install` below prefixes the bundled dependencies through
132+
# php-scoper, which needs PHP 8.2+, while this matrix goes down to 7.2.
133+
# Install the toolchain with a recent PHP first and hand that interpreter
134+
# to utils/prefix-dependencies.php, then switch to the matrix version.
135+
- name: Set up PHP for the php-scoper toolchain
136+
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
137+
with:
138+
php-version: 'latest'
139+
coverage: none
140+
tools: composer
141+
env:
142+
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
143+
144+
- name: Install the php-scoper toolchain
145+
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v4
146+
with:
147+
working-directory: utils/scoper
148+
149+
- name: Remember the PHP interpreter for php-scoper
150+
run: echo "WP_CLI_SCOPER_PHP=$(php -r 'echo PHP_BINARY;')" >> "$GITHUB_ENV"
151+
127152
- name: Set up PHP environment
128153
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
129154
with:

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PHAR_BUILD_VERSION
55
/cache
66
/packages
77
/vendor
8+
/third_party
89
/*.phar
910
.*.swp
1011
*.log

‎composer.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@
7474
"minimum-stability": "dev",
7575
"prefer-stable": true,
7676
"scripts": {
77+
"post-install-cmd": "@prefix-dependencies",
78+
"post-update-cmd": "@prefix-dependencies",
79+
"prefix-dependencies": "php utils/prefix-dependencies.php",
7780
"behat": "run-behat-tests",
7881
"behat-rerun": "rerun-behat-tests",
79-
"lint": "run-linter-tests",
82+
"lint": "run-linter-tests --exclude third_party --exclude utils/scoper/vendor",
8083
"lint-gherkin": "run-gherkin-lint-tests",
8184
"phpcs": "run-phpcs-tests",
8285
"phpstan": "run-phpstan-tests",
@@ -93,6 +96,9 @@
9396
"@behat"
9497
]
9598
},
99+
"scripts-descriptions": {
100+
"prefix-dependencies": "Prefix the namespaces of the Phar's third-party dependencies into third_party/ (needs PHP 8.2+; runs on install and update)."
101+
},
96102
"support": {
97103
"issues": "https://github.com/wp-cli/wp-cli-bundle/issues",
98104
"source": "https://github.com/wp-cli/wp-cli-bundle",

‎features/dependency-isolation.feature‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ Feature: Bundled dependencies do not conflict with the site's own
22

33
# WP-CLI's autoloader is registered before WordPress boots, so for any class
44
# 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.
5+
# imposed on the site. The Phar therefore ships Composer's dependency tree
6+
# under the `WP_CLI\Vendor` prefix instead, see utils/prefix-dependencies.php.
77
#
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.
8+
# Prefixing happens on `composer install` and needs PHP 8.2+, which is why
9+
# these scenarios build their own Phar from the checkout and only run where
10+
# that tree exists. A Composer-based installation resolves its own dependency
11+
# versions and has no conflict to avoid.
1112
#
1213
# See https://github.com/wp-cli/wp-cli/issues/5920
1314

15+
@require-php-8.2
1416
Scenario: A site providing its own psr/log is not broken by the bundled one
15-
Given a WP installation
17+
Given an empty directory
18+
And a new Phar with the same version
19+
And a WP installation
1620
# Stands in for a site that ships psr/log v3 through its own vendor
1721
# directory, as anything depending on monolog/monolog does. The typed
1822
# signatures are incompatible with the psr/log v1 that composer/composer
@@ -43,19 +47,15 @@ Feature: Bundled dependencies do not conflict with the site's own
4347
}
4448
"""
4549

46-
When I try `wp option get siteurl`
47-
Then STDERR should not contain:
48-
"""
49-
must be compatible with
50-
"""
51-
And STDERR should not contain:
52-
"""
53-
critical error
54-
"""
55-
And the return code should be 0
50+
When I run `php {PHAR_PATH} option get siteurl`
51+
Then STDOUT should not be empty
52+
And STDERR should be empty
5653

54+
@require-php-8.2
5755
Scenario: A site providing its own Symfony Console is not broken by the bundled one
58-
Given a WP installation
56+
Given an empty directory
57+
And a new Phar with the same version
58+
And a WP installation
5959
And a wp-content/mu-plugins/site-console.php file:
6060
"""
6161
<?php
@@ -81,18 +81,18 @@ Feature: Bundled dependencies do not conflict with the site's own
8181
}
8282
"""
8383

84-
When I try `wp option get siteurl`
85-
Then STDERR should not contain:
86-
"""
87-
must be compatible with
88-
"""
89-
And the return code should be 0
84+
When I run `php {PHAR_PATH} option get siteurl`
85+
Then STDOUT should not be empty
86+
And STDERR should be empty
9087

88+
@require-php-8.2
9189
Scenario: Package management still works against the prefixed tree
92-
# Exercises the paths where Composer resolves classes dynamically from
93-
# strings, which prefixing of static `use` statements does not cover.
90+
# Composer itself keeps its namespace and resolves plenty of classes from
91+
# strings at runtime, which the prefixing of static `use` statements does
92+
# not cover.
9493
Given an empty directory
94+
And a new Phar with the same version
9595

96-
When I run `wp package list`
96+
When I run `php {PHAR_PATH} package list`
9797
Then STDERR should be empty
9898
And the return code should be 0

‎features/make-phar.feature‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,17 @@ Feature: Check `utils/make-phar.php` output
5555
Error: Couldn't find plugin-status.mustache
5656
"""
5757
And the return code should be 0
58+
59+
# Prefixing runs on `composer install` and needs PHP 8.2+, so the tree only
60+
# exists on such a checkout. See utils/prefix-dependencies.php.
61+
@require-php-8.2
62+
Scenario: Third-party dependencies are bundled under the WP_CLI\Vendor prefix
63+
Given an empty directory
64+
And a new Phar with the same version
65+
66+
When I run `php {PHAR_PATH} eval --skip-wordpress 'echo json_encode( [ interface_exists( "Psr\\Log\\LoggerInterface" ), class_exists( "Symfony\\Component\\Console\\Application" ), interface_exists( "WP_CLI\\Vendor\\Psr\\Log\\LoggerInterface" ), class_exists( "WP_CLI\\Vendor\\Symfony\\Component\\Console\\Application" ), class_exists( "Composer\\Semver\\Comparator" ) ] );'`
67+
Then STDOUT should be:
68+
"""
69+
[false,false,true,true,true]
70+
"""
71+
And STDERR should be empty

‎php/boot-phar.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
// Store the path to the Phar early on for `Utils\phar-safe-path()` function.
2323
define( 'WP_CLI_PHAR_PATH', Phar::running( true ) );
2424

25+
// The bundled third-party dependencies are namespace-prefixed and ship with
26+
// their own autoloader. See utils/prefix-dependencies.php.
27+
$wp_cli_third_party_autoload = dirname( __DIR__ ) . '/third_party/vendor/autoload.php';
28+
if ( file_exists( $wp_cli_third_party_autoload ) ) {
29+
require $wp_cli_third_party_autoload;
30+
}
31+
unset( $wp_cli_third_party_autoload );
32+
2533
if ( file_exists( 'phar://wp-cli.phar/php/wp-cli.php' ) ) {
2634
define( 'WP_CLI_ROOT', 'phar://wp-cli.phar' );
2735
include WP_CLI_ROOT . '/php/wp-cli.php';

‎phpcs.xml.dist‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<!-- What to scan. -->
1414
<file>.</file>
1515

16+
<!-- Prefixed copies of third-party packages, generated by utils/prefix-dependencies.php. -->
17+
<exclude-pattern>*/third_party/*</exclude-pattern>
18+
1619
<!-- Show progress. -->
1720
<arg value="p"/>
1821

@@ -46,19 +49,21 @@
4649
#############################################################################
4750
-->
4851

49-
<!-- These are two procedural stand-alone file that is never loaded in a
52+
<!-- These are procedural stand-alone files that are never loaded in a
5053
WordPress context, so these files do not have to comply with WP naming
5154
conventions. -->
5255
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
5356
<exclude-pattern>*/utils/get-package-require-from-composer\.php$</exclude-pattern>
5457
<exclude-pattern>*/utils/make-phar\.php$</exclude-pattern>
55-
<exclude-pattern>*/utils/scope-dependencies\.php$</exclude-pattern>
58+
<exclude-pattern>*/utils/prefix-dependencies\.php$</exclude-pattern>
59+
<exclude-pattern>*/utils/scoper/prefixed-packages\.php$</exclude-pattern>
5660
<exclude-pattern>*/utils/scoper/scoper\.inc\.php$</exclude-pattern>
5761
</rule>
5862
<rule ref="WordPress.WP.GlobalVariablesOverride">
5963
<exclude-pattern>*/utils/get-package-require-from-composer\.php$</exclude-pattern>
6064
<exclude-pattern>*/utils/make-phar\.php$</exclude-pattern>
61-
<exclude-pattern>*/utils/scope-dependencies\.php$</exclude-pattern>
65+
<exclude-pattern>*/utils/prefix-dependencies\.php$</exclude-pattern>
66+
<exclude-pattern>*/utils/scoper/prefixed-packages\.php$</exclude-pattern>
6267
<exclude-pattern>*/utils/scoper/scoper\.inc\.php$</exclude-pattern>
6368
</rule>
6469

‎phpstan.neon.dist‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ parameters:
55
- utils
66
excludePaths:
77
analyse:
8-
# Isolated toolchain with its own composer.json; its dependencies are not
9-
# installed in this project's vendor directory.
8+
# php-scoper configuration; its classes only exist in the isolated toolchain.
109
- utils/scoper/scoper.inc.php
10+
analyseAndScan:
11+
# The isolated php-scoper toolchain.
12+
- utils/scoper/vendor
1113
scanDirectories:
1214
- vendor/wp-cli/wp-cli
1315
scanFiles:

0 commit comments

Comments
 (0)