Skip to content

Fix Windows ABSPATH handling: accept drive-letter + forward slash#6119

Merged
swissspidy merged 16 commits intowp-cli:mainfrom
adityaanurag0219:fix/6115-windows-path-absolute
Feb 7, 2026
Merged

Fix Windows ABSPATH handling: accept drive-letter + forward slash#6119
swissspidy merged 16 commits intowp-cli:mainfrom
adityaanurag0219:fix/6115-windows-path-absolute

Conversation

@adityaanurag0219
Copy link
Copy Markdown
Contributor

Fixes #6115

Problem

On Windows, WP-CLI defines ABSPATH as C:/path/..., but WordPress core
path_is_absolute() only recognizes C:\ as absolute.
This caused wp post delete <id> --force to fail cleaning up image files,
since path_join() mangled the paths and alternative image sizes weren’t deleted.

Steps to Reproduce

  1. Install WordPress on Windows, e.g. C:\wp\public.
  2. Run wp eval "echo ABSPATH;".
    • In browser: C:\wp\public/
    • In WP-CLI: C:/wp/public/
  3. Import an image:
    wp media import image.jpg 
  • generates image.jpg and image-150x150.jpg.
  1. Delete the image:
wp post delete <image-id> --force`
  1. Observe that the resized image (image-150x150.jpg) remains undeleted.

###Environment

  • OS: Windows 11

  • Shell: PowerShell / CMD

  • PHP version: 8.4.x

  • WP-CLI version: 2.12.0

Solution

  • Updated Runner::set_wp_root() to normalize paths beginning with drive-letter + forward slash (C:/) into drive-letter + backslash (C:\).
  • Added a unit test (PathTest) verifying behavior for:
    • C:\ accepted
    • C:/ now accepted
    • Unix absolute (/var/www/...) not matched
    • Relative (./...) not matched

###Tests

Added new unit test tests/PathTest.php covering:

  • ✅ C:\ accepted
  • ✅ C:/ accepted
  • 🚫 Unix absolute (/var/www/...) not matched
  • 🚫 Relative (./...) not matched

Verification

  • Confirmed regex behavior locally with a test script.
  • Added tests/PathTest.php to ensure cross-platform reliability.
  • Full PHPUnit suite will be validated by GitHub Actions CI.

This fix improves cross-platform compatibility for Windows users when deleting media files with WP-CLI.

@adityaanurag0219 adityaanurag0219 requested a review from a team as a code owner September 29, 2025 15:19
@codecov

This comment was marked as resolved.

@swissspidy swissspidy changed the title Fix Windows ABSPATH handling: accept drive-letter + forward slash (fixes #6115) Fix Windows ABSPATH handling: accept drive-letter + forward slash Sep 29, 2025
@swissspidy
Copy link
Copy Markdown
Member

Thanks for your PR. Please undo unrelated changes and follow the existing coding standards. You can do so via PHPCS and PHPCBF.

The unit test should actually use PHPUnit.

@adityaanurag0219
Copy link
Copy Markdown
Contributor Author

Thanks for the review, @swissspidy 🙌
I’ll revert the unrelated changes and ensure coding standards are followed via PHPCS/PHPCBF.
I’ll also migrate the new test to proper PHPUnit style (instead of the inline script I had earlier).
Once updated, I’ll push the changes and re-run the checks.

@adityaanurag0219
Copy link
Copy Markdown
Contributor Author

Thanks again for the review 🙌

I’ve updated the PR as requested:

Reverted unrelated changes.

Ensured coding standards via PHPCS/PHPCBF (my modified files are clean).

Migrated the new test to proper PHPUnit style with data providers and assertions. The test passes in isolation (OK, 1 test, 5 assertions). ✅

At this point, my changes are correct and self-contained.

The failing CI checks seem to be unrelated to this PR:

Unit and Functional tests fail early because existing tests (e.g. tests/ArgValidationTest.php) still reference WP_CLI\Tests\TestCase, which no longer exists in the repo.

PHPCS job still flags issues in unrelated files (e.g. composer-setup.php), not touched by this PR.

Once the upstream suite issues are resolved, this PR’s checks should pass as expected.

@swissspidy
Copy link
Copy Markdown
Member

Thanks! There are still a ton of unrelated changes in php/WP_CLI/Runner.php. Not sure why PHPCS doesn't flag them. Might be easiest to start over. Otherwise reviewing is hard.

@swissspidy
Copy link
Copy Markdown
Member

Unit and Functional tests fail early because existing tests (e.g. tests/ArgValidationTest.php) still reference WP_CLI\Tests\TestCase, which no longer exists in the repo.

It does exist. It's coming from the wp-cli-tests package. You should use the same TestCase class in your tests as well!

@adityaanurag0219 adityaanurag0219 force-pushed the fix/6115-windows-path-absolute branch from 7244f18 to 2efdc18 Compare October 1, 2025 14:57
@adityaanurag0219
Copy link
Copy Markdown
Contributor Author

“All unrelated changes reverted and final cleanup done ✅.
Tests pass locally; awaiting review confirmation. Thank you for your guidance!”

This comment was marked as resolved.

swissspidy and others added 7 commits January 21, 2026 20:11
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy added this to the 3.0.0 milestone Feb 7, 2026
@swissspidy swissspidy merged commit a8fba6e into wp-cli:main Feb 7, 2026
53 checks passed
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.

On Windows path_is_absolute() returns false for absolute paths starting with ABSPATH due to default setting

3 participants