Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Description

The prune() method skipped cache files with version numbers (e.g., jetpack-8.6.1.zip) because is_numeric('8.6.1') returns false. Files were never pruned despite reporting "Success: Cache pruned."

Solution

Extract version strings, not just numeric timestamps

  • Changed regex from checking is_numeric() to matching /^(\d+(?:\.\d+)*)/
  • Captures both 12345 and 8.6.1 formats

Use version_compare() for sorting

  • Replaces integer comparison $b['version'] <=> $a['version']
  • Handles dotted versions: version_compare('8.9.1', '8.6.1') returns 1
  • Falls back to mtime when versions unavailable or equal

Group before pruning

  • Original code deleted immediately upon finding older file
  • Now groups by base name first, then prunes each group

Coding standards compliance

  • Moved count() call outside loop condition per WP-CLI coding standards
  • Updated closures to use static function when no $this instance context is used

Updated for compatibility with main branch

  • Adapted prune logic to work with new get_cache_files() method using native PHP iterators
  • Removed dependency on Symfony Finder (merged from main)
  • Maintains all version-aware pruning functionality

Changes

  • php/WP_CLI/FileCache.php: Rewrite prune logic (~50 lines) and update to use native PHP iterators
  • features/cli-cache.feature: Add test for version-numbered cache files

Example

Before: All plugin versions kept (no numeric timestamp to compare)

jetpack-8.6.1.zip  ← kept
jetpack-8.7.1.zip  ← kept  
jetpack-8.9.1.zip  ← kept

After: Highest version kept via version_compare()

jetpack-8.6.1.zip  ← deleted
jetpack-8.7.1.zip  ← deleted
jetpack-8.9.1.zip  ← kept

Numeric timestamps still work (backward compatible):

file-a-12345.tmp  ← deleted
file-a-23456.tmp  ← kept
Original prompt

This section details on the original issue you should resolve

<issue_title>Command cli cache prune doesn't clear cache when file timemods are all the same</issue_title>
<issue_description>When I run cli cache prune it does nothing and echos "Success: Cache pruned."
When I run cli cache clear it does remove cache files and echos "Success: Cache cleared."

Adding --debug did not add anything interesting to the output, as far as I could tell.

My environment is a CentOS from root within a perl script, calling with backticks:
sudo -u foo $php /usr/local/bin/wp cli cache prune --path=/home/foo/public_html
where
$php = '/usr/local/bin/php -c ./php-wpcli.ini';

The cache is, for example:
ls -1 /home/foo/.wp-cli/cache/plugin

akismet-4.1.6.zip
autoptimize-2.7.3.zip
classic-editor-1.6.zip
disable-comments-1.11.0.zip
jetpack-8.6.1.zip
jetpack-8.7.1.zip
jetpack-8.9.1.zip
loginizer-1.4.4.zip
loginizer-1.4.8.zip
loginizer-1.6.0.zip
master-slider-3.6.2.zip
page-links-to-3.3.4.zip
wordpress-seo-14.3.zip
wordpress-seo-14.4.1.zip
wordpress-seo-14.5.zip
wordpress-seo-15.0.zip

The file timemods seem to be the date the script ran.
</issue_description>

<agent_instructions>Take a stab at this. Review #5660 and the discussion there for additional context.</agent_instructions>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix cli cache prune not clearing cache files Fix cli cache prune to handle version numbers in cache filenames Nov 14, 2025
Copilot AI requested a review from swissspidy November 14, 2025 16:31
@swissspidy swissspidy requested a review from Copilot November 14, 2025 16:33
@codecov
Copy link

codecov bot commented Nov 14, 2025

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/FileCache.php 94.59% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the cli cache prune command to properly handle cache files with semantic version numbers (e.g., jetpack-8.6.1.zip). Previously, the prune logic only worked with numeric timestamps and skipped all files with dotted version numbers because is_numeric('8.6.1') returns false.

Key changes:

  • Rewrote the version extraction logic to handle both numeric timestamps and semantic version strings using regex matching
  • Implemented version-aware sorting using PHP's version_compare() function with fallback to modification time
  • Changed from immediate deletion to a grouping-then-pruning approach for better maintainability

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
php/WP_CLI/FileCache.php Refactored prune() method to extract and compare semantic versions using version_compare(), with mtime fallback for non-versioned files
features/cli-cache.feature Added comprehensive test scenario verifying version-numbered plugin cache files are correctly pruned, keeping only the highest version

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI requested a review from swissspidy November 14, 2025 16:45
@swissspidy swissspidy marked this pull request as ready for review November 14, 2025 18:36
@swissspidy swissspidy requested a review from a team as a code owner November 14, 2025 18:36
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

@swissspidy swissspidy force-pushed the copilot/fix-cli-cache-prune-issue branch from f68906a to 05bce81 Compare December 10, 2025 10:29
@schlessera

This comment was marked as resolved.

This comment was marked as resolved.

@schlessera schlessera merged commit 51a062a into main Dec 12, 2025
54 checks passed
@schlessera schlessera deleted the copilot/fix-cli-cache-prune-issue branch December 12, 2025 16:20
@schlessera schlessera added this to the 3.0.0 milestone Dec 12, 2025
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.

Command cli cache prune doesn't clear cache when file timemods are all the same

3 participants