Skip to content

Commit

Permalink
feat(core): revert running plugins in isolation (nrwl#22246)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Mar 9, 2024
1 parent 235ca8c commit c01b566
Show file tree
Hide file tree
Showing 62 changed files with 871 additions and 1,454 deletions.
2 changes: 1 addition & 1 deletion docs/generated/devkit/NxPluginV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ A plugin for Nx which creates nodes and dependencies for the [ProjectGraph](../.
| Name | Type | Description |
| :-------------------- | :------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| `createDependencies?` | [`CreateDependencies`](../../devkit/documents/CreateDependencies)\<`TOptions`\> | Provides a function to analyze files to create dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph) |
| `createNodes?` | [`CreateNodes`](../../devkit/documents/CreateNodes)\<`TOptions`\> | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `createNodes?` | [`CreateNodes`](../../devkit/documents/CreateNodes) | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `name` | `string` | - |
17 changes: 8 additions & 9 deletions docs/generated/devkit/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

#### Type declaration

| 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` |
| 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` |
7 changes: 1 addition & 6 deletions packages/devkit/src/utils/convert-nx-executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { requireNx } from '../../nx';
import { convertNxExecutor } from './convert-nx-executor';

const { workspaceRoot } = requireNx();

describe('Convert Nx Executor', () => {
it('should convertNxExecutor to builder correctly and produce the same output', async () => {
// ARRANGE
const { schema } = require('@angular-devkit/core');
const {
TestingArchitectHost,
// nx-ignore-next-line
} = require('@angular-devkit/architect/testing') as typeof import('@angular-devkit/architect/testing');
} = require('@angular-devkit/architect/testing');
const { Architect } = require('@angular-devkit/architect');

const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
const testArchitectHost = new TestingArchitectHost();
testArchitectHost.workspaceRoot = workspaceRoot;
const architect = new Architect(testArchitectHost, registry);

const convertedExecutor = convertNxExecutor(echoExecutor);
Expand Down
13 changes: 5 additions & 8 deletions packages/nx/bin/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { readNxJson } from '../src/config/nx-json';
import { setupWorkspaceContext } from '../src/utils/workspace-context';

(async () => {
const start = new Date();
try {
setupWorkspaceContext(workspaceRoot);
if (isMainNxPackage() && fileExists(join(workspaceRoot, 'nx.json'))) {
const b = new Date();
assertSupportedPlatform();

try {
Expand All @@ -35,18 +35,15 @@ import { setupWorkspaceContext } from '../src/utils/workspace-context';
});
})
);
if (process.env.NX_VERBOSE_LOGGING === 'true') {
const a = new Date();
console.log(`Nx postinstall steps took ${a.getTime() - b.getTime()}ms`);
}
}
} catch (e) {
if (process.env.NX_VERBOSE_LOGGING === 'true') {
console.log(e);
}
} finally {
if (process.env.NX_VERBOSE_LOGGING === 'true') {
const end = new Date();
console.log(
`Nx postinstall steps took ${end.getTime() - start.getTime()}ms`
);
}
}
})();

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/project-graph/plugins';
import type { NxPluginV2 } from '../src/utils/nx-plugin';
import { workspaceRoot } from '../src/utils/workspace-root';
import { createNodeFromPackageJson } from '../src/plugins/package-json-workspaces';

Expand Down
4 changes: 1 addition & 3 deletions 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 '../project-graph/plugins';
import { NxPluginV2 } from '../utils/nx-plugin';

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

Expand All @@ -16,8 +16,6 @@ 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 '../project-graph/plugins';
import { readPluginPackageJson } from '../utils/nx-plugin';
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 '../../project-graph/plugins';
import { readPluginPackageJson } from '../../utils/nx-plugin';

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 '../../project-graph/plugins';
import { readPluginPackageJson } from '../../utils/nx-plugin';
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 '../project-graph/plugins';
import { registerPluginTSTranspiler } from '../utils/nx-plugin';

