Skip to content

Commit 97fa7f1

Browse files
authored
feat(core): allow disabling registered task sync generators (#27638)
<!-- 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` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> There's no way to disable sync generators registered in inferred tasks by a Crystal plugin. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> There should be a way to disable sync generators registered in inferred tasks by a Crystal plugin. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> <!-- Fixes NXC-904 --> Fixes #
1 parent a0dc0f1 commit 97fa7f1

File tree

6 files changed

+99
-24
lines changed

6 files changed

+99
-24
lines changed

packages/nx/schemas/nx-schema.json

+7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@
282282
"applyChanges": {
283283
"type": "boolean",
284284
"description": "Whether to automatically apply sync generator changes when running tasks. If not set, the user will be prompted. If set to `true`, the user will not be prompted and the changes will be applied. If set to `false`, the user will not be prompted and the changes will not be applied."
285+
},
286+
"disabledTaskSyncGenerators": {
287+
"type": "array",
288+
"items": {
289+
"type": "string"
290+
},
291+
"description": "List of registered task sync generators to disable."
285292
}
286293
},
287294
"additionalProperties": false

packages/nx/src/command-line/sync/sync.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ora from 'ora';
2+
import { readNxJson } from '../../config/nx-json';
23
import { createProjectGraphAsync } from '../../project-graph/project-graph';
34
import { output } from '../../utils/output';
45
import { handleErrors } from '../../utils/params';
@@ -18,9 +19,22 @@ interface SyncOptions extends SyncArgs {
1819
export function syncHandler(options: SyncOptions): Promise<number> {
1920
return handleErrors(options.verbose, async () => {
2021
const projectGraph = await createProjectGraphAsync();
22+
const nxJson = readNxJson();
2123
const syncGenerators = await collectAllRegisteredSyncGenerators(
22-
projectGraph
24+
projectGraph,
25+
nxJson
2326
);
27+
28+
if (!syncGenerators.length) {
29+
output.success({
30+
title: options.check
31+
? 'The workspace is up to date'
32+
: 'The workspace is already up to date',
33+
bodyLines: ['There are no sync generators to run.'],
34+
});
35+
return 0;
36+
}
37+
2438
const results = await getSyncGeneratorChanges(syncGenerators);
2539

2640
if (!results.length) {

packages/nx/src/config/nx-json.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,16 @@ export interface NxSyncConfiguration {
321321

322322
/**
323323
* Whether to automatically apply sync generator changes when running tasks.
324-
* If not set, the user will be prompted.
324+
* If not set, the user will be prompted in interactive mode.
325325
* If set to `true`, the user will not be prompted and the changes will be applied.
326326
* If set to `false`, the user will not be prompted and the changes will not be applied.
327327
*/
328328
applyChanges?: boolean;
329+
330+
/**
331+
* List of registered task sync generators to disable.
332+
*/
333+
disabledTaskSyncGenerators?: string[];
329334
}
330335

331336
/**

packages/nx/src/daemon/server/sync-generators.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { FsTree } from '../../generators/tree';
55
import { hashArray } from '../../hasher/file-hasher';
66
import { readProjectsConfigurationFromProjectGraph } from '../../project-graph/project-graph';
77
import {
8+
collectEnabledTaskSyncGeneratorsFromProjectGraph,
89
collectRegisteredGlobalSyncGenerators,
9-
collectRegisteredTaskSyncGenerators,
1010
flushSyncGeneratorChanges,
1111
runSyncGenerator,
1212
type SyncGeneratorChangesResult,
@@ -28,6 +28,7 @@ let registeredSyncGenerators: Set<string> | undefined;
2828
let scheduledTimeoutId: NodeJS.Timeout | undefined;
2929
let storedProjectGraphHash: string | undefined;
3030
let storedNxJsonHash: string | undefined;
31+
let storedDisabledTaskSyncGeneratorsHash: string | undefined;
3132

3233
const log = (...messageParts: unknown[]) => {
3334
serverLogger.log('[SYNC]:', ...messageParts);
@@ -146,6 +147,12 @@ export function collectAndScheduleSyncGenerators(
146147
// a change imply we need to re-run all the generators
147148
// make sure to schedule all the collected generators
148149
scheduledGenerators.clear();
150+
151+
if (!registeredSyncGenerators.size) {
152+
// there are no generators to run
153+
return;
154+
}
155+
149156
for (const generator of registeredSyncGenerators) {
150157
scheduledGenerators.add(generator);
151158
}
@@ -193,16 +200,23 @@ export async function getCachedRegisteredSyncGenerators(): Promise<string[]> {
193200
}
194201

195202
function collectAllRegisteredSyncGenerators(projectGraph: ProjectGraph): void {
203+
const nxJson = readNxJson();
196204
const projectGraphHash = hashProjectGraph(projectGraph);
197-
if (storedProjectGraphHash !== projectGraphHash) {
205+
const disabledTaskSyncGeneratorsHash = hashArray(
206+
nxJson.sync?.disabledTaskSyncGenerators?.sort() ?? []
207+
);
208+
if (
209+
projectGraphHash !== storedProjectGraphHash ||
210+
disabledTaskSyncGeneratorsHash !== storedDisabledTaskSyncGeneratorsHash
211+
) {
198212
storedProjectGraphHash = projectGraphHash;
213+
storedDisabledTaskSyncGeneratorsHash = disabledTaskSyncGeneratorsHash;
199214
registeredTaskSyncGenerators =
200-
collectRegisteredTaskSyncGenerators(projectGraph);
215+
collectEnabledTaskSyncGeneratorsFromProjectGraph(projectGraph, nxJson);
201216
} else {
202217
log('project graph hash is the same, not collecting task sync generators');
203218
}
204219

205-
const nxJson = readNxJson();
206220
const nxJsonHash = hashArray(nxJson.sync?.globalGenerators?.sort() ?? []);
207221
if (storedNxJsonHash !== nxJsonHash) {
208222
storedNxJsonHash = nxJsonHash;

packages/nx/src/tasks-runner/run-command.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { isNxCloudUsed } from '../utils/nx-cloud-utils';
2121
import { output } from '../utils/output';
2222
import { handleErrors } from '../utils/params';
2323
import {
24+
collectEnabledTaskSyncGeneratorsFromTaskGraph,
2425
flushSyncGeneratorChanges,
2526
getSyncGeneratorChanges,
2627
syncGeneratorResultsToMessageLines,
@@ -233,18 +234,11 @@ async function ensureWorkspaceIsInSyncAndGetGraphs(
233234
);
234235

235236
// collect unique syncGenerators from the tasks
236-
const uniqueSyncGenerators = new Set<string>();
237-
for (const { target } of Object.values(taskGraph.tasks)) {
238-
const { syncGenerators } =
239-
projectGraph.nodes[target.project].data.targets[target.target];
240-
if (!syncGenerators) {
241-
continue;
242-
}
243-
244-
for (const generator of syncGenerators) {
245-
uniqueSyncGenerators.add(generator);
246-
}
247-
}
237+
const uniqueSyncGenerators = collectEnabledTaskSyncGeneratorsFromTaskGraph(
238+
taskGraph,
239+
projectGraph,
240+
nxJson
241+
);
248242

249243
if (!uniqueSyncGenerators.size) {
250244
// There are no sync generators registered in the tasks to run

packages/nx/src/utils/sync-generators.ts

+47-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { performance } from 'perf_hooks';
22
import { parseGeneratorString } from '../command-line/generate/generate';
33
import { getGeneratorInformation } from '../command-line/generate/generator-utils';
44
import type { GeneratorCallback } from '../config/misc-interfaces';
5-
import { readNxJson } from '../config/nx-json';
5+
import { readNxJson, type NxJsonConfiguration } from '../config/nx-json';
66
import type { ProjectGraph } from '../config/project-graph';
7+
import type { TaskGraph } from '../config/task-graph';
78
import type { ProjectConfiguration } from '../config/workspace-json-project-json';
89
import { daemonClient } from '../daemon/client/client';
910
import { isOnDaemon } from '../daemon/is-on-daemon';
@@ -72,11 +73,12 @@ export async function flushSyncGeneratorChanges(
7273
}
7374

7475
export async function collectAllRegisteredSyncGenerators(
75-
projectGraph: ProjectGraph
76+
projectGraph: ProjectGraph,
77+
nxJson: NxJsonConfiguration
7678
): Promise<string[]> {
7779
if (!daemonClient.enabled()) {
7880
return [
79-
...collectRegisteredTaskSyncGenerators(projectGraph),
81+
...collectEnabledTaskSyncGeneratorsFromProjectGraph(projectGraph, nxJson),
8082
...collectRegisteredGlobalSyncGenerators(),
8183
];
8284
}
@@ -122,10 +124,14 @@ export async function runSyncGenerator(
122124
};
123125
}
124126

125-
export function collectRegisteredTaskSyncGenerators(
126-
projectGraph: ProjectGraph
127+
export function collectEnabledTaskSyncGeneratorsFromProjectGraph(
128+
projectGraph: ProjectGraph,
129+
nxJson: NxJsonConfiguration
127130
): Set<string> {
128131
const taskSyncGenerators = new Set<string>();
132+
const disabledTaskSyncGenerators = new Set(
133+
nxJson.sync?.disabledTaskSyncGenerators ?? []
134+
);
129135

130136
for (const {
131137
data: { targets },
@@ -135,11 +141,46 @@ export function collectRegisteredTaskSyncGenerators(
135141
}
136142

137143
for (const target of Object.values(targets)) {
138-
if (!target.syncGenerators) {
144+
if (!target.syncGenerators?.length) {
139145
continue;
140146
}
141147

142148
for (const generator of target.syncGenerators) {
149+
if (
150+
!disabledTaskSyncGenerators.has(generator) &&
151+
!taskSyncGenerators.has(generator)
152+
) {
153+
taskSyncGenerators.add(generator);
154+
}
155+
}
156+
}
157+
}
158+
159+
return taskSyncGenerators;
160+
}
161+
162+
export function collectEnabledTaskSyncGeneratorsFromTaskGraph(
163+
taskGraph: TaskGraph,
164+
projectGraph: ProjectGraph,
165+
nxJson: NxJsonConfiguration
166+
): Set<string> {
167+
const taskSyncGenerators = new Set<string>();
168+
const disabledTaskSyncGenerators = new Set(
169+
nxJson.sync?.disabledTaskSyncGenerators ?? []
170+
);
171+
172+
for (const { target } of Object.values(taskGraph.tasks)) {
173+
const { syncGenerators } =
174+
projectGraph.nodes[target.project].data.targets[target.target];
175+
if (!syncGenerators?.length) {
176+
continue;
177+
}
178+
179+
for (const generator of syncGenerators) {
180+
if (
181+
!disabledTaskSyncGenerators.has(generator) &&
182+
!taskSyncGenerators.has(generator)
183+
) {
143184
taskSyncGenerators.add(generator);
144185
}
145186
}

0 commit comments

Comments
 (0)