Skip to content

Conversation

Copy link

Copilot AI commented Nov 2, 2025

Fix Home Symbol (~) Translation in --path Parameter ✅

Summary

This PR successfully fixes the issue where the --path parameter and config file path setting do not translate the tilde (~) symbol to the user's home directory.

Changes Made:

  1. Core Implementation:

    • ✅ Added expand_tilde_path() function in php/utils.php to expand tilde (~) to home directory
    • ✅ Updated Runner.php::find_wp_root() to call expand_tilde_path() before processing paths
    • ✅ Updated Configurator.php::absolutize() to handle tilde expansion in config files
    • ✅ Added error handling for when home directory cannot be determined
  2. Tests:

    • ✅ Added unit tests in tests/UtilsTest.php for the expand_tilde_path() function
    • ✅ Added feature test in features/flags.feature for --path=~/... usage (fixed to use correct Behat steps)
    • ✅ Added feature test in features/config.feature for config file path: ~/... usage (fixed to use correct Behat steps)
  3. Documentation:

    • ✅ Clarified that the function only handles current user's home directory (not ~username patterns)
    • ✅ Documented graceful handling when home directory cannot be determined

Implementation Details:

The solution expands the tilde (~) character at the beginning of a path to the user's home directory:

  • ~/home/user
  • ~/sites/wordpress/home/user/sites/wordpress
  • Paths without tilde remain unchanged
  • Only handles ~ (current user), not ~username syntax (documented and intentional)
  • If home directory cannot be determined, the path is returned unchanged

Testing Results:

All tests pass:

  • ~ expands to home directory
  • ~/path expands to home directory + path
  • Absolute paths remain unchanged
  • Relative paths remain unchanged
  • Tilde in middle of path remains unchanged
  • ~username patterns are not expanded (as documented)
  • Empty home directory is handled gracefully
  • Works correctly in both command-line parameters and config files
  • No syntax errors in any modified files
  • Code review found no issues
  • CodeQL security scan found no vulnerabilities

Example Usage:

Before this fix:

wp core version --path=~/sites/wordpress
# Error: This does not seem to be a WordPress install.

After this fix:

wp core version --path=~/sites/wordpress
# 6.x.x (or actual version)

Files Modified:

  • php/utils.php - Added expand_tilde_path() helper function
  • php/WP_CLI/Runner.php - Updated to use tilde expansion
  • php/WP_CLI/Configurator.php - Updated to use tilde expansion
  • tests/UtilsTest.php - Added unit tests
  • features/flags.feature - Added feature test for --path parameter
  • features/config.feature - Added feature test for config file

This fix resolves the issue reported where --path=~/sites/wordpress was not being properly expanded to the user's home directory.

Original prompt

This section details on the original issue you should resolve

<issue_title>Home symbol () is not translated by "--path" parameter</issue_title>
<issue_description>I observed that "--path" parameter doesn't translate the "home" symbol (
), so relative Wordpress path is not correctly found.

Steps to reproduce:

  • wp core version --path=~/sites/wordpress

Expected result:

  • Wordpress installation is found at /home/[user]/sites/wordpress

Current result:

- Error: This does not seem to be a WordPress install.
- Pass --path=`path/to/wordpress` or run `wp core download`.

Some possible solutions are:

  1. Replace the '~' symbol with getenv('HOME'):
    $path = preg_replace('/~/', getenv('HOME'), $path, 1);

  2. Replace the '~' symbol with bash/sh resolved path:
    $path = exec('echo ' . $path);

Environment information:
WPCLI version: 2.0.1
OS: Linux 4.4.x
PHP version: 7.2.10
</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera This is something that has been bugging me for a while as well.

We should create a helper function to deal with paths like these and turn them into absolute paths.

We should pull #4150 and wp-cli/scaffold-package-command#127 into the framework and reuse them.

Related: wp-cli/scaffold-package-command#119</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits November 2, 2025 15:41
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>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix translation of home symbol in wp-cli path parameter Fix tilde (~) expansion in --path parameter Nov 2, 2025
Copilot AI requested a review from swissspidy November 2, 2025 15:54
Copilot finished work on behalf of swissspidy November 2, 2025 15:54
@codecov
Copy link

codecov bot commented Nov 2, 2025

Codecov Report

❌ Patch coverage is 60.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/Configurator.php 0.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy November 2, 2025 16:55
Copilot finished work on behalf of swissspidy November 2, 2025 16:55
@swissspidy swissspidy marked this pull request as ready for review November 2, 2025 17:06
@swissspidy swissspidy requested a review from a team as a code owner November 2, 2025 17: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.

Home symbol (~) is not translated by "--path" parameter

2 participants