Limits of Grunt's File Expansion Caching Across Tasks
0 reputation · 19 Jul 2021, 00:06 UTC
0 reputation · 19 Jul 2021, 00:06 UTC
Establish a repeatable development environment by leveraging Grunt’s built‑in file expansion cache (grunt.file.cached).
The cache is keyed solely by the glob pattern string. Identical patterns used in separate tasks share the same cache entry unless the working directory changes. Modifying files that match a cached pattern does not automatically invalidate the cache; a subsequent expand may return stale results unless grunt.file.cached.clear() is explicitly invoked. Documentation does not specify a deterministic rule for cache purging across task boundaries.
35775 reputation · 19 Jul 2021, 05:00 UTC
Question 1: No. When a task modifies files matching a cached glob pattern, grunt.file.cached does not automatically update. Subsequent expand calls with the same pattern and working directory return the stale cached array until grunt.file.cached.clear() is called.
Question 2: Yes. The cache key includes process.cwd() at call time. Changing the working directory between tasks (via grunt.file.setBase or process.chdir) creates a separate cache entry for identical glob patterns.
Question 3: No documented strategy exists. The only supported invalidation method is grunt.file.cached.clear(), which wipes the entire cache. Grunt's source (lib/grunt/file.js) shows a simple Map with no granular purge API.
grunt-shell, grunt-exec) do not share the parent's in-memory cache. File changes made in a child are invisible to the parent's cached expands unless the parent re-expands after the child exits.grunt.file.cached.clear() at the start of any task that depends on up-to-date file lists.grunt.file.expand for globs that may change during the build.process.cwd() before expanding—but treat this as an implementation detail, not a guaranteed contract.// Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({});
grunt.registerTask('write', 'Create a new .txt file', function() {
grunt.file.write('new.txt', 'hello');
});
grunt.registerTask('readCached', 'Expand with cache', function() {
const files = grunt.file.cached.expand('*.txt');
grunt.log.writeln('Cached expand found:', files);
});
grunt.registerTask('readFresh', 'Expand without cache', function() {
const files = grunt.file.expand('*.txt');
grunt.log.writeln('Fresh expand found:', files);
});
grunt.registerTask('clear', 'Clear cache', function() {
grunt.file.cached.clear();
grunt.log.writeln('Cache cleared');
});
// Run: grunt write readCached readFresh clear readCached
};
Run the task sequence above. readCached after write will miss new.txt; readFresh will include it. After clear, the next readCached picks up the new file.
If you observe different behavior, confirm the Grunt version (grunt --version) and whether any plugin monkey-patches grunt.file.cached. The cache implementation has been stable since Grunt 0.4, but plugins can replace it.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.