Skip to content

Commit

Permalink
feat(core): execute plugins in isolated processes
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Feb 20, 2024
1 parent ca3965f commit 57cc74c
Show file tree
Hide file tree
Showing 54 changed files with 1,223 additions and 759 deletions.
17 changes: 9 additions & 8 deletions docs/generated/devkit/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#### Type declaration

| Name | Type |
| :------ | :-------------------------- |
| `debug` | (...`s`: `any`[]) => `void` |
| `error` | (`s`: `any`) => `void` |
| `fatal` | (...`s`: `any`[]) => `void` |
| `info` | (`s`: `any`) => `void` |
| `log` | (...`s`: `any`[]) => `void` |
| `warn` | (`s`: `any`) => `void` |
| Name | Type |
| :-------- | :-------------------------- |
| `debug` | (...`s`: `any`[]) => `void` |
| `error` | (`s`: `any`) => `void` |
| `fatal` | (...`s`: `any`[]) => `void` |
| `info` | (`s`: `any`) => `void` |
| `log` | (...`s`: `any`[]) => `void` |
| `verbose` | (...`s`: `any`[]) => `void` |
| `warn` | (`s`: `any`) => `void` |
2 changes: 2 additions & 0 deletions packages/devkit/src/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
Workspaces,
readNxJsonFromDisk,
retrieveProjectConfigurationsWithAngularProjects,
shutdownPluginWorkers,
} = requireNx();

