diff --git a/packages/metro-file-map/src/cache/__tests__/DiskCacheManager-test.js b/packages/metro-file-map/src/cache/__tests__/DiskCacheManager-test.js index e00d96feb7..cf523d6f6e 100644 --- a/packages/metro-file-map/src/cache/__tests__/DiskCacheManager-test.js +++ b/packages/metro-file-map/src/cache/__tests__/DiskCacheManager-test.js @@ -215,6 +215,33 @@ describe('cacheManager', () => { ); }); + test('serialises the snapshot before anything can change it', async () => { + const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig); + // The file map's own state, which may change at the next `await`. + const fileSystemData = new Map(); + const snapshot: CacheData = { + clocks: new Map(), + fileSystemData, + plugins: new Map(), + }; + const written = serialize(snapshot); + const getSnapshot = () => { + queueMicrotask(() => { + fileSystemData.set('later.js', [0, 0, 0, null, 0]); + }); + return snapshot; + }; + await cacheManager.write(getSnapshot, { + changedSinceCacheRead: true, + eventSource: {onChange: () => () => {}}, + onWriteError: () => {}, + }); + expect(mockWriteFile).toHaveBeenCalledWith( + cacheManager.getCacheFilePath(), + written, + ); + }); + test('creates the cache directory if it does not exist', async () => { const cacheManager = new DiskCacheManager({buildParameters}, defaultConfig); const getSnapshot = jest.fn( diff --git a/packages/metro-file-map/src/flow-types.js b/packages/metro-file-map/src/flow-types.js index b39ce730b3..24a8524779 100644 --- a/packages/metro-file-map/src/flow-types.js +++ b/packages/metro-file-map/src/flow-types.js @@ -51,7 +51,9 @@ export interface CacheManager { * crawler - i.e. internal state reflects the current file system state. * * getSnapshot may be retained and called at any time before end(), such as - * in response to eventSource 'change' events. + * in response to eventSource 'change' events. What it returns is the file + * map's own state, not a copy, and may change at the next `await`. + * Serialize it synchronously, or copy it to keep it. */ write( getSnapshot: () => CacheData, @@ -392,6 +394,7 @@ export interface FileSystem { removedFiles: Set, filesToStat: Map, }; + // A copy of the file system's data, which later changes don't affect. getSerializableSnapshot(): CacheData['fileSystemData']; getSha1(file: Path): ?string; getOrComputeSha1(file: Path): Promise; @@ -594,6 +597,12 @@ export interface ReadonlyFileSystemChanges { } export interface MutableFileSystem extends FileSystem { + /** + * The file system's own data, not a copy, so it changes with the file + * system. Serialize it synchronously (see `CacheManager.write`), or use + * `getSerializableSnapshot()` for a copy. + */ + getSerializableState(): CacheData['fileSystemData']; remove(filePath: Path, listener?: FileSystemListener): void; addOrModify( filePath: Path, diff --git a/packages/metro-file-map/src/index.js b/packages/metro-file-map/src/index.js index a62ae0e7e4..d51f237aef 100644 --- a/packages/metro-file-map/src/index.js +++ b/packages/metro-file-map/src/index.js @@ -911,7 +911,7 @@ export default class FileMap extends EventEmitter { * 4. Serialize a snapshot of our raw data via the configured cache manager */ async #takeSnapshotAndPersist( - fileSystem: FileSystem, + fileSystem: MutableFileSystem, clocks: WatchmanClocks, plugins: ReadonlyArray, changedSinceCacheRead: boolean, @@ -920,7 +920,7 @@ export default class FileMap extends EventEmitter { await this.#cacheManager.write( () => ({ clocks: new Map(clocks), - fileSystemData: fileSystem.getSerializableSnapshot(), + fileSystemData: fileSystem.getSerializableState(), plugins: new Map( plugins.map(({plugin}) => [ plugin.name, diff --git a/packages/metro-file-map/src/lib/TreeFS.js b/packages/metro-file-map/src/lib/TreeFS.js index a2b1ea7ec2..88b3a76410 100644 --- a/packages/metro-file-map/src/lib/TreeFS.js +++ b/packages/metro-file-map/src/lib/TreeFS.js @@ -210,6 +210,10 @@ export default class TreeFS implements MutableFileSystem { return this.#cloneTree(this.#rootNode); } + getSerializableState(): CacheData['fileSystemData'] { + return this.#rootNode; + } + static fromDeserializedSnapshot(args: DeserializedSnapshotInput): TreeFS { const {rootDir, fileSystemData, processFile} = args; const tfs = new TreeFS({processFile, rootDir}); diff --git a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js index 74baaa404d..43faac6d99 100644 --- a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js +++ b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js @@ -119,6 +119,15 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => { expect(deserialized.exists(p('/project/foo/another.js'))).toBe(true); }); + test('getSerializableSnapshot returns a copy, getSerializableState does not', () => { + const snapshot = tfs.getSerializableSnapshot(); + const state = tfs.getSerializableState(); + expect(snapshot).toEqual(state); + tfs.addOrModify(p('/project/foo/new.js'), [0, 1, 0, null, 0, null]); + expect(snapshot).not.toEqual(tfs.getSerializableSnapshot()); + expect(state).toEqual(tfs.getSerializableSnapshot()); + }); + test('implements linkStats()', () => { expect(tfs.linkStats(p('/project/link-to-foo/another.js'))).toEqual({ fileType: 'f',