Skip to content

Comments

Fix phar path resolution when renamed from wp-cli.phar#872

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/fix-wp-config-mustache-file
Draft

Fix phar path resolution when renamed from wp-cli.phar#872
Copilot wants to merge 6 commits intomainfrom
copilot/fix-wp-config-mustache-file

Conversation

Copy link

Copilot AI commented Feb 15, 2026

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:

  1. WP_CLI_PHAR_PATH was set using Phar::running(true) which returns "phar:///path/to/file.phar", causing phar_safe_path() to search for the malformed string "phar://phar:///path/to/file.phar/"
  2. 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 name

Solution

  1. Changed WP_CLI_PHAR_PATH in boot-phar.php to use Phar::running(false) which returns the filesystem path without phar:// protocol (e.g., "/path/to/file.phar")
  2. Added cweagans/composer-patches plugin to apply a patch to wp-cli/wp-cli's phar_safe_path() function
  3. Created patches/wp-cli-phar-safe-path.patch that 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/..."
  • Template paths resolve correctly via the alias regardless of the actual phar filename
  • Both the original bug (malformed paths with --path flag) and template loading errors are fixed

Changes Made

  • Fix WP_CLI_PHAR_PATH to use Phar::running(false) in boot-phar.php
  • Add cweagans/composer-patches plugin to composer.json
  • Create patches/wp-cli-phar-safe-path.patch to fix phar_safe_path()
  • Update composer.lock to apply patch during install
  • Keep WP_CLI_ROOT using phar alias for template resolution
  • Pass linting and code style checks
  • Build and test phar with renamed file
  • Verify template paths resolve correctly

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 install and 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

  • a list of
  • steps to replicate
  • the error condition
$ curl -Lso ~/.local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ chmod +x ~/.local/bin/wp
$ wp core download --path=subfolder
Downloading WordPress 5.9 (en_US)...
md5 hash verified: b2ed7e1e6cf834564273f5b4581afd29
Success: WordPress downloaded.
$ wp config create --path=subfolder --dbname=mydb --dbuser=myuser --dbpass=mypass --skip-check --force
PHP Warning:  file_get_contents(phar://wp-cli.phar/vendor/wp-cli/wp-cli/templates/phar://.local/bin/wp/vendor/wp-cli/config-command/templates/wp-config.mustache): failed to open stream: phar error: "vendor/wp-cli/wp-cli/templates/phar:/.local/bin/wp/vendor/wp-cli/config-command/templates/wp-config.mustache" is not a file in phar "wp-cli.phar" in phar:///home/interface/.local/bin/wp/vendor/wp-cli/wp-cli/php/utils.php on line 577
Error: Could not create new 'wp-config.php' file.

However, with just a cd to the subfolder, it works just fine:

$ cd subfolder
$ wp config create --dbname=mydb --dbuser=myuser --dbpass=mypass --skip-check --force
Success: Generated 'wp-config.php' file.

Also, if I rename wp to wp-cli.phar, it works:

$ mv ~/.local/bin/wp ~/.local/bin/wp-cli.phar
$ wp-cli.phar config create --path=subfolder --dbname=mydb --dbuser=myuser --dbpass=mypass --skip-check --force
Success: Generated 'wp-config.php' file.

Describe what you would expect as the correct outcome

Success: Generated 'wp-config.php' file.

Let us know what environment you are running this on

OS:	Linux 3.10.0-1160.53.1.el7.x86_64 wp-cli/config-command#1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64
Shell:	/bin/bash
PHP binary:	/opt/rh/rh-php73/root/usr/bin/php
PHP version:	7.3.29
php.ini used:	/etc/opt/rh/rh-php73/php.ini
MySQL binary:	/opt/rh/rh-mariadb10...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes wp-cli/config-command#141

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/wp-cli/wp-cli-bundle/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 15, 2026 16:16
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
Copilot AI requested a review from swissspidy February 15, 2026 16:27
@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/boot-phar.php 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy

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.

@swissspidy

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.

@swissspidy
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants