Skip to content

metro-file-map: Create fileMapCacheDirectory if it doesn't exist - #2096

Open
shubh73 wants to merge 1 commit into
react:mainfrom
shubh73:file-map-cache-directory
Open

shubh73 wants to merge 1 commit into
react:mainfrom
shubh73:file-map-cache-directory

Conversation

@shubh73

@shubh73 shubh73 commented Oct 23, 2026 •

Copy link
Copy Markdown

If fileMapCacheDirectory points at a directory that doesn't exist yet, the file map cache is never written, so every restart crawls the project from scratch. The only sign is a warning at startup:

[metro-file-map] Cache write error
: Error: ENOENT: no such file or directory, open '/path/to/project/node_modules/.cache/metro-file-map/metro-file-map-<hash>-<hash>'

The default, os.tmpdir(), always exists, so this only affects a custom directory - but a project-local one like node_modules/.cache/metro-file-map won't exist on a fresh checkout or after node_modules is removed. The transform cache doesn't have this problem, because FileStore creates its directory when a write fails with ENOENT.

This change does the same in DiskCacheManager: on ENOENT, create the directory (recursively) and retry the write once. Writes into an existing directory are unchanged.

Note

Found in an Expo SDK 57 app. Expo CLI uses its fork, @expo/metro-file-map, which has the same writeFile in DiskCacheManager.ts, so it'll need the same change.

Changelog:

 - **[Fix]**: Create `fileMapCacheDirectory` if it doesn't exist, rather than failing to persist the file map cache

Test plan:
New test in DiskCacheManager-test.js where the first write fails with ENOENT, asserting that the directory is created and the snapshot written without an error. It fails before this change and passes after. A second test checks that other errors, e.g. EACCES, are still reported without a retry.

Also checked end-to-end with createFileMap and fileMapCacheDirectory set to a missing <project>/node_modules/.cache/metro-file-map: before, the warning above and no directory; after, no warning and the cache file is written.

yarn jest packages/metro-file-map packages/metro/src/node-haste packages/metro/src/DeltaBundler
yarn flow check
yarn eslint packages/metro-file-map
yarn verify-api-snapshots

馃 Generated with Claude Code

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 23, 2026
If `fileMapCacheDirectory` points at a directory that doesn't exist yet, the file map cache is never written, so every restart crawls the project from scratch. The only sign is a warning at startup:

```
[metro-file-map] Cache write error
: Error: ENOENT: no such file or directory, open '/path/to/project/node_modules/.cache/metro-file-map/metro-file-map-<hash>-<hash>'
```

The default, `os.tmpdir()`, always exists, so this only affects a custom directory - but a project-local one like `node_modules/.cache/metro-file-map` won't exist on a fresh checkout or after `node_modules` is removed. The transform cache doesn't have this problem, because `FileStore` creates its directory when a write fails with `ENOENT`.

This change does the same in `DiskCacheManager`: on `ENOENT`, create the directory (recursively) and retry the write once. Writes into an existing directory are unchanged.

> [!NOTE]
> Found in an Expo SDK 57 app. Expo CLI uses its fork, `@expo/metro-file-map`, which has the same `writeFile` in `DiskCacheManager.ts`, so it'll need the same change.

Changelog:
```
 - **[Fix]**: Create `fileMapCacheDirectory` if it doesn't exist, rather than failing to persist the file map cache
```

Test plan:
New test in `DiskCacheManager-test.js` where the first write fails with `ENOENT`, asserting that the directory is created and the snapshot written without an error. It fails before this change and passes after. A second test checks that other errors, e.g. `EACCES`, are still reported without a retry.

Also checked end-to-end with `createFileMap` and `fileMapCacheDirectory` set to a missing `<project>/node_modules/.cache/metro-file-map`: before, the warning above and no directory; after, no warning and the cache file is written.

```
yarn jest packages/metro-file-map packages/metro/src/node-haste packages/metro/src/DeltaBundler
yarn flow check
yarn eslint packages/metro-file-map
yarn verify-api-snapshots
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shubh73
shubh73 force-pushed the file-map-cache-directory branch from 8e5c936 to aa11861 Compare October 23, 2026 19:42

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant