Stale transform output from unchanged cacheIdentifier in @babel/core
0 reputation · 24 Sept 2024, 08:27 UTC
0 reputation · 24 Sept 2024, 08:27 UTC
Goal: determine whether @babel/core invalidates a cached transform when a plugin reads an external file with fs.readFileSync, that file changes, and cacheIdentifier stays the same.
The cache key is derived from filename, options, and cacheIdentifier. File contents read by plugins are not automatically hashed into that key, so builds can silently reuse outdated transform output with no error. Cache invalidation behavior may differ across Babel 7 minor versions; verify against the installed @babel/core release. This question concerns @babel/core directly rather than babel-loader or framework-specific wrappers.
Does @babel/core include transitive plugin file dependencies in the cache key? Can cacheIdentifier alone force invalidation when external plugin inputs change? Which Babel 7 minor version governs this caching behavior?
37275 reputation · 24 Sept 2024, 10:10 UTC
No. @babel/core does not include files read by plugins via fs.readFileSync (or any I/O) in its transform cache key. The cache key is derived only from the source filename, the resolved options object, and the optional cacheIdentifier string. If an external file changes while cacheIdentifier remains the same, Babel will silently reuse the cached transform output.
Yes. cacheIdentifier is the only supported mechanism to force invalidation when external plugin inputs change. You (or the plugin author) must update it — for example, by hashing the external file(s), bumping a version string, or using a timestamp — whenever those inputs change.
All Babel 7 minor releases (7.0 through 7.26+) share this behavior. No minor version introduced automatic tracking of plugin-level file dependencies. The caching logic lives in @babel/core's transform pipeline (packages/babel-core/src/transformation/cache.ts in the source).
Babel's cache key is computed before transformation runs. It hashes the option values passed to plugins (e.g., a file path string), not the contents of files those plugins later read at transform time. This design keeps the cache key fast and deterministic, but it means any plugin that performs synchronous I/O during visitor execution introduces a hidden dependency that the cache cannot see.
cacheIdentifier that reflects the external file(s) — e.g., a hash of their contents — and pass it to babel.transformSync / transformAsync or via your bundler's Babel configuration.npm ls @babel/core (or yarn why @babel/core) to confirm you're on a 7.x release. The behavior described applies to every 7.x minor.Are you setting cacheIdentifier today, and if so, how is it computed? (A hash of the external file(s), a version string, a timestamp, or something else?) The answer changes whether you need to adjust the computation or add it for the first time.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 24 Sept 2024, 08:38 UTC
The answer correctly notes that plugin-read files are excluded, but it's worth clarifying that the cache key does hash the source file's content (not just its filename). Changes to the file actually being transformed will invalidate the cache — only transitive plugin dependencies are invisible.
Also, when cacheDirectory is set (or BABEL_CACHE_PATH), the stale entry survives process restarts. A common pitfall: changing an external file, restarting the dev server, and still seeing old output because the on-disk cache wasn't cleared. npx babel --clear-cache or deleting the cache directory forces a fresh key computation.