Skip to content

Commit 38da368

Browse files
AgentEnderFrozenPandaz
authored andcommitted
fix(core): isolated plugins should provide cleanup function (#26657)
<!-- 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 - Isolated plugins return no-op cleanup function. ## Expected Behavior - Isolated plugins cleanup fn shuts them down. ## Related Issue(s) In #26625 we are starting to reload plugins when their configurations change. This makes sense, but means we should actually shutdown + reload workers. Fixes # (cherry picked from commit 3b2c42a)
1 parent 3e59e98 commit 38da368

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

packages/nx/src/project-graph/plugins/isolation/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,30 @@ import { loadRemoteNxPlugin } from './plugin-pool';
66
/**
77
* Used to ensure 1 plugin : 1 worker
88
*/
9-
const remotePluginCache = new Map<string, Promise<LoadedNxPlugin>>();
9+
const remotePluginCache = new Map<
10+
string,
11+
readonly [Promise<LoadedNxPlugin>, () => void]
12+
>();
1013

1114
export function loadNxPluginInIsolation(
1215
plugin: PluginConfiguration,
1316
root = workspaceRoot
14-
): [Promise<LoadedNxPlugin>, () => void] {
17+
): readonly [Promise<LoadedNxPlugin>, () => void] {
1518
const cacheKey = JSON.stringify(plugin);
1619

1720
if (remotePluginCache.has(cacheKey)) {
18-
return [remotePluginCache.get(cacheKey), () => {}];
21+
return remotePluginCache.get(cacheKey);
1922
}
2023

21-
const loadingPlugin = loadRemoteNxPlugin(plugin, root);
22-
remotePluginCache.set(cacheKey, loadingPlugin);
24+
const [loadingPlugin, cleanup] = loadRemoteNxPlugin(plugin, root);
2325
// We clean up plugin workers when Nx process completes.
24-
return [loadingPlugin, () => {}];
26+
const val = [
27+
loadingPlugin,
28+
() => {
29+
cleanup();
30+
remotePluginCache.delete(cacheKey);
31+
},
32+
] as const;
33+
remotePluginCache.set(cacheKey, val);
34+
return val;
2535
}

packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface PendingPromise {
2222
export function loadRemoteNxPlugin(
2323
plugin: PluginConfiguration,
2424
root: string
25-
): Promise<LoadedNxPlugin> {
25+
): [Promise<LoadedNxPlugin>, () => void] {
2626
// this should only really be true when running unit tests within
2727
// the Nx repo. We still need to start the worker in this case,
2828
// but its typescript.
@@ -66,13 +66,19 @@ export function loadRemoteNxPlugin(
6666

6767
cleanupFunctions.add(cleanupFunction);
6868

69-
return new Promise<LoadedNxPlugin>((res, rej) => {
70-
worker.on(
71-
'message',
72-
createWorkerHandler(worker, pendingPromises, res, rej)
73-
);
74-
worker.on('exit', exitHandler);
75-
});
69+
return [
70+
new Promise<LoadedNxPlugin>((res, rej) => {
71+
worker.on(
72+
'message',
73+
createWorkerHandler(worker, pendingPromises, res, rej)
74+
);
75+
worker.on('exit', exitHandler);
76+
}),
77+
() => {
78+
cleanupFunction();
79+
cleanupFunctions.delete(cleanupFunction);
80+
},
81+
];
7682
}
7783

7884
function shutdownPluginWorker(worker: ChildProcess) {

packages/nx/src/project-graph/project-graph.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ export async function buildProjectGraphAndSourceMapsWithoutDaemon() {
148148
throw e;
149149
}
150150
} finally {
151-
cleanup();
151+
// When plugins are isolated we don't clean them up during
152+
// a single run of the CLI. They are cleaned up when the CLI
153+
// process exits. Cleaning them here could cause issues if pending
154+
// promises are not resolved.
155+
if (process.env.NX_ISOLATE_PLUGINS !== 'true') {
156+
cleanup();
157+
}
152158
}
153159

154160
const { projectGraph, projectFileMapCache } = projectGraphResult;

0 commit comments

Comments
 (0)