Skip to content

Fix phar path resolution when WP_CLI_PHAR_PATH includes phar:// prefix#6242

Draft
Copilot wants to merge 9 commits intomainfrom
copilot/fix-wp-config-not-file
Draft

Fix phar path resolution when WP_CLI_PHAR_PATH includes phar:// prefix#6242
Copilot wants to merge 9 commits intomainfrom
copilot/fix-wp-config-not-file

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 14, 2026

The phar_safe_path() function fails when the phar is renamed (e.g., wp-cli.pharwp) because it incorrectly handles the WP_CLI_PHAR_PATH constant. This constant is always defined in wp-cli-bundle as Phar::running(true), which includes the phar:// prefix. The original code didn't account for this prefix, causing it to be prepended twice and creating malformed paths like:

phar://wp-cli.phar/vendor/wp-cli/wp-cli/templates/phar://.local/bin/wp/vendor/wp-cli/config-command/templates/wp-config.mustache

This causes wp config create --path=subfolder to fail.

Changes

  • php/utils.php: Modified phar_safe_path() to strip the phar:// prefix from WP_CLI_PHAR_PATH before using it in path replacement
  • tests/UtilsTest.php: Added unit test covering non-phar execution path

The function now correctly extracts internal phar paths regardless of the phar filename:

// Before: assumed WP_CLI_PHAR_PATH didn't include phar:// prefix
return str_replace(
    PHAR_STREAM_PREFIX . rtrim( WP_CLI_PHAR_PATH, '/' ) . '/',
    PHAR_STREAM_PREFIX,
    $path
);

// After: strips phar:// prefix if present
$phar_path = WP_CLI_PHAR_PATH;
if ( 0 === strpos( $phar_path, PHAR_STREAM_PREFIX ) ) {
    $phar_path = substr( $phar_path, strlen( PHAR_STREAM_PREFIX ) );
}
return str_replace(
    PHAR_STREAM_PREFIX . rtrim( $phar_path, '/' ) . '/',
    PHAR_STREAM_PREFIX,
    $path
);

See wp-cli/config-command#141

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

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 14, 2026

Codecov Report

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

Files with missing lines Patch % Lines
php/utils.php 0.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI changed the title [WIP] Fix wp-config.mustache is not a file in phar Fix phar path resolution when WP_CLI_PHAR_PATH is undefined Feb 14, 2026
Copilot AI requested a review from swissspidy February 14, 2026 22:36
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

Copilot AI changed the title Fix phar path resolution when WP_CLI_PHAR_PATH is undefined Fix phar path resolution when WP_CLI_PHAR_PATH includes phar:// prefix Feb 15, 2026
Copilot AI added a commit to wp-cli/wp-cli-bundle that referenced this pull request Feb 19, 2026
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>
Copilot AI and others added 7 commits February 26, 2026 10:53
…defined

This fixes the issue where wp config create --path=subfolder would fail when the phar file is not named wp-cli.phar. The function now properly uses Phar::running() to determine the phar file path instead of relying on the potentially undefined WP_CLI_PHAR_PATH constant.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
…refix

WP_CLI_PHAR_PATH is set to Phar::running(true) in wp-cli-bundle which includes the phar:// prefix. The function now strips this prefix before using it in the path replacement to avoid double-prefixing.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy force-pushed the copilot/fix-wp-config-not-file branch from 5b3d2f4 to 0ffeaea Compare February 26, 2026 09:59
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