Skip to content

Commit

Permalink
cleanup(core): refactor plugin shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Mar 8, 2024
1 parent 6235e6a commit a17ef8b
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 169 deletions.
4 changes: 2 additions & 2 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { loadNxPluginsRemotely } from '../project-graph/plugins/internal-api';
import { loadNxPluginsInIsolation } from '../project-graph/plugins/internal-api';

describe('Workspaces', () => {
let fs: TempFs;
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('Workspaces', () => {
NX_WORKSPACE_ROOT_PATH: fs.tempDir,
},
async () => {
const [plugins, cleanup] = await loadNxPluginsRemotely(
const [plugins, cleanup] = await loadNxPluginsInIsolation(
readNxJson(fs.tempDir).plugins,
fs.tempDir
);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/daemon/server/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readNxJson } from '../../config/nx-json';
import {
RemotePlugin,
loadNxPluginsRemotely,
loadNxPluginsInIsolation,
} from '../../project-graph/plugins/internal-api';
import { workspaceRoot } from '../../utils/workspace-root';

Expand All @@ -13,7 +13,7 @@ export async function getPlugins() {
return loadedPlugins;
}
const pluginsConfiguration = readNxJson().plugins ?? [];
const [result, cleanupFn] = await loadNxPluginsRemotely(
const [result, cleanupFn] = await loadNxPluginsInIsolation(
pluginsConfiguration,
workspaceRoot
);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/executors/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readNxJson } from '../../config/nx-json';
import { Executor, ExecutorContext } from '../../config/misc-interfaces';
import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files';
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
import { loadNxPluginsRemotely } from '../../project-graph/plugins/internal-api';
import { loadNxPluginsInIsolation } from '../../project-graph/plugins/internal-api';

/**
* Convert an Nx Executor into an Angular Devkit Builder
Expand All @@ -19,7 +19,7 @@ export function convertNxExecutor(executor: Executor) {
const promise = async () => {
const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot);

const [plugins, cleanup] = await loadNxPluginsRemotely(
const [plugins, cleanup] = await loadNxPluginsInIsolation(
nxJsonConfiguration.plugins,
builderContext.workspaceRoot
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import { setupWorkspaceContext } from '../../../../utils/workspace-context';
import ProjectJsonProjectsPlugin from '../../../project-json/build-nodes/project-json';
import { loadNxPluginsRemotely } from '../../../../project-graph/plugins/internal-api';
import { loadNxPluginsInIsolation } from '../../../../project-graph/plugins/internal-api';

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

setupWorkspaceContext(tempFs.tempDir);

const [plugins, cleanup] = await loadNxPluginsRemotely([], tempFs.tempDir);
const [plugins, cleanup] = await loadNxPluginsInIsolation([], tempFs.tempDir);
const { projects, projectRootMap } = await retrieveProjectConfigurations(
plugins,
tempFs.tempDir,
Expand Down
83 changes: 6 additions & 77 deletions packages/nx/src/project-graph/plugins/internal-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import { loadRemoteNxPlugin } from './plugin-pool';
import {
CreateNodesContext,
CreateNodesResult,
NxPlugin,
NxPluginV2,
} from './public-api';
import { ProjectConfiguration } from '../../config/workspace-json-project-json';
import { retrieveProjectConfigurationsWithoutPluginInference } from '../utils/retrieve-workspace-files';
import { getPluginPathAndName } from './worker-api';
import { getNxRequirePaths } from '../../utils/installation-directory';
import { normalizeNxPlugin } from './utils';

export { loadPlugins, loadPlugin } from './worker-api';

export type CreateNodesResultWithContext = CreateNodesResult & {
file: string;
Expand Down Expand Up @@ -52,7 +48,10 @@ export type RemotePlugin =
export const nxPluginCache: Map<unknown, [Promise<RemotePlugin>, () => void]> =
new Map();

export async function loadNxPluginsRemotely(
/**
* This loads plugins in isolation in their own worker so that they do not disturb other workers or the main process.
*/
export async function loadNxPluginsInIsolation(
plugins: PluginConfiguration[],
root = workspaceRoot
): Promise<[RemotePlugin[], () => void]> {
Expand Down Expand Up @@ -113,73 +112,3 @@ export async function getDefaultPlugins(root: string) {
join(__dirname, '../../plugins/project-json/build-nodes/project-json'),
];
}

let projectsWithoutInference: Record<string, ProjectConfiguration>;

export async function loadPlugins(
plugins: PluginConfiguration[],
root: string
): Promise<LoadedNxPlugin[]> {
return await Promise.all(plugins.map((p) => loadPlugin(p, root)));
}

export async function loadPlugin(plugin: PluginConfiguration, root: string) {
try {
require.resolve(typeof plugin === 'string' ? plugin : plugin.plugin);
} catch {
// If a plugin cannot be resolved, we will need projects to resolve it
projectsWithoutInference ??=
await retrieveProjectConfigurationsWithoutPluginInference(root);
}
return await loadNxPluginAsync(
plugin,
getNxRequirePaths(root),
projectsWithoutInference,
root
);
}

export type LoadedNxPlugin = {
plugin: NxPlugin;
options?: unknown;
};

export async function loadNxPluginAsync(
pluginConfiguration: PluginConfiguration,
paths: string[],
projects: Record<string, ProjectConfiguration>,
root: string
): Promise<LoadedNxPlugin> {
const { plugin: moduleName, options } =
typeof pluginConfiguration === 'object'
? pluginConfiguration
: { plugin: pluginConfiguration, options: undefined };

performance.mark(`Load Nx Plugin: ${moduleName} - start`);
let { pluginPath, name } = await getPluginPathAndName(
moduleName,
paths,
projects,
root
);
const plugin = normalizeNxPlugin(await importPluginModule(pluginPath));
plugin.name ??= name;
performance.mark(`Load Nx Plugin: ${moduleName} - end`);
performance.measure(
`Load Nx Plugin: ${moduleName}`,
`Load Nx Plugin: ${moduleName} - start`,
`Load Nx Plugin: ${moduleName} - end`
);
return { plugin, options };
}

async function importPluginModule(pluginPath: string): Promise<NxPlugin> {
const m = await import(pluginPath);
if (
m.default &&
('createNodes' in m.default || 'createDependencies' in m.default)
) {
return m.default;
}
return m;
}
Loading

0 comments on commit a17ef8b

Please sign in to comment.