Skip to content

Commit a2e2c7a

Browse files
committed
fix(fmt): avoid reading unsupported cache misses
1 parent 30e8fbd commit a2e2c7a

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

packages/rstack/src/fmt/worker.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ const formatFile = async ({
2525
}: FormatFileTask): Promise<FmtWorkerResult> => {
2626
let source: string | undefined;
2727
let contentHash: string | undefined;
28+
const fileCache = shouldWrite ? undefined : cache;
29+
30+
const readSource = (): string => {
31+
if (!fileCache) {
32+
return readFileSync(file.path, 'utf8');
33+
}
2834

29-
if (cache && !shouldWrite) {
3035
const content = readFileSync(file.path);
3136
contentHash = hashContent(content);
32-
source = content.toString('utf8');
37+
return content.toString('utf8');
38+
};
3339

34-
const { entry, optionsHash } = cache;
35-
if (entry?.[0] === contentHash && entry[1] === optionsHash) {
40+
if (fileCache?.entry && fileCache.entry[1] === fileCache.optionsHash) {
41+
source = readSource();
42+
const { entry } = fileCache;
43+
if (entry[0] === contentHash) {
3644
return { status: entry[2] === 'clean' ? 'unchanged' : 'changed' };
3745
}
3846
}
3947

4048
const { formatFmtSource } = await import('./format.ts');
41-
const result = await formatFmtSource(file, () => (source ??= readFileSync(file.path, 'utf8')));
49+
const result = await formatFmtSource(file, () => (source ??= readSource()));
4250
if (result.status === 'unsupported') {
4351
return { status: 'unsupported' };
4452
}
@@ -50,11 +58,15 @@ const formatFile = async ({
5058
}
5159

5260
const status = unchanged ? 'unchanged' : 'changed';
53-
if (!cache || contentHash === undefined) {
61+
if (!fileCache || contentHash === undefined) {
5462
return { status };
5563
}
5664

57-
const cacheEntry: FmtCacheEntry = [contentHash, cache.optionsHash, unchanged ? 'clean' : 'dirty'];
65+
const cacheEntry: FmtCacheEntry = [
66+
contentHash,
67+
fileCache.optionsHash,
68+
unchanged ? 'clean' : 'dirty',
69+
];
5870
return { status, cacheEntry };
5971
};
6072

packages/rstack/tests/fmt/worker.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path';
12
import { readFileSync } from 'node:fs';
23
import { expect, test } from 'rstack/test';
34
import { sha256 } from '../../src/fmt/cacheIdentity.ts';
@@ -72,3 +73,21 @@ test('returns cached states before resolving the parser', async () => {
7273
}
7374
});
7475
});
76+
77+
test('resolves parser support before reading on a cache miss', async () => {
78+
await withTempProject(async (rootPath) => {
79+
await expect(
80+
formatFile({
81+
file: {
82+
path: path.join(rootPath, 'missing.unknown'),
83+
options: {},
84+
},
85+
shouldWrite: false,
86+
cache: {
87+
entry: undefined,
88+
optionsHash: 'options',
89+
},
90+
}),
91+
).resolves.toEqual({ status: 'unsupported' });
92+
});
93+
});

0 commit comments

Comments
 (0)