Skip to content

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

Draft
Copilot wants to merge 11 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 11 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/WP_CLI/Path.php: Modified Path::phar_safe() to strip the phar:// prefix from WP_CLI_PHAR_PATH before using it in path replacement
  • php/utils.php: Updated deprecated phar_safe_path() wrapper to delegate to Path::phar_safe()
  • tests/PathTest.php: Added unit test covering non-phar execution path and the deprecated wrapper function

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(
    self::PHAR_STREAM_PREFIX . rtrim( WP_CLI_PHAR_PATH, '/' ) . '/',
    self::PHAR_STREAM_PREFIX,
    $path
);

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

Note: This PR has been updated to work with the refactored WP_CLI\Path class introduced in the main branch. The fix is now applied to Path::phar_safe() instead of the original phar_safe_path() function, which is now a deprecated wrapper.

See wp-cli/config-command#141


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute 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/WP_CLI/Path.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
@swissspidy
Copy link
Copy Markdown
Member

@copilot merge in changes from main and resolve conflicts.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 8, 2026

@copilot merge in changes from main and resolve conflicts.

Done! Merged latest changes from main in commit b07e22e.

The merge conflict in php/utils.php was resolved by accepting the main branch's refactored version (which delegates to Path::phar_safe()), and I've applied the phar:// prefix stripping fix to the new Path::phar_safe() method in php/WP_CLI/Path.php.

All tests pass successfully.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /usr/bin/php php vendor/bin/phpunit --color=always --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php get onfig-not-file /usr/local/bin/git http.https://gitbash (dns block)
  • nosuchhost_asdf_asdf_asdf.com
    • Triggering command: /usr/bin/php php vendor/bin/phpunit --color=always --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php get onfig-not-file /usr/local/bin/git http.https://gitbash (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@swissspidy swissspidy force-pushed the copilot/fix-wp-config-not-file branch from b07e22e to 8465d57 Compare April 8, 2026 16:06
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