Skip to content

Commit

Permalink
fix(core): avoid launching default plugins twice (#29539)
Browse files Browse the repository at this point in the history
<!-- 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
Default plugins are launched twice when loading plugins in a workspace
that has local plugins:
- Once to resolve the local plugin
- Once to be used as an actual plugin

## Expected Behavior
Default plugins are launched once and reused

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: FrozenPandaz <[email protected]>
(cherry picked from commit 0edd110)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Jan 15, 2025
1 parent 206a47e commit 843c06f
Show file tree
Hide file tree
Showing 24 changed files with 540 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
parseTargetString,
} from '@nx/devkit';
import { addE2eCiTargetDefaults as _addE2eCiTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import { LoadedNxPlugin } from 'nx/src/project-graph/plugins/internal-api';
import { LoadedNxPlugin } from 'nx/src/project-graph/plugins/loaded-nx-plugin';
import type { ConfigurationResult } from 'nx/src/project-graph/utils/project-configuration-utils';
import {
ProjectConfigurationsError,
Expand Down
12 changes: 6 additions & 6 deletions packages/nx/src/config/to-project-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { TempFs } from '../internal-testing-utils/temp-fs';
import { withEnvironmentVariables } from '../internal-testing-utils/with-environment';
import { retrieveProjectConfigurations } from '../project-graph/utils/retrieve-workspace-files';
import { readNxJson } from './configuration';
import { loadNxPlugins } from '../project-graph/plugins/internal-api';
import {
cleanupPlugins,
getPlugins,
} from '../project-graph/plugins/get-plugins';

describe('Workspaces', () => {
let fs: TempFs;
Expand Down Expand Up @@ -40,16 +43,13 @@ describe('Workspaces', () => {
NX_WORKSPACE_ROOT_PATH: fs.tempDir,
},
async () => {
const [plugins, cleanup] = await loadNxPlugins(
readNxJson(fs.tempDir).plugins,
fs.tempDir
);
const plugins = await getPlugins(fs.tempDir);
const res = await retrieveProjectConfigurations(
plugins,
fs.tempDir,
readNxJson(fs.tempDir)
);
cleanup();
cleanupPlugins();
return res;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { notifyFileWatcherSockets } from './file-watching/file-watcher-sockets';
import { serverLogger } from './logger';
import { NxWorkspaceFilesExternals } from '../../native';
import { ConfigurationResult } from '../../project-graph/utils/project-configuration-utils';
import { LoadedNxPlugin } from '../../project-graph/plugins/internal-api';
import type { LoadedNxPlugin } from '../../project-graph/plugins/loaded-nx-plugin';
import {
DaemonProjectGraphError,
ProjectConfigurationsError,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {
findProjectForPath,
} from './project-graph/utils/find-project-for-path';
export { retrieveProjectConfigurations } from './project-graph/utils/retrieve-workspace-files';
export { LoadedNxPlugin } from './project-graph/plugins/internal-api';
export { LoadedNxPlugin } from './project-graph/plugins/loaded-nx-plugin';
export * from './project-graph/error-types';
export { registerTsProject } from './plugins/js/utils/register';
export { interpolate } from './tasks-runner/utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const tempFs = new TempFs('explicit-project-deps');
import { ProjectGraphProjectNode } from '../../../../config/project-graph';
import { ProjectConfiguration } from '../../../../config/workspace-json-project-json';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import { loadNxPlugins } from '../../../../project-graph/plugins/internal-api';
import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
import {
retrieveProjectConfigurations,
Expand All @@ -14,6 +13,10 @@ import {
import { setupWorkspaceContext } from '../../../../utils/workspace-context';
import { buildExplicitTypeScriptDependencies } from './explicit-project-dependencies';
import { TargetProjectLocator } from './target-project-locator';
import {
cleanupPlugins,
getOnlyDefaultPlugins,
} from '../../../../project-graph/plugins/get-plugins';

// projectName => tsconfig import path
const dependencyProjectNamesToImportPaths = {
Expand Down Expand Up @@ -698,13 +701,13 @@ async function createContext(

setupWorkspaceContext(tempFs.tempDir);

const [plugins, cleanup] = await loadNxPlugins([], tempFs.tempDir);
const plugins = await getOnlyDefaultPlugins(tempFs.tempDir);
const { projects, projectRootMap } = await retrieveProjectConfigurations(
plugins,
tempFs.tempDir,
nxJson
);
cleanup();
cleanupPlugins();

const { fileMap } = await retrieveWorkspaceFiles(
tempFs.tempDir,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './nx-deps-cache';
import { applyImplicitDependencies } from './utils/implicit-project-dependencies';
import { normalizeProjectNodes } from './utils/normalize-project-nodes';
import { LoadedNxPlugin } from './plugins/internal-api';
import type { LoadedNxPlugin } from './plugins/loaded-nx-plugin';
import {
CreateDependenciesContext,
CreateMetadataContext,
Expand Down
181 changes: 168 additions & 13 deletions packages/nx/src/project-graph/plugins/get-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { join } from 'node:path';

import { shouldMergeAngularProjects } from '../../adapter/angular-json';
import { PluginConfiguration, readNxJson } from '../../config/nx-json';
import { hashObject } from '../../hasher/file-hasher';
import { readNxJson } from '../../config/nx-json';
import { LoadedNxPlugin, loadNxPlugins } from './internal-api';
import { IS_WASM } from '../../native';
import { workspaceRoot } from '../../utils/workspace-root';
import { loadNxPluginInIsolation } from './isolation';
import { loadNxPlugin } from './in-process-loader';

import type { LoadedNxPlugin } from './loaded-nx-plugin';
import {
cleanupPluginTSTranspiler,
pluginTranspilerIsRegistered,
} from './transpiler';

/**
* Stuff for specified NX Plugins.
*/
let currentPluginsConfigurationHash: string;
let loadedPlugins: LoadedNxPlugin[];
let pendingPluginsPromise:
| Promise<readonly [LoadedNxPlugin[], () => void]>
| undefined;
let cleanup: () => void;
let cleanup: () => void | undefined;

export async function getPlugins() {
const pluginsConfiguration = readNxJson().plugins ?? [];
export async function getPlugins(
root = workspaceRoot
): Promise<LoadedNxPlugin[]> {
const pluginsConfiguration = readNxJson(root).plugins ?? [];
const pluginsConfigurationHash = hashObject(pluginsConfiguration);

// If the plugins configuration has not changed, reuse the current plugins
Expand All @@ -28,22 +44,29 @@ export async function getPlugins() {
cleanup();
}

pendingPluginsPromise ??= loadNxPlugins(pluginsConfiguration, workspaceRoot);
pendingPluginsPromise ??= loadSpecifiedNxPlugins(pluginsConfiguration, root);

currentPluginsConfigurationHash = pluginsConfigurationHash;
const [result, cleanupFn] = await pendingPluginsPromise;
const [[result, cleanupFn], defaultPlugins] = await Promise.all([
pendingPluginsPromise,
getOnlyDefaultPlugins(root),
]);
cleanup = cleanupFn;
loadedPlugins = result;
return result;
loadedPlugins = result.concat(defaultPlugins);
return loadedPlugins;
}

/**
* Stuff for default NX Plugins.
*/

let loadedDefaultPlugins: LoadedNxPlugin[];
let cleanupDefaultPlugins: () => void;
let pendingDefaultPluginPromise:
| Promise<readonly [LoadedNxPlugin[], () => void]>
| undefined;

export async function getOnlyDefaultPlugins() {
export async function getOnlyDefaultPlugins(root = workspaceRoot) {
// If the plugins configuration has not changed, reuse the current plugins
if (loadedDefaultPlugins) {
return loadedPlugins;
Expand All @@ -55,7 +78,7 @@ export async function getOnlyDefaultPlugins() {
cleanupDefaultPlugins();
}

pendingDefaultPluginPromise ??= loadNxPlugins([], workspaceRoot);
pendingDefaultPluginPromise ??= loadDefaultNxPlugins(workspaceRoot);

const [result, cleanupFn] = await pendingDefaultPluginPromise;
cleanupDefaultPlugins = cleanupFn;
Expand All @@ -66,6 +89,138 @@ export async function getOnlyDefaultPlugins() {
export function cleanupPlugins() {
pendingPluginsPromise = undefined;
pendingDefaultPluginPromise = undefined;
cleanup();
cleanupDefaultPlugins();
cleanup?.();
cleanupDefaultPlugins?.();
}

/**
* Stuff for generic loading
*/

function isIsolationEnabled() {
// Explicitly enabled, regardless of further conditions
if (process.env.NX_ISOLATE_PLUGINS === 'true') {
return true;
}
if (
// Explicitly disabled
process.env.NX_ISOLATE_PLUGINS === 'false' ||
// Isolation is disabled on WASM builds currently.
IS_WASM
) {
return false;
}
// Default value
return true;
}

const loadingMethod = isIsolationEnabled()
? loadNxPluginInIsolation
: loadNxPlugin;

async function loadDefaultNxPlugins(root = workspaceRoot) {
performance.mark('loadDefaultNxPlugins:start');

const plugins = getDefaultPlugins(root);

const cleanupFunctions: Array<() => void> = [];
const ret = [
await Promise.all(
plugins.map(async (plugin) => {
performance.mark(`Load Nx Plugin: ${plugin} - start`);

const [loadedPluginPromise, cleanup] = await loadingMethod(
plugin,
root
);

cleanupFunctions.push(cleanup);
const res = await loadedPluginPromise;
performance.mark(`Load Nx Plugin: ${plugin} - end`);
performance.measure(
`Load Nx Plugin: ${plugin}`,
`Load Nx Plugin: ${plugin} - start`,
`Load Nx Plugin: ${plugin} - end`
);

return res;
})
),
() => {
for (const fn of cleanupFunctions) {
fn();
}
if (pluginTranspilerIsRegistered()) {
cleanupPluginTSTranspiler();
}
},
] as const;
performance.mark('loadDefaultNxPlugins:end');
performance.measure(
'loadDefaultNxPlugins',
'loadDefaultNxPlugins:start',
'loadDefaultNxPlugins:end'
);
return ret;
}

async function loadSpecifiedNxPlugins(
plugins: PluginConfiguration[],
root = workspaceRoot
): Promise<readonly [LoadedNxPlugin[], () => void]> {
performance.mark('loadSpecifiedNxPlugins:start');

plugins ??= [];

const cleanupFunctions: Array<() => void> = [];
const ret = [
await Promise.all(
plugins.map(async (plugin) => {
const pluginPath = typeof plugin === 'string' ? plugin : plugin.plugin;
performance.mark(`Load Nx Plugin: ${pluginPath} - start`);

const [loadedPluginPromise, cleanup] = await loadingMethod(
plugin,
root
);

cleanupFunctions.push(cleanup);
const res = await loadedPluginPromise;
performance.mark(`Load Nx Plugin: ${pluginPath} - end`);
performance.measure(
`Load Nx Plugin: ${pluginPath}`,
`Load Nx Plugin: ${pluginPath} - start`,
`Load Nx Plugin: ${pluginPath} - end`
);

return res;
})
),
() => {
for (const fn of cleanupFunctions) {
fn();
}
if (pluginTranspilerIsRegistered()) {
cleanupPluginTSTranspiler();
}
},
] as const;
performance.mark('loadSpecifiedNxPlugins:end');
performance.measure(
'loadSpecifiedNxPlugins',
'loadSpecifiedNxPlugins:start',
'loadSpecifiedNxPlugins:end'
);
return ret;
}

function getDefaultPlugins(root: string) {
return [
join(__dirname, '../../plugins/js'),
...(shouldMergeAngularProjects(root, false)
? [join(__dirname, '../../adapter/angular-json')]
: []),
join(__dirname, '../../plugins/package-json'),
join(__dirname, '../../plugins/project-json/build-nodes/project-json'),
];
}
Loading

0 comments on commit 843c06f

Please sign in to comment.