Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ The number of workers to use for parallel processing in Metro. Defaults to rough

Type: `string`

The path to the `metro-file-map` cache directory, defaults to `os.tmpdir()`.
The path to the `metro-file-map` cache directory, defaults to `os.tmpdir()`. Created on first write if it doesn't exist.

#### `hasteMapCacheDirectory` <div class="label deprecated">Deprecated</div>

Expand Down
14 changes: 13 additions & 1 deletion packages/metro-file-map/src/cache/DiskCacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,19 @@ export class DiskCacheManager implements CacheManager {
}
const data = getSnapshot();
this.#hasUnwrittenChanges = false;
await fsPromises.writeFile(this.#cachePath, serialize(data));
const serialized = serialize(data);
try {
await fsPromises.writeFile(this.#cachePath, serialized);
} catch (e) {
if (e?.code !== 'ENOENT') {
throw e;
}
// The cache directory doesn't exist yet - create it and retry.
await fsPromises.mkdir(path.dirname(this.#cachePath), {
recursive: true,
});
await fsPromises.writeFile(this.#cachePath, serialized);
}
debug('Written cache to %s', this.#cachePath);
})
.catch(onWriteError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import EventEmitter from 'node:events';
import * as path from 'node:path';
import {serialize} from 'node:v8';

const mockMkdir = jest.fn();
const mockReadFile = jest.fn();
const mockWriteFile = jest.fn();

jest.mock('node:fs', () => ({
promises: {
mkdir: (...args) => mockMkdir(...args),
readFile: (...args) => mockReadFile(...args),
writeFile: (...args) => mockWriteFile(...args),
},
Expand Down Expand Up @@ -189,6 +191,58 @@ describe('cacheManager', () => {
);
});

test('creates the cache directory if it does not exist', async () => {
const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig);
const getSnapshot = jest.fn(
() =>
({
clocks: new Map([['foo', 'bar']]),
fileSystemData: new Map(),
plugins: new Map(),
}) as CacheData,
);
mockWriteFile.mockRejectedValueOnce({code: 'ENOENT'});
const onWriteError = jest.fn();
await cacheManager.write(getSnapshot, {
changedSinceCacheRead: true,
eventSource: {onChange: () => () => {}},
onWriteError,
});
expect(mockMkdir).toHaveBeenCalledWith(
path.dirname(cacheManager.getCacheFilePath()),
{recursive: true},
);
expect(mockWriteFile).toHaveBeenCalledTimes(2);
expect(mockWriteFile).toHaveBeenLastCalledWith(
cacheManager.getCacheFilePath(),
serialize(getSnapshot()),
);
expect(onWriteError).not.toHaveBeenCalled();
});

test('reports other write errors without retrying', async () => {
const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig);
const error = {code: 'EACCES'};
mockWriteFile.mockRejectedValueOnce(error);
const onWriteError = jest.fn();
await cacheManager.write(
() =>
({
clocks: new Map(),
fileSystemData: new Map(),
plugins: new Map(),
}) as CacheData,
{
changedSinceCacheRead: true,
eventSource: {onChange: () => () => {}},
onWriteError,
},
);
expect(mockMkdir).not.toHaveBeenCalled();
expect(mockWriteFile).toHaveBeenCalledTimes(1);
expect(onWriteError).toHaveBeenCalledWith(error);
});

test('does not write when there have been no changes', async () => {
const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig);
const getSnapshot = jest.fn(
Expand Down
Loading