Skip to content

Commit a4a185f

Browse files
authored
fix(misc): various inference plugins caching should track changes (#23315)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior Plugin's cache entries overwrite each other if multiple instances of the same plugin are running. They also don't remember previous cache states. ## Expected Behavior Plugin's caches grow as changes are made, and don't overwrite previous entries. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 445916f commit a4a185f

File tree

21 files changed

+389
-422
lines changed

21 files changed

+389
-422
lines changed

packages/cypress/src/plugins/plugin.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ describe('@nx/cypress/plugin', () => {
348348
});
349349

350350
function mockCypressConfig(cypressConfig: Cypress.ConfigOptions) {
351+
// This isn't JS, but all that really matters here
352+
// is that the hash is different after updating the
353+
// config file. The actual config read is mocked below.
354+
tempFs.createFileSync('cypress.config.js', JSON.stringify(cypressConfig));
351355
jest.mock(
352356
join(tempFs.tempDir, 'cypress.config.js'),
353357
() => ({

packages/cypress/src/plugins/plugin.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ export interface CypressPluginOptions {
3131
}
3232

3333
const cachePath = join(projectGraphCacheDirectory, 'cypress.hash');
34-
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
35-
36-
const calculatedTargets: Record<string, CypressTargets> = {};
34+
const targetsCache = readTargetsCache();
3735

3836
function readTargetsCache(): Record<string, CypressTargets> {
39-
return readJsonFile(cachePath);
37+
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
4038
}
4139

42-
function writeTargetsToCache(targets: Record<string, CypressTargets>) {
43-
writeJsonFile(cachePath, targets);
40+
function writeTargetsToCache() {
41+
const oldCache = readTargetsCache();
42+
writeJsonFile(cachePath, {
43+
...oldCache,
44+
targetsCache,
45+
});
4446
}
4547

4648
export const createDependencies: CreateDependencies = () => {
47-
writeTargetsToCache(calculatedTargets);
49+
writeTargetsToCache();
4850
return [];
4951
};
5052

@@ -67,16 +69,13 @@ export const createNodes: CreateNodes<CypressPluginOptions> = [
6769
getLockFileName(detectPackageManager(context.workspaceRoot)),
6870
]);
6971

70-
const { targets, metadata } = targetsCache[hash]
71-
? targetsCache[hash]
72-
: await buildCypressTargets(
73-
configFilePath,
74-
projectRoot,
75-
options,
76-
context
77-
);
78-
79-
calculatedTargets[hash] = { targets, metadata };
72+
targetsCache[hash] ??= await buildCypressTargets(
73+
configFilePath,
74+
projectRoot,
75+
options,
76+
context
77+
);
78+
const { targets, metadata } = targetsCache[hash];
8079

8180
const project: Omit<ProjectConfiguration, 'root'> = {
8281
projectType: 'application',

packages/detox/src/plugins/plugin.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,21 @@ export interface DetoxPluginOptions {
2222
}
2323

2424
const cachePath = join(projectGraphCacheDirectory, 'detox.hash');
25-
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
26-
27-
const calculatedTargets: Record<
28-
string,
29-
Record<string, TargetConfiguration>
30-
> = {};
25+
const targetsCache = readTargetsCache();
3126

3227
function readTargetsCache(): Record<
3328
string,
3429
Record<string, TargetConfiguration<DetoxPluginOptions>>
3530
> {
36-
return readJsonFile(cachePath);
31+
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
3732
}
3833

39-
function writeTargetsToCache(
40-
targets: Record<
41-
string,
42-
Record<string, TargetConfiguration<DetoxPluginOptions>>
43-
>
44-
) {
45-
writeJsonFile(cachePath, targets);
34+
function writeTargetsToCache() {
35+
writeJsonFile(cachePath, targetsCache);
4636
}
4737

4838
export const createDependencies: CreateDependencies = () => {
49-
writeTargetsToCache(calculatedTargets);
39+
writeTargetsToCache();
5040
return [];
5141
};
5242

@@ -66,16 +56,12 @@ export const createNodes: CreateNodes<DetoxPluginOptions> = [
6656
getLockFileName(detectPackageManager(context.workspaceRoot)),
6757
]);
6858

69-
const targets = targetsCache[hash]
70-
? targetsCache[hash]
71-
: buildDetoxTargets(projectRoot, options, context);
72-
73-
calculatedTargets[hash] = targets;
59+
targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context);
7460

7561
return {
7662
projects: {
7763
[projectRoot]: {
78-
targets,
64+
targets: targetsCache[hash],
7965
},
8066
},
8167
};

packages/expo/plugins/plugin.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,25 @@ export interface ExpoPluginOptions {
3030
}
3131

3232
const cachePath = join(projectGraphCacheDirectory, 'expo.hash');
33-
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
34-
35-
const calculatedTargets: Record<
36-
string,
37-
Record<string, TargetConfiguration>
38-
> = {};
33+
const targetsCache = readTargetsCache();
3934

4035
function readTargetsCache(): Record<
4136
string,
4237
Record<string, TargetConfiguration<ExpoPluginOptions>>
4338
> {
44-
return readJsonFile(cachePath);
39+
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
4540
}
4641

47-
function writeTargetsToCache(
48-
targets: Record<
49-
string,
50-
Record<string, TargetConfiguration<ExpoPluginOptions>>
51-
>
52-
) {
53-
writeJsonFile(cachePath, targets);
42+
function writeTargetsToCache() {
43+
const oldCache = readTargetsCache();
44+
writeJsonFile(cachePath, {
45+
...oldCache,
46+
targetsCache,
47+
});
5448
}
5549

5650
export const createDependencies: CreateDependencies = () => {
57-
writeTargetsToCache(calculatedTargets);
51+
writeTargetsToCache();
5852
return [];
5953
};
6054

@@ -82,16 +76,12 @@ export const createNodes: CreateNodes<ExpoPluginOptions> = [
8276
getLockFileName(detectPackageManager(context.workspaceRoot)),
8377
]);
8478

85-
const targets = targetsCache[hash]
86-
? targetsCache[hash]
87-
: buildExpoTargets(projectRoot, options, context);
88-
89-
calculatedTargets[hash] = targets;
79+
targetsCache[hash] ??= buildExpoTargets(projectRoot, options, context);
9080

9181
return {
9282
projects: {
9383
[projectRoot]: {
94-
targets,
84+
targets: targetsCache[hash],
9585
},
9686
},
9787
};

packages/gradle/src/plugin/dependencies.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
getGradleReport,
1414
invalidateGradleReportCache,
1515
} from '../utils/get-gradle-report';
16-
import { calculatedTargets, writeTargetsToCache } from './nodes';
16+
import { writeTargetsToCache } from './nodes';
1717

1818
export const createDependencies: CreateDependencies = async (
1919
_,
@@ -58,7 +58,7 @@ export const createDependencies: CreateDependencies = async (
5858
gradleDependenciesEnd.name
5959
);
6060

61-
writeTargetsToCache(calculatedTargets);
61+
writeTargetsToCache();
6262
if (dependencies.length) {
6363
invalidateGradleReportCache();
6464
}

packages/gradle/src/plugin/nodes.ts

Lines changed: 84 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,26 @@ export interface GradlePluginOptions {
3434
}
3535

3636
const cachePath = join(projectGraphCacheDirectory, 'gradle.hash');
37-
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
38-
39-
export const calculatedTargets: Record<
37+
const targetsCache = readTargetsCache();
38+
type GradleTargets = Record<
4039
string,
4140
{
4241
name: string;
4342
targets: Record<string, TargetConfiguration>;
4443
metadata: ProjectConfiguration['metadata'];
4544
}
46-
> = {};
45+
>;
4746

48-
function readTargetsCache(): Record<
49-
string,
50-
{
51-
name: string;
52-
targets: Record<string, TargetConfiguration>;
53-
metadata: ProjectConfiguration['metadata'];
54-
}
55-
> {
56-
return readJsonFile(cachePath);
47+
function readTargetsCache(): GradleTargets {
48+
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
5749
}
5850

59-
export function writeTargetsToCache(
60-
targets: Record<
61-
string,
62-
{
63-
name: string;
64-
targets: Record<string, TargetConfiguration>;
65-
metadata: ProjectConfiguration['metadata'];
66-
}
67-
>
68-
) {
69-
writeJsonFile(cachePath, targets);
51+
export function writeTargetsToCache() {
52+
const oldCache = readTargetsCache();
53+
writeJsonFile(cachePath, {
54+
...oldCache,
55+
...targetsCache,
56+
});
7057
}
7158

7259
export const createNodes: CreateNodes<GradlePluginOptions> = [
@@ -83,76 +70,84 @@ export const createNodes: CreateNodes<GradlePluginOptions> = [
8370
options ?? {},
8471
context
8572
);
86-
if (targetsCache[hash]) {
87-
calculatedTargets[hash] = targetsCache[hash];
88-
return {
89-
projects: {
90-
[projectRoot]: targetsCache[hash],
91-
},
92-
};
93-
}
94-
95-
try {
96-
const {
97-
gradleProjectToTasksTypeMap,
98-
gradleFileToOutputDirsMap,
99-
gradleFileToGradleProjectMap,
100-
gradleProjectToProjectName,
101-
} = getGradleReport();
102-
103-
const gradleProject = gradleFileToGradleProjectMap.get(
104-
gradleFilePath
105-
) as string;
106-
const projectName = gradleProjectToProjectName.get(gradleProject);
107-
if (!projectName) {
108-
return;
109-
}
110-
111-
const tasksTypeMap = gradleProjectToTasksTypeMap.get(
112-
gradleProject
113-
) as Map<string, string>;
114-
let tasks: GradleTask[] = [];
115-
for (let [taskName, taskType] of tasksTypeMap.entries()) {
116-
tasks.push({
117-
type: taskType,
118-
name: taskName,
119-
});
120-
}
121-
122-
const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map<
123-
string,
124-
string
125-
>;
126-
127-
const { targets, targetGroups } = createGradleTargets(
128-
tasks,
129-
options,
130-
context,
131-
outputDirs,
132-
gradleProject
133-
);
134-
const project = {
135-
name: projectName,
136-
targets,
137-
metadata: {
138-
targetGroups,
139-
technologies: ['gradle'],
140-
},
141-
};
142-
calculatedTargets[hash] = project;
143-
144-
return {
145-
projects: {
146-
[projectRoot]: project,
147-
},
148-
};
149-
} catch (e) {
150-
console.error(e);
73+
targetsCache[hash] ??= createGradleProject(
74+
gradleFilePath,
75+
options,
76+
context
77+
);
78+
const project = targetsCache[hash];
79+
if (!project) {
15180
return {};
15281
}
82+
return {
83+
projects: {
84+
[projectRoot]: project,
85+
},
86+
};
15387
},
15488
];
15589

90+
function createGradleProject(
91+
gradleFilePath: string,
92+
options: GradlePluginOptions | undefined,
93+
context: CreateNodesContext
94+
) {
95+
try {
96+
const {
97+
gradleProjectToTasksTypeMap,
98+
gradleFileToOutputDirsMap,
99+
gradleFileToGradleProjectMap,
100+
gradleProjectToProjectName,
101+
} = getGradleReport();
102+
103+
const gradleProject = gradleFileToGradleProjectMap.get(
104+
gradleFilePath
105+
) as string;
106+
const projectName = gradleProjectToProjectName.get(gradleProject);
107+
if (!projectName) {
108+
return;
109+
}
110+
111+
const tasksTypeMap = gradleProjectToTasksTypeMap.get(gradleProject) as Map<
112+
string,
113+
string
114+
>;
115+
let tasks: GradleTask[] = [];
116+
for (let [taskName, taskType] of tasksTypeMap.entries()) {
117+
tasks.push({
118+
type: taskType,
119+
name: taskName,
120+
});
121+
}
122+
123+
const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map<
124+
string,
125+
string
126+
>;
127+
128+
const { targets, targetGroups } = createGradleTargets(
129+
tasks,
130+
options,
131+
context,
132+
outputDirs,
133+
gradleProject
134+
);
135+
const project = {
136+
name: projectName,
137+
targets,
138+
metadata: {
139+
targetGroups,
140+
technologies: ['gradle'],
141+
},
142+
};
143+
144+
return project;
145+
} catch (e) {
146+
console.error(e);
147+
return undefined;
148+
}
149+
}
150+
156151
function createGradleTargets(
157152
tasks: GradleTask[],
158153
options: GradlePluginOptions | undefined,

0 commit comments

Comments
 (0)