/**
Expand Down Expand Up @@ -38,6 +39,7 @@ export function convertNxExecutor(executor: Executor) {
(workspaces as any).readProjectsConfigurations({
_includeProjectsFromAngularJson: true,
});
shutdownPluginWorkers?.();

const context: ExecutorContext = {
root: builderContext.workspaceRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/plugins/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NxPluginV2 } from '../src/utils/nx-plugin';
import type { NxPluginV2 } from '../src/project-graph/plugins';
import { workspaceRoot } from '../src/utils/workspace-root';
import { createNodeFromPackageJson } from '../src/plugins/package-json-workspaces';

Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/adapter/angular-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import * as path from 'path';
import { readJsonFile } from '../utils/fileutils';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import { NxPluginV2 } from '../utils/nx-plugin';
import { NxPluginV2 } from '../project-graph/plugins';

export const NX_ANGULAR_JSON_PLUGIN_NAME = 'nx-angular-json-plugin';

Expand All @@ -16,6 +16,8 @@ export const NxAngularJsonPlugin: NxPluginV2 = {
],
};

export default NxAngularJsonPlugin;

export function shouldMergeAngularProjects(
root: string,
includeProjectsFromAngularJson: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
ExecutorsJson,
TaskGraphExecutor,
} from '../config/misc-interfaces';
import { readPluginPackageJson } from '../utils/nx-plugin';
import { readPluginPackageJson } from '../project-graph/plugins';
import {
getImplementationFactory,
resolveImplementation,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/generate/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
resolveSchema,
} from '../../config/schema-utils';
import { readJsonFile } from '../../utils/fileutils';
import { readPluginPackageJson } from '../../utils/nx-plugin';
import { readPluginPackageJson } from '../../project-graph/plugins';

export function getGeneratorInformation(
collectionName: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/run/executor-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, join } from 'path';

import { readPluginPackageJson } from '../../utils/nx-plugin';
import { readPluginPackageJson } from '../../project-graph/plugins';
import {
CustomHasher,
Executor,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/config/schema-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'fs';
import { extname, join } from 'path';
import { registerPluginTSTranspiler } from '../utils/nx-plugin';
import { registerPluginTSTranspiler } from '../project-graph/plugins/load-plugin';

/**
* This function is used to get the implementation factory of an executor or generator.
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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 { shutdownPluginWorkers } from '../project-graph/plugins/plugin-pool';

const libConfig = (root, name?: string) => ({
name: name ?? toProjectName(`${root}/some-file`),
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('Workspaces', () => {

const { projects } = await withEnvironmentVariables(
{
NX_WORKSPACE_ROOT: fs.tempDir,
NX_WORKSPACE_ROOT_PATH: fs.tempDir,
},
() => retrieveProjectConfigurations(fs.tempDir, readNxJson(fs.tempDir))
);
Expand All @@ -65,6 +66,7 @@ describe('Workspaces', () => {
},
},
});
await shutdownPluginWorkers();
});
});
});
9 changes: 6 additions & 3 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ export { workspaceLayout } from './config/configuration';

export type {
NxPlugin,
NxPluginV1,
NxPluginV2,
ProjectTargetConfigurator,
CreateNodes,
CreateNodesFunction,
CreateNodesResult,
CreateNodesContext,
CreateDependencies,
CreateDependenciesContext,
} from './utils/nx-plugin';
} from './project-graph/plugins';

export type {
NxPluginV1,
ProjectTargetConfigurator,
} from './utils/nx-plugin.deprecated';

/**
* @category Workspace
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { shutdownPluginWorkers } from './project-graph/plugins/plugin-pool';

/**
* Note to developers: STOP! These exports are available via requireNx in @nx/devkit.
*
Expand All @@ -21,3 +23,4 @@ export {
findProjectForPath,
} from './project-graph/utils/find-project-for-path';
export { registerTsProject } from './plugins/js/utils/register';
export { shutdownPluginWorkers } from './project-graph/plugins/plugin-pool';
6 changes: 3 additions & 3 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { basename, join, relative } from 'path';
import {
buildProjectConfigurationFromPackageJson,
getGlobPatternsFromPackageManagerWorkspaces,
getNxPackageJsonWorkspacesPlugin,
createNodes as packageJsonWorkspacesCreateNodes,
} from '../../plugins/package-json-workspaces';
import {
buildProjectFromProjectJson,
Expand Down Expand Up @@ -196,8 +196,8 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
),
];
const projectGlobPatterns = configurationGlobs([
{ plugin: ProjectJsonProjectsPlugin },
{ plugin: getNxPackageJsonWorkspacesPlugin(tree.root) },
ProjectJsonProjectsPlugin,
{ createNodes: packageJsonWorkspacesCreateNodes },
]);
const globbedFiles = globWithWorkspaceContext(tree.root, projectGlobPatterns);
const createdFiles = findCreatedProjectFiles(tree, patterns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dirname } from 'path';
import { readJson, writeJson } from '../../generators/utils/json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { retrieveProjectConfigurationPaths } from '../../project-graph/utils/retrieve-workspace-files';
import { loadNxPlugins } from '../../utils/nx-plugin';
import { loadNxPlugins } from '../../project-graph/plugins';

export default async function (tree: Tree) {
const nxJson = readNxJson(tree);
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CreateDependencies,
CreateDependenciesContext,
CreateNodes,
} from '../../utils/nx-plugin';
} from '../../project-graph/plugins';
import {
getLockFileDependencies,
getLockFileName,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
import { pruneProjectGraph } from './project-graph-pruning';
import { normalizePackageJson } from './utils/package-json';
import { readJsonFile } from '../../../utils/fileutils';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

const YARN_LOCK_FILE = 'yarn.lock';
const NPM_LOCK_FILE = 'package-lock.json';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/npm-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { pruneProjectGraph } from './project-graph-pruning';
import { vol } from 'memfs';
import { ProjectGraph } from '../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

jest.mock('fs', () => {
const memFs = require('memfs').fs;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/npm-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ProjectGraphExternalNode,
} from '../../../config/project-graph';
import { hashArray } from '../../../hasher/file-hasher';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

/**
* NPM
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/pnpm-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ProjectGraphBuilder,
RawProjectGraphDependency,
} from '../../../project-graph/project-graph-builder';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

jest.mock('fs', () => {
const memFs = require('memfs').fs;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/pnpm-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ProjectGraphExternalNode,
} from '../../../config/project-graph';
import { hashArray } from '../../../hasher/file-hasher';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

// we use key => node map to avoid duplicate work when parsing keys
let keyMap = new Map<string, ProjectGraphExternalNode>();
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { vol } from 'memfs';
import { ProjectGraph } from '../../../config/project-graph';
import { PackageJson } from '../../../utils/package-json';
import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

jest.mock('fs', () => {
const memFs = require('memfs').fs;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/yarn-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../../../config/project-graph';
import { hashArray } from '../../../hasher/file-hasher';
import { sortObjectByKeys } from '../../../utils/object-sort';
import { CreateDependenciesContext } from '../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../project-graph/plugins';

/**
* Yarn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildExplicitTypeScriptDependencies } from './explicit-project-dependencies';
import { buildExplicitPackageJsonDependencies } from './explicit-package-json-dependencies';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import { RawProjectGraphDependency } from '../../../../project-graph/project-graph-builder';

export function buildExplicitDependencies(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildExplicitPackageJsonDependencies } from './explicit-package-json-de
import { ProjectGraphProjectNode } from '../../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
import { createFileMap } from '../../../../project-graph/file-map-utils';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import { getAllFileDataInContext } from '../../../../utils/workspace-context';

describe('explicit package json dependencies', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../../../config/workspace-json-project-json';
import { NxJsonConfiguration } from '../../../../config/nx-json';
import { PackageJson } from '../../../../utils/package-json';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import {
RawProjectGraphDependency,
validateDependency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
retrieveProjectConfigurations,
retrieveWorkspaceFiles,
} from '../../../../project-graph/utils/retrieve-workspace-files';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import { setupWorkspaceContext } from '../../../../utils/workspace-context';

// projectName => tsconfig import path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { join, relative } from 'path';
import { workspaceRoot } from '../../../../utils/workspace-root';
import { normalizePath } from '../../../../utils/path';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { CreateDependenciesContext } from '../../../../project-graph/plugins';
import {
RawProjectGraphDependency,
validateDependency,
Expand Down
64 changes: 29 additions & 35 deletions packages/nx/src/plugins/package-json-workspaces/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,42 @@ import { toProjectName } from '../../config/workspaces';
import { readJsonFile, readYamlFile } from '../../utils/fileutils';
import { combineGlobPatterns } from '../../utils/globs';
import { NX_PREFIX } from '../../utils/logger';
import { NxPluginV2 } from '../../utils/nx-plugin';
import { output } from '../../utils/output';
import {
PackageJson,
readTargetsFromPackageJson,
} from '../../utils/package-json';
import { joinPathFragments } from '../../utils/path';

export function getNxPackageJsonWorkspacesPlugin(root: string): NxPluginV2 {
const readJson = (f) => readJsonFile(join(root, f));
const patterns = getGlobPatternsFromPackageManagerWorkspaces(root, readJson);

// If the user only specified a negative pattern, we should find all package.json
// files and only return those that don't match a negative pattern.
const negativePatterns = patterns.filter((p) => p.startsWith('!'));
let positivePatterns = patterns.filter((p) => !p.startsWith('!'));

if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}

return {
name: 'nx/core/package-json-workspaces',
createNodes: [
combineGlobPatterns(positivePatterns),
(p) => {
if (!negativePatterns.some((negative) => minimatch(p, negative))) {
return createNodeFromPackageJson(p, root);
}
// A negative pattern matched, so we should not create a node for this package.json
return {};
},
],
};
import { workspaceRoot } from '../../utils/workspace-root';
import { CreateNodes } from '../../project-graph/plugins';

const readJson = (f) => readJsonFile(join(workspaceRoot, f));
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
workspaceRoot,
readJson
);
const negativePatterns = patterns.filter((p) => p.startsWith('!'));
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));
if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}
export const createNodes: CreateNodes = [
combineGlobPatterns(positivePatterns),
(p, _, { workspaceRoot }) => {
if (!negativePatterns.some((negative) => minimatch(p, negative))) {
return createNodeFromPackageJson(p, workspaceRoot);
}
// A negative pattern matched, so we should not create a node for this package.json
return {};
},
];

export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/plugins/package-json-workspaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-nodes';
export const name = 'nx/core/package-json-workspaces';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as memfs from 'memfs';
import '../../../internal-testing-utils/mock-fs';

import { PackageJsonProjectsNextToProjectJsonPlugin } from './package-json-next-to-project-json';
import { CreateNodesContext } from '../../../utils/nx-plugin';
import { CreateNodesContext } from '../../../project-graph/plugins';
const { createNodes } = PackageJsonProjectsNextToProjectJsonPlugin;

describe('nx project.json plugin', () => {
Expand Down
Loading

0 comments on commit 57cc74c

Please sign in to comment.