Skip to content

fix(Cache): delete hidden cache entries in the File handler - #10785

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/cache-file-hidden-entries
Open

gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/cache-file-hidden-entries

Conversation

@gr8man

@gr8man gr8man commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description
Cache entries whose keys start with a dot were skipped by clean() / cache:clear, getCacheInfo() and deleteMatching(), so they could never be reported or removed through the framework API. This change makes those operations hidden-aware while keeping .htaccess protected from deleteMatching() and the htdocs protection files intact in clean(); regression tests and the user guide are updated. Follow-up to #10766.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added needs template Opened issues not following the bug form template bug Verified issues on the current code behavior or pull requests that will fix them labels Nov 18, 2026
@carson-codeigniter4

This comment has been minimized.

@carson-codeigniter4 carson-codeigniter4 Bot removed the needs template Opened issues not following the bug form template label Nov 18, 2026

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer to keep the current behavior for the reasons I mentioned in my review: #10766 (review)

Applications can already use an explicit pattern like deleteMatching('.app_*') to remove files with that prefix. In 4.7.6, we deliberately preserved the glob() behavior where deleteMatching('*') skips files whose names start with a dot.

This PR would also make cache:clear delete unrelated files such as .gitkeep and .gitignore. Adding exceptions for those two names would still leave other hidden files in the cache directory subject to deletion.

@gr8man

gr8man commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

One concern with keeping the current behavior: without this change, cache entries whose keys start with a dot are never reported by getCacheInfo() and never removed by clean()/cache:clear or deleteMatching('*'). They can only be removed manually from the filesystem, making them effectively permanent cache entries — that is the bug this PR fixes.

Regarding .gitkeep/.gitignore: those are repository scaffolding files, not cache entries. Instead of reverting the whole change, could we protect them explicitly — i.e., add them to the protected list in delete_files() (alongside .htaccess, index.* and web.config) — so repository files stay safe while hidden cache keys can still be cleared? I'm happy to adjust the PR in that direction if you prefer.

@michalsn

Copy link
Copy Markdown
Member

I see this as an edge case. As I understand it, it surfaced during AI-assisted review, and I haven't seen a report of it affecting a real application. The limitation is documented, and delete('.app_item') or deleteMatching('.app_*') can already remove these entries through the cache API.

I don't think that justifies changing a shared filesystem helper or broadening what cache:clear and deleteMatching('*') delete. Exceptions for .gitkeep and .gitignore would still leave other hidden files exposed.

That said I'm happy to hear what other maintainers think - I won't be stubborn about it.

@paulbalandan

Copy link
Copy Markdown
Member

Just reading the PR title and body makes me think "this should be an opt-in 4.8 feature then". In practice, or at least me personally, I don't see cache items starting with dots. Though it's possible, it's quite rare. My understanding is dot files are supposed to be hidden, but cache items don't need to.

@neznaika0

Copy link
Copy Markdown
Contributor
  1. The user can save the cache under any name.

  2. I think it’s worth showing all files when searching in the directory — this will be the expected result. It’s unnatural when the function says the folder is empty, but the rmdir() deletion throws an error.

  3. Deleting everything except the reserved .htaccess, index.* looks like the right solution, and it’s documented. I suggest adding case for cleaning such ignored files to the documentation. Expand the list .gitkeep and other dot files can cause difficulties: not everyone uses Git or other tools, and they may need to delete such files (for example, add builds with .build/, .vscode/, .thumbs to the cache).

@michalsn

Copy link
Copy Markdown
Member

Okay, seems like most people think this is a good idea. I'm gonna go with the majority, although I'm a little sceptical about "this should be an opt-in 4.8 feature". That would mean adding another option to Config\Feature, which feels like overkill for something as simple as deleting hidden files.

A cache key may begin with a period, so save() writes a hidden cache file.
clean() called delete_files() without $hidden, getCacheInfo() used a helper
that always skipped dot-leading names, and deleteMatching() used FNM_PERIOD.
Such an entry was not listed by getCacheInfo() nor bulk-removed by clean()
or deleteMatching(), so cache:clear left it in place permanently.

- clean(): pass $hidden = true. .htaccess, index.* and web.config stay
  protected by $htdocs.
- getCacheInfo(): add an optional $hidden parameter to get_dir_file_info(),
  keep its default behavior, and include hidden files.
- deleteMatching(): drop FNM_PERIOD so wildcards match dot-leading entries,
  and skip the .htaccess protection file case-insensitively.

The user guide and the 4.7.7 changelog describe the new behavior.
@gr8man
gr8man force-pushed the fix/cache-file-hidden-entries branch from 22afa4b to 69c8959 Compare November 25, 2026 18:58
@gr8man

gr8man commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto develop. Could the maintainers please let me know the final decision on how to proceed with this PR? Here is an objective summary of the options discussed so far:

Option A: Merge as-is into develop (current PR)

  • Pros:
    • Keeps FileHandler consistent: any key accepted by save()/get() (including dot-leading keys) is reported by getCacheInfo() and removed by clean(), cache:clear, and deleteMatching('*').
    • Avoids adding a new Config\Feature flag for a small edge case.
    • Preserves backward compatibility for get_dir_file_info() ($hidden = false by default) and keeps .htaccess / index.* / web.config protected.
  • Cons:
    • Broadens clean() and deleteMatching('*') in a patch release to also remove other dot-leading files in the cache directory (such as .gitkeep, .gitignore, or .env), which is a behavior change for setups relying on glob() skipping dotfiles.

Option B: Retarget to 4.8 (either as an opt-in Config\Feature flag or as a 4.8 behavior change)

  • Pros:
    • Avoids any behavior change for dot-leading files in the 4.7.x patch line; opt-in via Config\Feature also prevents existing apps from unexpectedly deleting .gitkeep/.gitignore on upgrade.
  • Cons:
    • Adds a Config\Feature option and extra branching for a narrow edge case (if opt-in), and leaves 4.7.x unable to clear dot-leading cache entries via cache:clear or deleteMatching('*').

Option C: Keep current behavior (close PR)

  • Pros:
    • Zero risk of deleting unrelated dotfiles (.gitkeep, .gitignore) and no changes to filesystem_helper.php.
    • Explicit delete('.key') and deleteMatching('.prefix_*') already work.
  • Cons:
    • getCacheInfo(), clean(), and cache:clear remain unable to see or remove dot-leading cache entries.

Happy to update the target branch or adjust the implementation once there is consensus.

@neznaika0

Copy link
Copy Markdown
Contributor

I would prefer:

  • to delete everything except .htaccess and index.*, in order to preserve the exclusions. That is, to specify hidden:true for delete_files().

  • Leave deleteMatching("*") as described in the documentation in accordance with glob().

  • When displaying the list of files, it shows absolutely everything so that you can be sure about hidden files. If you need to perform the deletion manually, rmdir() will throw an error.

  • I don’t understand why the web.config is ignored. Should it be left in the list?

  • We should reflect this in the manual, as we will accept this PR. The manual describes the interface, so all notes can be added below for FileHandler below.

  • delete_files() as well as Cache::clean() do not mention ignored files in the manual.

This branch has not been deployed

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

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants