Fix phar path resolution when renamed from wp-cli.phar#872
Draft
Fix phar path resolution when renamed from wp-cli.phar#872
Conversation
Replace hardcoded 'phar://wp-cli.phar' with dynamic Phar::running(false) to correctly handle renamed phar files. This ensures WP_CLI_ROOT always points to the actual phar stream path, fixing template path resolution when the phar is not named 'wp-cli.phar'. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Use Phar::running(true) for WP_CLI_ROOT (returns phar:// stream path) and Phar::running(false) for WP_CLI_PHAR_PATH (returns filesystem path). This ensures proper path resolution in phar_safe_path() function. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix wp-config.mustache file not found in phar
Fix phar path resolution when renamed from wp-cli.phar
Feb 15, 2026
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This comment was marked as resolved.
This comment was marked as resolved.
Keep WP_CLI_ROOT using the hardcoded alias 'phar://wp-cli.phar' to ensure template paths resolve correctly. Only change WP_CLI_PHAR_PATH to use Phar::running(false) which returns the filesystem path without phar:// protocol, preventing phar_safe_path() from creating malformed paths. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Created php/utils-override.php with fixed phar_safe_path() that replaces full paths with alias 'phar://wp-cli.phar/' instead of bare 'phar://'. Include this override in boot-phar.php after wp-cli.php is loaded. This is a temporary fix until wp-cli/wp-cli#6242 is merged. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Member
|
@copilot I don't think it works like that.. the test is still failing |
Removed non-working function override approach. Instead, added cweagans/composer-patches plugin and created a patch file to fix phar_safe_path() in wp-cli/wp-cli vendor code. The patch changes the replacement from bare 'phar://' to 'phar://wp-cli.phar/' alias, ensuring template paths resolve correctly. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix phar path resolution when phar file is not named "wp-cli.phar"
Problem
When running
wp config create --path=subfolder ...with a phar file named something other than "wp-cli.phar" (e.g., "wp"), the path resolution fails with a malformed path error. Additionally, template loading fails with "Couldn't find plugin-status.mustache" errors.Root Cause
Two issues were present:
WP_CLI_PHAR_PATHwas set usingPhar::running(true)which returns"phar:///path/to/file.phar", causingphar_safe_path()to search for the malformed string"phar://phar:///path/to/file.phar/"phar_safe_path()in wp-cli/wp-cli was replacing full paths with bare"phar://"instead of the alias"phar://wp-cli.phar/", causing template paths like"phar://vendor/..."which don't resolve without an archive nameSolution
WP_CLI_PHAR_PATHin boot-phar.php to usePhar::running(false)which returns the filesystem path withoutphar://protocol (e.g.,"/path/to/file.phar")cweagans/composer-patchesplugin to apply a patch to wp-cli/wp-cli'sphar_safe_path()functionpatches/wp-cli-phar-safe-path.patchthat fixes the replacement to use'phar://wp-cli.phar/'instead of bare'phar://'This ensures:
phar_safe_path()correctly converts"phar:///tmp/some.phar/vendor/..."to"phar://wp-cli.phar/vendor/..."Changes Made
Note
The patch will be removed once the fix in wp-cli/wp-cli#6242 is merged and the dependency is updated. The patch is automatically applied during
composer installand modifies the vendored wp-cli/wp-cli code at build time.Original prompt
This section details on the original issue you should resolve
<issue_title>wp-config.mustache is not a file in phar</issue_title>
<issue_description>## Bug Report
Describe the current, buggy behavior
When wp-cli.phar is renamed to wp and moved into a path location, --path option seems to have conflict with wp create command. This has been reported before (#31)
Describe how other contributors can replicate this bug
However, with just a cd to the subfolder, it works just fine:
Also, if I rename wp to wp-cli.phar, it works:
Describe what you would expect as the correct outcome
Let us know what environment you are running this on