platformio-core icon indicating copy to clipboard operation
platformio-core copied to clipboard

build_cache_dir has no size/age limit and grows without bound; request retention option

Open aphelps opened this issue 1 month ago • 1 comments

With build_cache_dir set, the SCons CacheDir is never pruned. Every flag, library or framework change adds hash-keyed entries, and old ones are never evicted.

In my case an ESP32/Arduino (WLED) setup reached about 62 GB (75,000 files, about 93% unused for over 45 days) in about 10 months.

The docs for the option don't mention growth, and pio system prune --cache doesn't touch this directory. I found no existing issue about this (closest: #4574).

Proposal

Two optional settings, both off by default (no behaviour change):

  • build_cache_max_age: remove entries not used for N days
  • build_cache_max_size: cap the cache size, e.g. 10GB; least recently used entries are removed first

SCons updates a cache file's mtime on every hit, so mtime works as "last used". The prune runs at most once a day when a build starts, and only touches entries inside the cache's subfolders (not SCons's own config/.sconsign files). Size is enforced at build start, so a build can briefly exceed the cap.

Status

I have a working implementation with tests, verified against real builds, ready as a PR. It was developed with AI assistance (Claude Code) and reviewed by me.

Would you accept a PR for this, and do you have preferences on the option names?

aphelps avatar Oct 21 '26 02:10 aphelps

Your reading matches the code path: pio system prune works on the package and platform directories under ~/.platformio, and the SCons CacheDir is a separate tree that SCons owns - PlatformIO never enumerates it, so nothing in prune can remove an entry.

Until your PR lands, a scheduled prune does the job, and mtime is the right key for the reason you give (SCons touches the cache file on every hit):

# entries not written for 30 days
find "$HOME/.platformio/cache" -type f -mtime +30 -delete
find "$HOME/.platformio/cache" -type d -empty -delete

Two caveats from running this on a build machine:

  • -atime is unreliable under relatime/noatime, which is what most CI images mount with, so -mtime is the safer key even though it keeps files that were read but never rewritten.
  • Deleting cache files while a build is running can hand SCons a half-written object; run the prune between builds, or take the same lock the build takes.

Your "at build start, at most once a day" proposal avoids both, which is why it is a better shape than a cron job.

One data point for the PR description, if useful: the growth is worst when the framework version is unpinned, because every change that alters the framework hash produces a new entry for every object file and the old set stays forever. A sentence about that in the docs for build_cache_dir would prevent most of these reports.

Esmail-sarhadi avatar Nov 02 '26 05:11 Esmail-sarhadi