Skip to content

Commit ce41ded

Browse files
authored
fix(js): ensure the tsconfig files cache is correctly busted when implementation changes (#30689)
## Current Behavior The `@nx/js/typescript` plugin sometimes throws an error due to a mismatch between the cached information for the tsconfig files and a newer implementation. ## Expected Behavior The `@nx/js/typescript` plugin should correctly invalidate the cached tsconfig files when the implementation changes. ## Related Issue(s) Fixes #
1 parent f3013cc commit ce41ded

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

packages/js/src/plugins/typescript/plugin.ts

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@ type TsconfigCacheData = {
8282
hash: string;
8383
extendedFilesHash: string;
8484
};
85+
type TsconfigCache = {
86+
version: number;
87+
data: Record<string, TsconfigCacheData>;
88+
};
8589

8690
let ts: typeof import('typescript');
8791
const pmc = getPackageManagerCommand();
8892

89-
let tsConfigCache: Record<string, TsconfigCacheData>;
90-
const tsConfigCachePath = join(workspaceDataDirectory, 'tsconfig-files.hash');
93+
const TSCONFIG_CACHE_VERSION = 1;
94+
const TS_CONFIG_CACHE_PATH = join(
95+
workspaceDataDirectory,
96+
'tsconfig-files.hash'
97+
);
98+
let tsConfigCacheData: Record<string, TsconfigCacheData>;
9199
let cache: {
92100
fileHashes: Record<string, string>;
93101
rawFiles: Record<string, string>;
@@ -103,10 +111,25 @@ function readFromCache<T extends object>(cachePath: string): T {
103111
return {} as T;
104112
}
105113
}
114+
function readTsConfigCacheData(): Record<string, TsconfigCacheData> {
115+
const cache = readFromCache<TsconfigCache>(TS_CONFIG_CACHE_PATH);
116+
117+
if (cache.version !== TSCONFIG_CACHE_VERSION) {
118+
return {};
119+
}
120+
121+
return cache.data;
122+
}
106123

107124
function writeToCache<T extends object>(cachePath: string, data: T) {
108125
writeJsonFile(cachePath, data, { spaces: 0 });
109126
}
127+
function writeTsConfigCache(data: Record<string, TsconfigCacheData>) {
128+
writeToCache(TS_CONFIG_CACHE_PATH, {
129+
version: TSCONFIG_CACHE_VERSION,
130+
data,
131+
});
132+
}
110133

111134
/**
112135
* @deprecated The 'createDependencies' function is now a no-op. This functionality is included in 'createNodesV2'.
@@ -175,9 +198,8 @@ export const createNodesV2: CreateNodesV2<TscPluginOptions> = [
175198
);
176199
} finally {
177200
writeToCache(targetsCachePath, targetsCache);
178-
writeToCache(
179-
tsConfigCachePath,
180-
toRelativePaths(tsConfigCache, context.workspaceRoot)
201+
writeTsConfigCache(
202+
toRelativePaths(tsConfigCacheData, context.workspaceRoot)
181203
);
182204
}
183205
},
@@ -208,9 +230,8 @@ export const createNodes: CreateNodes<TscPluginOptions> = [
208230
context
209231
);
210232

211-
writeToCache(
212-
tsConfigCachePath,
213-
toRelativePaths(tsConfigCache, context.workspaceRoot)
233+
writeTsConfigCache(
234+
toRelativePaths(tsConfigCacheData, context.workspaceRoot)
214235
);
215236

216237
return {
@@ -1097,19 +1118,16 @@ function retrieveTsConfigFromCache(
10971118

10981119
// we don't need to check the hash if it's in the cache, because we've already
10991120
// checked it when we initially populated the cache
1100-
return tsConfigCache[relativePath]
1101-
? tsConfigCache[relativePath].data
1121+
return tsConfigCacheData[relativePath]
1122+
? tsConfigCacheData[relativePath].data
11021123
: readTsConfigAndCache(tsConfigPath, workspaceRoot);
11031124
}
11041125

11051126
function initializeTsConfigCache(
11061127
configFilePaths: readonly string[],
11071128
workspaceRoot: string
11081129
): void {
1109-
tsConfigCache = toAbsolutePaths(
1110-
readFromCache<Record<string, TsconfigCacheData>>(tsConfigCachePath),
1111-
workspaceRoot
1112-
);
1130+
tsConfigCacheData = toAbsolutePaths(readTsConfigCacheData(), workspaceRoot);
11131131

11141132
// ensure hashes are checked and the cache is invalidated and populated as needed
11151133
for (const configFilePath of configFilePaths) {
@@ -1127,15 +1145,17 @@ function readTsConfigAndCache(
11271145

11281146
let extendedFilesHash: string;
11291147
if (
1130-
tsConfigCache[relativePath] &&
1131-
tsConfigCache[relativePath].hash === hash
1148+
tsConfigCacheData[relativePath] &&
1149+
tsConfigCacheData[relativePath].hash === hash
11321150
) {
11331151
extendedFilesHash = getExtendedFilesHash(
1134-
tsConfigCache[relativePath].data.extendedConfigFiles,
1152+
tsConfigCacheData[relativePath].data.extendedConfigFiles,
11351153
workspaceRoot
11361154
);
1137-
if (tsConfigCache[relativePath].extendedFilesHash === extendedFilesHash) {
1138-
return tsConfigCache[relativePath].data;
1155+
if (
1156+
tsConfigCacheData[relativePath].extendedFilesHash === extendedFilesHash
1157+
) {
1158+
return tsConfigCacheData[relativePath].data;
11391159
}
11401160
}
11411161

@@ -1161,7 +1181,7 @@ function readTsConfigAndCache(
11611181
workspaceRoot
11621182
);
11631183

1164-
tsConfigCache[relativePath] = {
1184+
tsConfigCacheData[relativePath] = {
11651185
data: {
11661186
options: tsConfig.options,
11671187
projectReferences: tsConfig.projectReferences,
@@ -1172,7 +1192,7 @@ function readTsConfigAndCache(
11721192
extendedFilesHash,
11731193
};
11741194

1175-
return tsConfigCache[relativePath].data;
1195+
return tsConfigCacheData[relativePath].data;
11761196
}
11771197

11781198
function getExtendedFilesHash(

0 commit comments

Comments
 (0)