/**
* This function is used to get the implementation factory of an executor or generator.
Expand Down
33 changes: 17 additions & 16 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { toProjectName } from './workspaces';
import { toProjectName, Workspaces } from './workspaces';
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 { loadNxPluginsInIsolation } from '../project-graph/plugins/internal-api';

const libConfig = (root, name?: string) => ({
name: name ?? toProjectName(`${root}/some-file`),
projectType: 'library',
root: `libs/${root}`,
sourceRoot: `libs/${root}/src`,
targets: {
'nx-release-publish': {
dependsOn: ['^nx-release-publish'],
executor: '@nx/js:release-publish',
options: {},
},
},
});

describe('Workspaces', () => {
let fs: TempFs;
Expand Down Expand Up @@ -35,21 +48,9 @@ describe('Workspaces', () => {

const { projects } = await withEnvironmentVariables(
{
NX_WORKSPACE_ROOT_PATH: fs.tempDir,
NX_WORKSPACE_ROOT: fs.tempDir,
},
async () => {
const [plugins, cleanup] = await loadNxPluginsInIsolation(
readNxJson(fs.tempDir).plugins,
fs.tempDir
);
const res = retrieveProjectConfigurations(
plugins,
fs.tempDir,
readNxJson(fs.tempDir)
);
cleanup();
return res;
}
() => retrieveProjectConfigurations(fs.tempDir, readNxJson(fs.tempDir))
);
expect(projects['my-package']).toEqual({
name: 'my-package',
Expand Down
2 changes: 0 additions & 2 deletions packages/nx/src/daemon/server/handle-request-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { serializeResult } from '../socket-utils';
import { serverLogger } from './logger';
import { getCachedSerializedProjectGraphPromise } from './project-graph-incremental-recomputation';
import { HandlerResult } from './server';
import { getPlugins } from './plugins';
import { readNxJson } from '../../config/nx-json';

export async function handleRequestProjectGraph(): Promise<HandlerResult> {
try {
Expand Down
26 changes: 0 additions & 26 deletions packages/nx/src/daemon/server/plugins.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import { workspaceRoot } from '../../utils/workspace-root';
import { notifyFileWatcherSockets } from './file-watching/file-watcher-sockets';
import { serverLogger } from './logger';
import { NxWorkspaceFilesExternals } from '../../native';
import { RemotePlugin } from '../../project-graph/plugins/internal-api';
import { getPlugins } from './plugins';

interface SerializedProjectGraph {
error: Error | null;
Expand Down Expand Up @@ -71,15 +69,14 @@ export async function getCachedSerializedProjectGraphPromise(): Promise<Serializ
// reset the wait time
waitPeriod = 100;
await resetInternalStateIfNxDepsMissing();
const plugins = await getPlugins();
if (collectedUpdatedFiles.size == 0 && collectedDeletedFiles.size == 0) {
if (!cachedSerializedProjectGraphPromise) {
cachedSerializedProjectGraphPromise =
processFilesAndCreateAndSerializeProjectGraph(plugins);
processFilesAndCreateAndSerializeProjectGraph();
}
} else {
cachedSerializedProjectGraphPromise =
processFilesAndCreateAndSerializeProjectGraph(plugins);
processFilesAndCreateAndSerializeProjectGraph();
}
return await cachedSerializedProjectGraphPromise;
} catch (e) {
Expand Down Expand Up @@ -126,7 +123,7 @@ export function addUpdatedAndDeletedFiles(
}

cachedSerializedProjectGraphPromise =
processFilesAndCreateAndSerializeProjectGraph(await getPlugins());
processFilesAndCreateAndSerializeProjectGraph();
await cachedSerializedProjectGraphPromise;

if (createdFiles.length > 0) {
Expand Down Expand Up @@ -202,9 +199,7 @@ async function processCollectedUpdatedAndDeletedFiles(
}
}

async function processFilesAndCreateAndSerializeProjectGraph(
plugins: RemotePlugin[]
): Promise<SerializedProjectGraph> {
async function processFilesAndCreateAndSerializeProjectGraph(): Promise<SerializedProjectGraph> {
try {
performance.mark('hash-watched-changes-start');
const updatedFiles = [...collectedUpdatedFiles.values()];
Expand All @@ -222,9 +217,9 @@ async function processFilesAndCreateAndSerializeProjectGraph(
serverLogger.requestLog([...updatedFiles.values()]);
serverLogger.requestLog([...deletedFiles]);
const nxJson = readNxJson(workspaceRoot);
// Set this globally to allow plugins to know if they are being called from the project graph creation
global.NX_GRAPH_CREATION = true;
const graphNodes = await retrieveProjectConfigurations(
plugins,
workspaceRoot,
nxJson
);
Expand All @@ -234,6 +229,7 @@ async function processFilesAndCreateAndSerializeProjectGraph(
deletedFiles
);
const g = createAndSerializeProjectGraph(graphNodes);

delete global.NX_GRAPH_CREATION;
return g;
} catch (err) {
Expand Down Expand Up @@ -281,8 +277,7 @@ async function createAndSerializeProjectGraph({
allWorkspaceFiles,
rustReferences,
currentProjectFileMapCache || readFileMapCache(),
true,
await getPlugins()
true
);
currentProjectFileMapCache = projectFileMapCache;
currentProjectGraph = projectGraph;
Expand Down
6 changes: 0 additions & 6 deletions packages/nx/src/daemon/server/shutdown-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@ import { serverLogger } from './logger';
import { serializeResult } from '../socket-utils';
import { deleteDaemonJsonProcessCache } from '../cache';
import type { Watcher } from '../../native';
import { cleanupPlugins } from './plugins';

export const SERVER_INACTIVITY_TIMEOUT_MS = 10800000 as const; // 10800000 ms = 3 hours

let watcherInstance: Watcher | undefined;

export function storeWatcherInstance(instance: Watcher) {
watcherInstance = instance;
}

export function getWatcherInstance() {
return watcherInstance;
}

let outputWatcherInstance: Watcher | undefined;

export function storeOutputWatcherInstance(instance: Watcher) {
outputWatcherInstance = instance;
}

export function getOutputWatcherInstance() {
return outputWatcherInstance;
}
Expand All @@ -40,7 +35,6 @@ export async function handleServerProcessTermination({
try {
server.close();
deleteDaemonJsonProcessCache();
cleanupPlugins();

if (watcherInstance) {
await watcherInstance.stop();
Expand Down
9 changes: 3 additions & 6 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,16 @@ export { workspaceLayout } from './config/configuration';

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

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

/**
* @category Workspace
Expand Down
8 changes: 0 additions & 8 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,6 @@ 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 { loadNxPluginsInIsolation } from '../../project-graph/plugins/internal-api';

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

const [plugins, cleanup] = await loadNxPluginsInIsolation(
nxJsonConfiguration.plugins,
builderContext.workspaceRoot
);
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: (
await retrieveProjectConfigurations(
plugins,
builderContext.workspaceRoot,
nxJsonConfiguration
)
).projects,
};
cleanup();
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,
Expand Down
Loading

0 comments on commit c01b566

Please sign in to comment.