Skip to content

Commit e23fa7b

Browse files
committed
refactor(cli): simplify cache key and fix cache fragmentation
- Remove argv from cache key (was causing cache fragmentation due to argument order) - Use absolute paths instead of relative paths - Include languages in cache key to distinguish --project vs --vue-project - Simplify cache directory structure: tmpdir/tsslint-cache/{version}/
1 parent 4229354 commit e23fa7b

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

packages/cli/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Project {
168168
);
169169

170170
if (!process.argv.includes('--force')) {
171-
this.cache = cache.loadCache(this.tsconfig, this.configFile, ts.sys.createHash);
171+
this.cache = cache.loadCache(this.tsconfig, this.configFile, this.languages, ts.sys.createHash);
172172
}
173173

174174
return this;
@@ -585,7 +585,7 @@ class Project {
585585
);
586586
}
587587

588-
cache.saveCache(project.tsconfig, project.configFile!, project.cache, ts.sys.createHash);
588+
cache.saveCache(project.tsconfig, project.configFile!, project.languages, project.cache, ts.sys.createHash);
589589

590590
await startWorker(linterWorker);
591591
}

packages/cli/lib/cache.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ const pkg = require('../package.json');
1010
export function loadCache(
1111
tsconfig: string,
1212
configFilePath: string,
13+
languages: string[],
1314
createHash: (path: string) => string = btoa,
1415
): CacheData {
15-
const outDir = getTsslintCachePath(configFilePath, createHash);
16-
const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '_'
17-
+ createHash(JSON.stringify(process.argv)) + '_' + createHash(path.relative(outDir, tsconfig)) + '.cache.json';
16+
const outDir = getTsslintCachePath();
17+
const cacheKey = configFilePath + '\0' + tsconfig + '\0' + languages.sort().join(',');
18+
const cacheFileName = createHash(cacheKey) + '.cache.json';
1819
const cacheFilePath = path.join(outDir, cacheFileName);
1920
const cacheFileStat = fs.statSync(cacheFilePath, { throwIfNoEntry: false });
2021
const configFileStat = fs.statSync(configFilePath, { throwIfNoEntry: false });
@@ -32,19 +33,18 @@ export function loadCache(
3233
export function saveCache(
3334
tsconfig: string,
3435
configFilePath: string,
36+
languages: string[],
3537
cache: CacheData,
3638
createHash: (path: string) => string = btoa,
3739
): void {
38-
const outDir = getTsslintCachePath(configFilePath, createHash);
39-
const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '_'
40-
+ createHash(JSON.stringify(process.argv)) + '_' + createHash(path.relative(outDir, tsconfig)) + '.cache.json';
40+
const outDir = getTsslintCachePath();
41+
const cacheKey = configFilePath + '\0' + tsconfig + '\0' + languages.sort().join(',');
42+
const cacheFileName = createHash(cacheKey) + '.cache.json';
4143
const cacheFilePath = path.join(outDir, cacheFileName);
4244
fs.mkdirSync(outDir, { recursive: true });
4345
fs.writeFileSync(cacheFilePath, JSON.stringify(cache));
4446
}
4547

46-
function getTsslintCachePath(configFilePath: string, createHash: (path: string) => string): string {
47-
const projectRoot = path.resolve(configFilePath, '..');
48-
const projectHash = createHash(projectRoot);
49-
return path.join(os.tmpdir(), 'tsslint-cache-' + pkg.version + '-' + projectHash);
48+
function getTsslintCachePath(): string {
49+
return path.join(os.tmpdir(), 'tsslint-cache', pkg.version);
5050
}

0 commit comments

Comments
 (0)