Skip to content

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

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

Fix phar path resolution when renamed from wp-cli.phar#872
Copilot wants to merge 4 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.

Root Cause

In the original code, WP_CLI_PHAR_PATH was set using Phar::running(true) which returns "phar:///path/to/file.phar". When phar_safe_path() attempted to normalize paths, it tried to replace "phar://phar:///path/to/file.phar/" with "phar://", creating a malformed search string with double phar:// prefixes.

Solution

Changed WP_CLI_PHAR_PATH to use Phar::running(false) which returns the filesystem path without the phar:// protocol (e.g., "/path/to/file.phar"). This allows phar_safe_path() to correctly search for "phar:///path/to/file.phar/" when normalizing paths.

Kept WP_CLI_ROOT using the hardcoded alias 'phar://wp-cli.phar' (set during phar creation) to ensure all template paths resolve correctly regardless of the actual phar filename.

Changes Made

  • Fix WP_CLI_PHAR_PATH to use Phar::running(false)
  • 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
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 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/boot-phar.php 0.00% 6 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>
Copy link
Author

Copilot AI commented Feb 15, 2026

@copilot this breaks existing tests:

...

Fixed in bf3c0cc. The issue was that I changed both WP_CLI_PHAR_PATH and WP_CLI_ROOT, but only WP_CLI_PHAR_PATH needed to change. Now WP_CLI_ROOT continues using the hardcoded alias 'phar://wp-cli.phar' so templates resolve correctly, while WP_CLI_PHAR_PATH uses Phar::running(false) to prevent the malformed double-prefix issue in phar_safe_path().

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:

  • api.wordpress.org
    • Triggering command: /usr/bin/curl curl -s REDACTED (dns block)
  • downloads.wordpress.org
    • Triggering command: /usr/bin/curl curl -sSfL REDACTED (dns block)

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants