Repository navigation
Commit 4540816
metro-file-map: Create
* metro-file-map: Create `fileMapCacheDirectory` if it doesn't exist
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>
* Docs: Note that fileMapCacheDirectory is created on first write
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Rob Hogan <2590098+robhogan@users.noreply.github.com>fileMapCacheDirectory if it doesn't exist (#2096)1 parent 56efab3 commit 4540816
3 files changed
Lines changed: 68 additions & 2 deletions
File tree
- docs
- packages/metro-file-map/src/cache
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
178 | | - | |
| 178 | + | |
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
119 | 131 | | |
120 | 132 | | |
121 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
192 | 246 | | |
193 | 247 | | |
194 | 248 | | |
| |||
0 commit comments