Skip to content

Commit

Permalink
Warn and annotate on metro-file-map cache read error
Browse files Browse the repository at this point in the history
Summary:
Currently `metro-file-map` will *silently* ignore errors encountered when reading from the configured cache manager, and fall back to a cold crawl.

This adds a console warning and records an annotation to give these error cases more visibility.

In addition, propagate unexpected errors, such as deserialisation failures, from the default `DiskCacheManager`.

Changelog:
**[Fix]** Log warning on unexpected error during `metro-file-map`cache read.

Reviewed By: huntie

Differential Revision: D39514918

fbshipit-source-id: 4569a1ed7f2114307c96c0b5064b367ebd487947
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 16, 2022
1 parent ace28a9 commit 7028b7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/metro-file-map/src/cache/DiskCacheManager.js
Expand Up @@ -68,8 +68,14 @@ export class DiskCacheManager implements CacheManager {
async read(): Promise<?InternalData> {
try {
return deserialize(readFileSync(this._cachePath));
} catch {}
return null;
} catch (e) {
if (e?.code === 'ENOENT') {
// Cache file not found - not considered an error.
return null;
}
// Rethrow anything else.
throw e;
}
}

async write(
Expand Down
10 changes: 9 additions & 1 deletion packages/metro-file-map/src/index.js
Expand Up @@ -411,7 +411,15 @@ export default class HasteMap extends EventEmitter {
this._options.perfLogger?.point('read_start');
try {
data = await this._cacheManager.read();
} catch {}
} catch (e) {
this._console.warn(
'Error while reading cache, falling back to a full crawl:\n',
e,
);
this._options.perfLogger?.annotate({
string: {cacheReadError: e.toString()},
});
}
data = data ?? this._createEmptyMap();
this._options.perfLogger?.point('read_end');

Expand Down

0 comments on commit 7028b7f

Please sign in to comment.