Skip to content

Commit

Permalink
FileStore: lazily create empty cache directories
Browse files Browse the repository at this point in the history
Summary:
Metro's transform cache `FileStore` currently creates its file structure on construction, which typically happens within `metro-config`'s `getDefaultValues`:

https://github.com/facebook/metro/blob/46aad4669098c694134bbc4b4f98cb9db9fdd7a2/packages/metro-config/src/defaults/index.js#L156

This means that's there's no way to use Metro's `loadConfig` or `getDefaultConfig` without creating an empty cache directory structure within `os.tmpdir()`, even if you subsequently override `cacheStores: []` and don't want a cache.

`getDefaultConfig()` shouldn't be writing to the file system - this is a bug. There's also no good reason for `FileStore` to initialise this structure eagerly, since with a lazy structure:
 - Cache misses will fail in the same way (faster, if anything).
 - Cache writes already create directories if they don't exist (as they must, to avoid crashing if the user deletes the cache while Metro is running), and since cache writes are relatively slow (typically following a transform), the extra mkdirp isn't likely to be significant.

Here we make directory creation lazy.

Changelog:
```
 - **[Fix]**: Don't eagerly create empty cache directories during `getDefaultConfig`
```

Reviewed By: vzaidman

Differential Revision: D63030122

fbshipit-source-id: fd28f88113d09831affebec0deca4d33de877628
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 20, 2024
1 parent a792d85 commit bfd9473
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
10 changes: 0 additions & 10 deletions packages/metro-cache/src/stores/FileStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class FileStore<T> {

constructor(options: Options) {
this._root = options.root;
this._createDirs();
}

async get(key: Buffer): Promise<?T> {
Expand Down Expand Up @@ -72,7 +71,6 @@ class FileStore<T> {

clear() {
this._removeDirs();
this._createDirs();
}

_getFilePath(key: Buffer): string {
Expand All @@ -83,14 +81,6 @@ class FileStore<T> {
);
}

_createDirs() {
for (let i = 0; i < 256; i++) {
fs.mkdirSync(path.join(this._root, ('0' + i.toString(16)).slice(-2)), {
recursive: true,
});
}
}

_removeDirs() {
for (let i = 0; i < 256; i++) {
fs.rmSync(path.join(this._root, ('0' + i.toString(16)).slice(-2)), {
Expand Down
4 changes: 3 additions & 1 deletion packages/metro-cache/src/stores/__tests__/FileStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

const {dirname} = require('path');

describe('FileStore', () => {
let FileStore;
let fs;
Expand Down Expand Up @@ -44,7 +46,7 @@ describe('FileStore', () => {
const fileStore = new FileStore({root: '/root'});
const cache = Buffer.from([0xfa, 0xce, 0xb0, 0x0c]);
const filePath = fileStore._getFilePath(cache);

fs.mkdirSync(dirname(filePath), {recursive: true});
fs.writeFileSync(filePath, '');
expect(await fileStore.get(cache)).toEqual(null);
});
Expand Down

0 comments on commit bfd9473

Please sign in to comment.