GitHub Actions Cache "Cannot open: File exists" / tar Extraction Failed
A cache restore fails during tar extraction with "Cannot open: File exists" because files already exist at the restore path - a prior step populated the directory, so the archive cannot overwrite into it.
What this error means
The cache restore step fails with tar errors like "Cannot open: File exists", usually because something created or restored files at the same path before the cache action extracted its archive.
/usr/bin/tar: ./node_modules/.bin/x: Cannot open: File exists
/usr/bin/tar: Exiting with failure status due to previous errors
Warning: Failed to restore: ...Common causes
Path already populated before restore
A step (an install, a checkout of vendored files, a second cache) created files at the cache path. tar then refuses to overwrite existing files during extraction.
Two cache steps target the same path
Restoring two caches into the same directory makes the second collide with files the first already wrote.
How to fix it
Restore before populating the path
Place the cache restore before any step that writes to that path, so extraction lands in a clean directory.
- uses: actions/checkout@v4
- uses: actions/cache@v4 # restore FIRST, into an empty path
with:
path: node_modules
key: deps-${{ hashFiles('package-lock.json') }}
- run: npm ci # then populate / reconcileAvoid overlapping cache paths
- Do not restore two caches into the same directory.
- Clear or use a clean directory before restoring if an earlier step must populate it.
- If a tool must write first, cache a different, non-overlapping path.
How to prevent it
- Restore caches before steps that write to the same path.
- Keep each cache pointed at a distinct, non-overlapping directory.
- Avoid restoring into a directory another step already populated.