From 0e202bde161e93040aff4840ab6a52f5d8c815ed Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 9 May 2024 12:56:33 +0100 Subject: [PATCH] fix(core): addPlugin should not conflict on project.json targets --- .../application/application.spec.ts | 2 + .../cypress-component-configuration.spec.ts | 2 + .../angular/src/generators/host/host.spec.ts | 2 +- .../src/generators/library/library.spec.ts | 2 +- .../ngrx-feature-store.spec.ts | 2 +- .../ngrx-root-store/ngrx-root-store.spec.ts | 2 +- .../angular/src/generators/ngrx/ngrx.spec.ts | 2 +- .../src/generators/remote/remote.spec.ts | 2 +- .../scam-to-standalone.spec.ts | 2 +- .../src/generators/setup-mf/setup-mf.spec.ts | 2 +- .../generators/setup-ssr/setup-ssr.spec.ts | 2 +- .../generators/stories/stories-app.spec.ts | 2 +- .../storybook-configuration.spec.ts | 2 + .../generators/web-worker/web-worker.spec.ts | 2 +- .../rename-webpack-server.spec.ts | 2 +- .../remove-browserlist-config.spec.ts | 2 +- .../update-typescript-target.spec.ts | 2 +- .../update-workspace-config.spec.ts | 2 +- .../update-testing-tsconfig.spec.ts | 2 + .../generators/configuration/configuration.ts | 2 +- packages/cypress/src/generators/init/init.ts | 21 +- packages/detox/src/generators/init/init.ts | 9 +- packages/devkit/src/utils/add-plugin.spec.ts | 208 +++++++++++++----- packages/devkit/src/utils/add-plugin.ts | 99 +++++++-- .../rename-workspace-rules.spec.ts | 2 + .../eslint/src/generators/init/init.spec.ts | 2 - packages/eslint/src/generators/init/init.ts | 4 - .../expo/src/generators/init/init.spec.ts | 2 - packages/expo/src/generators/init/init.ts | 1 - .../jest/src/generators/init/init.spec.ts | 2 - packages/jest/src/generators/init/init.ts | 1 - .../next/src/generators/init/init.spec.ts | 13 +- packages/next/src/generators/init/init.ts | 1 - .../application/application.spec.ts | 2 +- .../nuxt/src/generators/init/init.spec.ts | 2 - packages/nuxt/src/generators/init/init.ts | 1 - packages/nx/src/devkit-internals.ts | 11 +- .../mock-load-nx-plugin.ts | 6 + .../src/project-graph/plugins/internal-api.ts | 7 +- .../utils/retrieve-workspace-files.ts | 1 + .../src/generators/init/init.spec.ts | 18 +- .../playwright/src/generators/init/init.ts | 1 - .../application/application.spec.ts | 2 - .../src/generators/init/init.spec.ts | 2 - .../react-native/src/generators/init/init.ts | 2 - .../application/application.spec.ts | 2 +- .../cypress-component-configuration.spec.ts | 3 - .../remix/src/generators/init/init.spec.ts | 2 - packages/remix/src/generators/init/init.ts | 7 +- .../rollup/src/generators/init/init.spec.ts | 14 -- packages/rollup/src/generators/init/init.ts | 2 - .../src/generators/init/init.spec.ts | 18 +- .../storybook/src/generators/init/init.ts | 2 - .../vite/src/generators/init/init.spec.ts | 13 -- packages/vite/src/generators/init/init.ts | 2 - .../application/application.spec.ts | 2 +- .../webpack/src/generators/init/init.spec.ts | 2 - packages/webpack/src/generators/init/init.ts | 2 - .../src/generators/preset/preset.spec.ts | 2 +- 59 files changed, 291 insertions(+), 242 deletions(-) create mode 100644 packages/nx/src/internal-testing-utils/mock-load-nx-plugin.ts diff --git a/packages/angular/src/generators/application/application.spec.ts b/packages/angular/src/generators/application/application.spec.ts index 514129d1c5b6b..f0b7c8d612c2c 100644 --- a/packages/angular/src/generators/application/application.spec.ts +++ b/packages/angular/src/generators/application/application.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; + import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import type { Tree } from '@nx/devkit'; import * as devkit from '@nx/devkit'; diff --git a/packages/angular/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts b/packages/angular/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts index 0a318c4b0c246..6ac46bee0c621 100644 --- a/packages/angular/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts +++ b/packages/angular/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; + import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import { DependencyType, diff --git a/packages/angular/src/generators/host/host.spec.ts b/packages/angular/src/generators/host/host.spec.ts index ab04beb3eaa2e..9130e19eaa3a0 100644 --- a/packages/angular/src/generators/host/host.spec.ts +++ b/packages/angular/src/generators/host/host.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { readJson, updateJson } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index 5dfa64c650a92..083210fb64849 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { getProjects, diff --git a/packages/angular/src/generators/ngrx-feature-store/ngrx-feature-store.spec.ts b/packages/angular/src/generators/ngrx-feature-store/ngrx-feature-store.spec.ts index df0332ea9fa92..34359a5c444fa 100644 --- a/packages/angular/src/generators/ngrx-feature-store/ngrx-feature-store.spec.ts +++ b/packages/angular/src/generators/ngrx-feature-store/ngrx-feature-store.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import type { Tree } from '@nx/devkit'; import { readJson } from '@nx/devkit'; diff --git a/packages/angular/src/generators/ngrx-root-store/ngrx-root-store.spec.ts b/packages/angular/src/generators/ngrx-root-store/ngrx-root-store.spec.ts index 7148489abe09b..f159db6447b66 100644 --- a/packages/angular/src/generators/ngrx-root-store/ngrx-root-store.spec.ts +++ b/packages/angular/src/generators/ngrx-root-store/ngrx-root-store.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import type { Tree } from '@nx/devkit'; import { readJson } from '@nx/devkit'; diff --git a/packages/angular/src/generators/ngrx/ngrx.spec.ts b/packages/angular/src/generators/ngrx/ngrx.spec.ts index 72b2e3ea32d4c..bbea30387b050 100644 --- a/packages/angular/src/generators/ngrx/ngrx.spec.ts +++ b/packages/angular/src/generators/ngrx/ngrx.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import type { Tree } from '@nx/devkit'; import * as devkit from '@nx/devkit'; diff --git a/packages/angular/src/generators/remote/remote.spec.ts b/packages/angular/src/generators/remote/remote.spec.ts index 8e2fbb78685ae..95b153a0ca750 100644 --- a/packages/angular/src/generators/remote/remote.spec.ts +++ b/packages/angular/src/generators/remote/remote.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { E2eTestRunner } from '../../utils/test-runners'; import { diff --git a/packages/angular/src/generators/scam-to-standalone/scam-to-standalone.spec.ts b/packages/angular/src/generators/scam-to-standalone/scam-to-standalone.spec.ts index 87e89d3007395..e52338334287e 100644 --- a/packages/angular/src/generators/scam-to-standalone/scam-to-standalone.spec.ts +++ b/packages/angular/src/generators/scam-to-standalone/scam-to-standalone.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import scamGenerator from '../scam/scam'; diff --git a/packages/angular/src/generators/setup-mf/setup-mf.spec.ts b/packages/angular/src/generators/setup-mf/setup-mf.spec.ts index 5a7491a203e0e..009844cf90c52 100644 --- a/packages/angular/src/generators/setup-mf/setup-mf.spec.ts +++ b/packages/angular/src/generators/setup-mf/setup-mf.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { readJson, diff --git a/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts b/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts index fb18673bf58f2..3d8f829150179 100644 --- a/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts +++ b/packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { NxJsonConfiguration, diff --git a/packages/angular/src/generators/stories/stories-app.spec.ts b/packages/angular/src/generators/stories/stories-app.spec.ts index 7d67280605dba..079afc6491a51 100644 --- a/packages/angular/src/generators/stories/stories-app.spec.ts +++ b/packages/angular/src/generators/stories/stories-app.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import type { Tree } from '@nx/devkit'; diff --git a/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts b/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts index 5f07c2ebfdd03..4d2ed4e01f63f 100644 --- a/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts +++ b/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; + import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import type { Tree } from '@nx/devkit'; import { readJson, writeJson } from '@nx/devkit'; diff --git a/packages/angular/src/generators/web-worker/web-worker.spec.ts b/packages/angular/src/generators/web-worker/web-worker.spec.ts index fc298a45d93ab..1b03930a61a4e 100644 --- a/packages/angular/src/generators/web-worker/web-worker.spec.ts +++ b/packages/angular/src/generators/web-worker/web-worker.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import type { Tree } from '@nx/devkit'; import * as devkit from '@nx/devkit'; diff --git a/packages/angular/src/migrations/update-14-8-0/rename-webpack-server.spec.ts b/packages/angular/src/migrations/update-14-8-0/rename-webpack-server.spec.ts index 707a28f9edb71..d2e65afe5a213 100644 --- a/packages/angular/src/migrations/update-14-8-0/rename-webpack-server.spec.ts +++ b/packages/angular/src/migrations/update-14-8-0/rename-webpack-server.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { readJson, updateJson } from '@nx/devkit'; import * as devkit from '@nx/devkit'; diff --git a/packages/angular/src/migrations/update-15-2-0/remove-browserlist-config.spec.ts b/packages/angular/src/migrations/update-15-2-0/remove-browserlist-config.spec.ts index 9be54c6387f12..f1d08de0c1504 100644 --- a/packages/angular/src/migrations/update-15-2-0/remove-browserlist-config.spec.ts +++ b/packages/angular/src/migrations/update-15-2-0/remove-browserlist-config.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import type { Tree } from '@nx/devkit'; import * as devkit from '@nx/devkit'; diff --git a/packages/angular/src/migrations/update-15-2-0/update-typescript-target.spec.ts b/packages/angular/src/migrations/update-15-2-0/update-typescript-target.spec.ts index 46b5d45687dfb..f4193b02d6e57 100644 --- a/packages/angular/src/migrations/update-15-2-0/update-typescript-target.spec.ts +++ b/packages/angular/src/migrations/update-15-2-0/update-typescript-target.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { readJson, diff --git a/packages/angular/src/migrations/update-15-2-0/update-workspace-config.spec.ts b/packages/angular/src/migrations/update-15-2-0/update-workspace-config.spec.ts index 3c5967bd06bcf..9e70d26c354b0 100644 --- a/packages/angular/src/migrations/update-15-2-0/update-workspace-config.spec.ts +++ b/packages/angular/src/migrations/update-15-2-0/update-workspace-config.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import * as devkit from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; diff --git a/packages/angular/src/migrations/update-15-9-0/update-testing-tsconfig.spec.ts b/packages/angular/src/migrations/update-15-9-0/update-testing-tsconfig.spec.ts index 5da3ccf68b663..555b0af4a42c5 100644 --- a/packages/angular/src/migrations/update-15-9-0/update-testing-tsconfig.spec.ts +++ b/packages/angular/src/migrations/update-15-9-0/update-testing-tsconfig.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; + import { ProjectGraph, readJson, diff --git a/packages/cypress/src/generators/configuration/configuration.ts b/packages/cypress/src/generators/configuration/configuration.ts index 8635ee5f9664c..35a404f7af7e3 100644 --- a/packages/cypress/src/generators/configuration/configuration.ts +++ b/packages/cypress/src/generators/configuration/configuration.ts @@ -80,7 +80,7 @@ export async function configurationGeneratorInternal( }) ); } else if (opts.addPlugin) { - await addPlugin(tree, projectGraph, false); + await addPlugin(tree, false); } const nxJson = readNxJson(tree); diff --git a/packages/cypress/src/generators/init/init.ts b/packages/cypress/src/generators/init/init.ts index f4d633d87049d..a7ed8ce00c7b1 100644 --- a/packages/cypress/src/generators/init/init.ts +++ b/packages/cypress/src/generators/init/init.ts @@ -1,23 +1,17 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, - ProjectGraph, readNxJson, removeDependenciesFromPackageJson, runTasksInSerial, Tree, updateNxJson, } from '@nx/devkit'; -import { - addPlugin as _addPlugin, - generateCombinations, -} from '@nx/devkit/src/utils/add-plugin'; +import { addPlugin as _addPlugin } from '@nx/devkit/src/utils/add-plugin'; import { createNodes } from '../../plugins/plugin'; import { cypressVersion, nxVersion } from '../../utils/versions'; import { Schema } from './schema'; -import { CypressPluginOptions } from '../../plugins/plugin'; function setupE2ETargetDefaults(tree: Tree) { const nxJson = readNxJson(tree); @@ -60,14 +54,9 @@ function updateDependencies(tree: Tree, options: Schema) { return runTasksInSerial(...tasks); } -export function addPlugin( - tree: Tree, - graph: ProjectGraph, - updatePackageScripts: boolean -) { +export function addPlugin(tree: Tree, updatePackageScripts: boolean) { return _addPlugin( tree, - graph, '@nx/cypress/plugin', createNodes, { @@ -118,11 +107,7 @@ export async function cypressInitGeneratorInternal( nxJson.useInferencePlugins !== false; if (options.addPlugin) { - await addPlugin( - tree, - await createProjectGraphAsync(), - options.updatePackageScripts - ); + await addPlugin(tree, options.updatePackageScripts); } else { setupE2ETargetDefaults(tree); } diff --git a/packages/detox/src/generators/init/init.ts b/packages/detox/src/generators/init/init.ts index 8753d6ecf9836..1340eb644088c 100644 --- a/packages/detox/src/generators/init/init.ts +++ b/packages/detox/src/generators/init/init.ts @@ -1,6 +1,5 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -8,11 +7,8 @@ import { runTasksInSerial, Tree, } from '@nx/devkit'; -import { - addPlugin, - generateCombinations, -} from '@nx/devkit/src/utils/add-plugin'; -import { createNodes, DetoxPluginOptions } from '../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodes } from '../../plugins/plugin'; import { detoxVersion, nxVersion } from '../../utils/versions'; import { Schema } from './schema'; @@ -38,7 +34,6 @@ export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) { if (schema.addPlugin) { await addPlugin( host, - await createProjectGraphAsync(), '@nx/detox/plugin', createNodes, { diff --git a/packages/devkit/src/utils/add-plugin.spec.ts b/packages/devkit/src/utils/add-plugin.spec.ts index 6a8135d929330..034a84210c661 100644 --- a/packages/devkit/src/utils/add-plugin.spec.ts +++ b/packages/devkit/src/utils/add-plugin.spec.ts @@ -1,17 +1,50 @@ +jest.mock('vite', () => ({ + resolveConfig: jest.fn().mockImplementation(() => { + return Promise.resolve({ + path: 'vite.config.ts', + config: {}, + build: {}, + dependencies: [], + }); + }), +})); + +jest.mock('@nx/vite/plugin', () => ({ + createNodes: [ + '**/vite.config.{js,cjs,mjs}', + (_, { targetName }) => ({ + projects: { + app1: { + name: 'app1', + targets: { + [targetName]: { command: 'vite build' }, + }, + }, + app2: { + name: 'app2', + targets: { + [targetName]: { command: 'vite build' }, + }, + }, + }, + }), + ], + createDependencies: jest.fn(), +})); + import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace'; import type { Tree } from 'nx/src/generators/tree'; import { readJson, writeJson } from 'nx/src/generators/utils/json'; import type { PackageJson } from 'nx/src/utils/package-json'; import { CreateNodes } from 'nx/src/project-graph/plugins'; -import { ProjectGraph } from 'nx/src/devkit-exports'; import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; import { addPlugin, generateCombinations } from './add-plugin'; +import { updateNxJson } from 'nx/src/generators/utils/nx-json'; describe('addPlugin', () => { let tree: Tree; let createNodes: CreateNodes<{ targetName: string }>; - let graph: ProjectGraph; let fs: TempFs; beforeEach(async () => { @@ -20,39 +53,6 @@ describe('addPlugin', () => { fs = new TempFs('add-plugin'); tree.root = fs.tempDir; - graph = { - nodes: { - app1: { - name: 'app1', - type: 'app', - data: { - root: 'app1', - targets: {}, - }, - }, - app2: { - name: 'app2', - type: 'app', - data: { - root: 'app2', - targets: {}, - }, - }, - app3: { - name: 'app3', - type: 'app', - data: { - root: 'app3', - targets: {}, - }, - }, - }, - dependencies: { - app1: [], - app2: [], - app3: [], - }, - }; createNodes = [ '**/next.config.{js,cjs,mjs}', (_, { targetName }) => ({ @@ -84,41 +84,148 @@ describe('addPlugin', () => { }); describe('adding the plugin', () => { - it('should not conflicting with the existing graph', async () => { - graph.nodes.app1.data.targets.build = {}; - graph.nodes.app2.data.targets.build1 = {}; - // app 3 doesn't have a next config, so it having this - // target should not affect the plugin options - graph.nodes.app3.data.targets.build2 = {}; + it('should not conflicting with the existing plugins', async () => { + // ARRANGE + const nxJson = readJson(tree, 'nx.json'); + nxJson.plugins ??= []; + nxJson.plugins.push({ + plugin: '@nx/vite/plugin', + options: { targetName: 'build' }, + }); + updateNxJson(tree, nxJson); + fs.createFilesSync({ + 'app1/vite.config.js': '', + 'app2/vite.config.js': '', + }); + // ACT await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, { - targetName: ['build', 'build1', 'build2'], + targetName: ['build', 'build1'], }, true ); - expect(readJson(tree, 'nx.json').plugins).toContainEqual({ + // ASSERT + expect( + readJson(tree, 'nx.json').plugins.find( + (p) => p.plugin === '@nx/next/plugin' + ) + ).toEqual({ plugin: '@nx/next/plugin', options: { - targetName: 'build2', + targetName: 'build1', + }, + }); + }); + + it('should not conflicting with existing project.json with executor and run-commands in plugin', async () => { + // ARRANGE + fs.createFilesSync({ + 'app1/project.json': JSON.stringify({ + targets: { build: { executor: '@nx/next:build' } }, + }), + }); + + // ACT + await addPlugin( + tree, + '@nx/next/plugin', + createNodes, + + { + targetName: ['build'], + }, + true + ); + + // ASSERT + expect( + readJson(tree, 'nx.json').plugins.find( + (p) => p.plugin === '@nx/next/plugin' + ) + ).toEqual({ + plugin: '@nx/next/plugin', + options: { + targetName: 'build', + }, + }); + }); + + it('should not conflicting with existing project.json with executor and executor in plugin', async () => { + // ARRANGE + fs.createFilesSync({ + 'app1/project.json': JSON.stringify({ + targets: { build: { executor: '@nx/next:build' } }, + }), + }); + + createNodes = [ + '**/next.config.{js,cjs,mjs}', + (_, { targetName }) => ({ + projects: { + app1: { + name: 'app1', + targets: { + [targetName]: { executor: '@nx/next:build' }, + }, + }, + app2: { + name: 'app2', + targets: { + [targetName]: { executor: '@nx/next:build' }, + }, + }, + }, + }), + ]; + + // ACT + await addPlugin( + tree, + '@nx/next/plugin', + createNodes, + + { + targetName: ['build'], + }, + true + ); + + // ASSERT + expect( + readJson(tree, 'nx.json').plugins.find( + (p) => p.plugin === '@nx/next/plugin' + ) + ).toEqual({ + plugin: '@nx/next/plugin', + options: { + targetName: 'build', }, }); }); }); it('should throw an error if no non-conflicting options are provided', async () => { - graph.nodes.app1.data.targets.build = {}; + const nxJson = readJson(tree, 'nx.json'); + nxJson.plugins ??= []; + nxJson.plugins.push({ + plugin: '@nx/vite/plugin', + options: { targetName: 'build' }, + }); + updateNxJson(tree, nxJson); + fs.createFilesSync({ + 'app1/vite.config.js': '', + 'app2/vite.config.js': '', + }); try { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -147,7 +254,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -185,7 +291,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -234,7 +339,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/cypress/plugin', createNodes, @@ -259,7 +363,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -300,7 +403,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -342,7 +444,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, @@ -384,7 +485,6 @@ describe('addPlugin', () => { await addPlugin( tree, - graph, '@nx/next/plugin', createNodes, diff --git a/packages/devkit/src/utils/add-plugin.ts b/packages/devkit/src/utils/add-plugin.ts index 79fcf70e0743b..627e94b6b22c1 100644 --- a/packages/devkit/src/utils/add-plugin.ts +++ b/packages/devkit/src/utils/add-plugin.ts @@ -1,12 +1,12 @@ import type { PackageJson } from 'nx/src/utils/package-json'; - import type { ConfigurationResult } from 'nx/src/project-graph/utils/project-configuration-utils'; + import * as yargs from 'yargs-parser'; import { CreateNodes, + NxJsonConfiguration, ProjectConfiguration, - ProjectGraph, readJson, readNxJson, Tree, @@ -15,6 +15,10 @@ import { } from 'nx/src/devkit-exports'; import { LoadedNxPlugin, + loadNxPlugins, + isCompatibleTarget, + mergeTargetConfigurations, + deepEquals, ProjectConfigurationsError, retrieveProjectConfigurations, } from 'nx/src/devkit-internals'; @@ -25,7 +29,6 @@ import { */ export async function addPlugin( tree: Tree, - graph: ProjectGraph, pluginName: string, createNodesTuple: CreateNodes, options: Partial< @@ -33,11 +36,12 @@ export async function addPlugin( >, shouldUpdatePackageJsonScripts: boolean ): Promise { - const graphNodes = Object.values(graph.nodes); const nxJson = readNxJson(tree); let pluginOptions: PluginOptions; - let projConfigs: ConfigurationResult; + let projConfigsWithExistingNxJsonPlugins: ConfigurationResult; + let projConfigsWithNewPlugin: ConfigurationResult; + let projConfigsWithCorePlugins: ConfigurationResult; const combinations = generateCombinations(options); optionsLoop: for (const _pluginOptions of combinations) { pluginOptions = _pluginOptions as PluginOptions; @@ -54,8 +58,21 @@ export async function addPlugin( return; } global.NX_GRAPH_CREATION = true; - try { - projConfigs = await retrieveProjectConfigurations( + + const [nxJsonPlugins] = await loadNxPlugins( + nxJson.plugins, + tree.root, + true + ); + projConfigsWithExistingNxJsonPlugins = + await retrieveProjectConfigurationsWithPartialResult( + nxJsonPlugins, + tree.root, + nxJson + ); + + projConfigsWithNewPlugin = + await retrieveProjectConfigurationsWithPartialResult( [ new LoadedNxPlugin( { @@ -71,27 +88,38 @@ export async function addPlugin( tree.root, nxJson ); - } catch (e) { - // Errors are okay for this because we're only running 1 plugin - if (e instanceof ProjectConfigurationsError) { - projConfigs = e.partialProjectConfigurationsResult; - } else { - throw e; - } - } - global.NX_GRAPH_CREATION = false; - for (const projConfig of Object.values(projConfigs.projects)) { - const node = graphNodes.find( - (node) => node.data.root === projConfig.root + const [corePlugins] = await loadNxPlugins([], tree.root); + projConfigsWithCorePlugins = + await retrieveProjectConfigurationsWithPartialResult( + corePlugins, + tree.root, + nxJson ); - if (!node) { - continue; - } + global.NX_GRAPH_CREATION = false; + for (const projConfig of Object.values(projConfigsWithNewPlugin.projects)) { for (const targetName in projConfig.targets) { - if (node.data.targets[targetName]) { + const existingPluginCreatedTarget = + projConfigsWithExistingNxJsonPlugins?.projects?.[projConfig.root] + ?.targets?.[targetName]; + + const corePluginsCreatedTarget = + projConfigsWithCorePlugins?.projects?.[projConfig.root]?.targets?.[ + targetName + ]; + if ( + existingPluginCreatedTarget || + (corePluginsCreatedTarget && + !deepEquals( + corePluginsCreatedTarget, + mergeTargetConfigurations( + corePluginsCreatedTarget, + projConfig.targets[targetName] + ) + )) + ) { // Conflicting Target Name, check the next one pluginOptions = null; continue optionsLoop; @@ -116,7 +144,7 @@ export async function addPlugin( updateNxJson(tree, nxJson); if (shouldUpdatePackageJsonScripts) { - updatePackageScripts(tree, projConfigs); + updatePackageScripts(tree, projConfigsWithNewPlugin); } } @@ -353,3 +381,26 @@ function _generateCombinations(input: T[][]): T[][] { ); } } + +export async function retrieveProjectConfigurationsWithPartialResult( + plugins: LoadedNxPlugin[], + workspaceRoot: string, + nxJson: NxJsonConfiguration +): Promise { + let projConfigs: ConfigurationResult; + try { + projConfigs = await retrieveProjectConfigurations( + plugins, + workspaceRoot, + nxJson + ); + } catch (e) { + // Errors are okay for this because we're only running 1 plugin + if (e instanceof ProjectConfigurationsError) { + projConfigs = e.partialProjectConfigurationsResult; + } else { + throw e; + } + } + return projConfigs; +} diff --git a/packages/eslint-plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.spec.ts b/packages/eslint-plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.spec.ts index c00e45fe28f70..4d4126b6af1fe 100644 --- a/packages/eslint-plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.spec.ts +++ b/packages/eslint-plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; + import { Tree, readJson, writeJson } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports'; import { WORKSPACE_PLUGIN_DIR } from '../../constants'; diff --git a/packages/eslint/src/generators/init/init.spec.ts b/packages/eslint/src/generators/init/init.spec.ts index bcf161b287aef..552b65d8f363c 100644 --- a/packages/eslint/src/generators/init/init.spec.ts +++ b/packages/eslint/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { NxJsonConfiguration, readJson, Tree, updateJson } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { LinterInitOptions, lintInitGenerator } from './init'; diff --git a/packages/eslint/src/generators/init/init.ts b/packages/eslint/src/generators/init/init.ts index dfe622ea8cae5..d756eddcaff35 100644 --- a/packages/eslint/src/generators/init/init.ts +++ b/packages/eslint/src/generators/init/init.ts @@ -61,8 +61,6 @@ export async function initEsLint( const hasPlugin = hasEslintPlugin(tree); const rootEslintFile = findEslintFile(tree); - const graph = await createProjectGraphAsync(); - const lintTargetNames = [ 'lint', 'eslint:lint', @@ -75,7 +73,6 @@ export async function initEsLint( if (rootEslintFile && options.addPlugin && !hasPlugin) { await addPlugin( tree, - graph, '@nx/eslint/plugin', createNodes, { @@ -96,7 +93,6 @@ export async function initEsLint( if (options.addPlugin) { await addPlugin( tree, - graph, '@nx/eslint/plugin', createNodes, { diff --git a/packages/expo/src/generators/init/init.spec.ts b/packages/expo/src/generators/init/init.spec.ts index b40251d9a146d..64efd74987ba6 100644 --- a/packages/expo/src/generators/init/init.spec.ts +++ b/packages/expo/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { readJson, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { expoInitGenerator } from './init'; diff --git a/packages/expo/src/generators/init/init.ts b/packages/expo/src/generators/init/init.ts index da4a087481624..b3290d32a9d88 100644 --- a/packages/expo/src/generators/init/init.ts +++ b/packages/expo/src/generators/init/init.ts @@ -38,7 +38,6 @@ export async function expoInitGeneratorInternal(host: Tree, schema: Schema) { if (schema.addPlugin) { await addPlugin( host, - await createProjectGraphAsync(), '@nx/expo/plugin', createNodes, { diff --git a/packages/jest/src/generators/init/init.spec.ts b/packages/jest/src/generators/init/init.spec.ts index 6841ce4218131..eb59d466d95da 100644 --- a/packages/jest/src/generators/init/init.spec.ts +++ b/packages/jest/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { type NxJsonConfiguration, readJson, diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 78c6fed32061d..77772298daa0b 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -103,7 +103,6 @@ export async function jestInitGeneratorInternal( if (options.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/jest/plugin', createNodes, { diff --git a/packages/next/src/generators/init/init.spec.ts b/packages/next/src/generators/init/init.spec.ts index a0b3497ed2420..094e47f8ddb3a 100644 --- a/packages/next/src/generators/init/init.spec.ts +++ b/packages/next/src/generators/init/init.spec.ts @@ -1,23 +1,12 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; -import { readJson, Tree, ProjectGraph } from '@nx/devkit'; +import { readJson, Tree } from '@nx/devkit'; import { nextInitGenerator } from './init'; -let projectGraph: ProjectGraph; -jest.mock('@nx/devkit', () => ({ - ...jest.requireActual('@nx/devkit'), - createProjectGraphAsync: jest.fn().mockImplementation(async () => { - return projectGraph; - }), -})); describe('init', () => { let tree: Tree; beforeEach(() => { - projectGraph = { - nodes: {}, - dependencies: {}, - }; tree = createTreeWithEmptyWorkspace(); }); diff --git a/packages/next/src/generators/init/init.ts b/packages/next/src/generators/init/init.ts index 7608702ef4b0b..4f1e7be26a621 100644 --- a/packages/next/src/generators/init/init.ts +++ b/packages/next/src/generators/init/init.ts @@ -55,7 +55,6 @@ export async function nextInitGeneratorInternal( const { createNodes } = await import('../../plugins/plugin'); await addPlugin( host, - await createProjectGraphAsync(), '@nx/next/plugin', createNodes, { diff --git a/packages/node/src/generators/application/application.spec.ts b/packages/node/src/generators/application/application.spec.ts index 3d130a7a8ebad..2ccad9b543f28 100644 --- a/packages/node/src/generators/application/application.spec.ts +++ b/packages/node/src/generators/application/application.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import * as devkit from '@nx/devkit'; import { diff --git a/packages/nuxt/src/generators/init/init.spec.ts b/packages/nuxt/src/generators/init/init.spec.ts index b5885a2deecab..814777b34d100 100644 --- a/packages/nuxt/src/generators/init/init.spec.ts +++ b/packages/nuxt/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { readJson, readNxJson, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { nuxtInitGenerator } from './init'; diff --git a/packages/nuxt/src/generators/init/init.ts b/packages/nuxt/src/generators/init/init.ts index 819b22f609a5c..f72542e33fc94 100644 --- a/packages/nuxt/src/generators/init/init.ts +++ b/packages/nuxt/src/generators/init/init.ts @@ -8,7 +8,6 @@ import { updateDependencies } from './lib/utils'; export async function nuxtInitGenerator(host: Tree, schema: InitSchema) { await addPlugin( host, - await createProjectGraphAsync(), '@nx/nuxt/plugin', createNodes, { diff --git a/packages/nx/src/devkit-internals.ts b/packages/nx/src/devkit-internals.ts index 1deeefad1430b..847793e152004 100644 --- a/packages/nx/src/devkit-internals.ts +++ b/packages/nx/src/devkit-internals.ts @@ -4,12 +4,16 @@ * These may not be available in certain version of Nx, so be sure to check them first. */ export { createTempNpmDirectory } from './utils/package-manager'; +export { deepEquals } from './utils/json-diff'; export { getExecutorInformation } from './command-line/run/executor-utils'; export { readNxJson as readNxJsonFromDisk } from './config/nx-json'; export { calculateDefaultProjectName } from './config/calculate-default-project-name'; export { retrieveProjectConfigurationsWithAngularProjects } from './project-graph/utils/retrieve-workspace-files'; export { mergeTargetConfigurations } from './project-graph/utils/project-configuration-utils'; -export { readProjectConfigurationsFromRootMap } from './project-graph/utils/project-configuration-utils'; +export { + readProjectConfigurationsFromRootMap, + isCompatibleTarget, +} from './project-graph/utils/project-configuration-utils'; export { splitTarget } from './utils/split-target'; export { combineOptionsForExecutor } from './utils/params'; export { sortObjectByKeys } from './utils/object-sort'; @@ -23,6 +27,9 @@ 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, + loadNxPlugins, +} from './project-graph/plugins/internal-api'; export * from './project-graph/error-types'; export { registerTsProject } from './plugins/js/utils/register'; diff --git a/packages/nx/src/internal-testing-utils/mock-load-nx-plugin.ts b/packages/nx/src/internal-testing-utils/mock-load-nx-plugin.ts new file mode 100644 index 0000000000000..90fd25f7d51e6 --- /dev/null +++ b/packages/nx/src/internal-testing-utils/mock-load-nx-plugin.ts @@ -0,0 +1,6 @@ +jest.mock('nx/src/project-graph/plugins/loader', () => ({ + ...jest.requireActual('nx/src/project-graph/plugins/loader'), + loadNxPlugin: jest.fn().mockImplementation(() => { + return [Promise.resolve({}), () => {}]; + }), +})); diff --git a/packages/nx/src/project-graph/plugins/internal-api.ts b/packages/nx/src/project-graph/plugins/internal-api.ts index 874f750d66f7f..83718d392ece6 100644 --- a/packages/nx/src/project-graph/plugins/internal-api.ts +++ b/packages/nx/src/project-graph/plugins/internal-api.ts @@ -98,7 +98,8 @@ export const nxPluginCache: Map< export async function loadNxPlugins( plugins: PluginConfiguration[], - root = workspaceRoot + root = workspaceRoot, + skipDefaultPlugins = false ): Promise<[LoadedNxPlugin[], () => void]> { const result: Promise[] = []; @@ -107,7 +108,9 @@ export async function loadNxPlugins( ? loadNxPluginInIsolation : loadNxPlugin; - plugins = await normalizePlugins(plugins, root); + plugins = skipDefaultPlugins + ? plugins ?? [] + : await normalizePlugins(plugins, root); const cleanupFunctions: Array<() => void> = []; for (const plugin of plugins) { diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts index d61a11cef9a4b..893dbc4bdb5ba 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -16,6 +16,7 @@ import { } from '../../utils/workspace-context'; import { buildAllWorkspaceFiles } from './build-all-workspace-files'; import { join } from 'path'; +import { ProjectConfigurationsError } from '../error-types'; /** * Walks the workspace directory to create the `projectFileMap`, `ProjectConfigurations` and `allWorkspaceFiles` diff --git a/packages/playwright/src/generators/init/init.spec.ts b/packages/playwright/src/generators/init/init.spec.ts index c4dea0c653753..8c1edc2f9cf65 100644 --- a/packages/playwright/src/generators/init/init.spec.ts +++ b/packages/playwright/src/generators/init/init.spec.ts @@ -1,24 +1,12 @@ -import { ProjectGraph, readNxJson, Tree, updateNxJson } from '@nx/devkit'; +import { readNxJson, Tree, updateNxJson } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { initGenerator } from './init'; -let projectGraph: ProjectGraph; -jest.mock('@nx/devkit', () => ({ - ...jest.requireActual('@nx/devkit'), - createProjectGraphAsync: jest.fn().mockImplementation(async () => { - return projectGraph; - }), -})); - describe('@nx/playwright:init', () => { let tree: Tree; beforeEach(() => { - projectGraph = { - nodes: {}, - dependencies: {}, - }; tree = createTreeWithEmptyWorkspace(); }); @@ -43,7 +31,7 @@ describe('@nx/playwright:init', () => { it('should not overwrite existing plugins', async () => { updateNxJson(tree, { - plugins: ['foo'], + plugins: ['@nx/vite/plugin'], }); await initGenerator(tree, { skipFormat: true, @@ -53,7 +41,7 @@ describe('@nx/playwright:init', () => { const nxJson = readNxJson(tree); expect(nxJson.plugins).toMatchInlineSnapshot(` [ - "foo", + "@nx/vite/plugin", { "options": { "targetName": "e2e", diff --git a/packages/playwright/src/generators/init/init.ts b/packages/playwright/src/generators/init/init.ts index ae90ae3a95556..a75695deff38a 100644 --- a/packages/playwright/src/generators/init/init.ts +++ b/packages/playwright/src/generators/init/init.ts @@ -47,7 +47,6 @@ export async function initGeneratorInternal( if (options.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/playwright/plugin', createNodes, { targetName: ['e2e', 'playwright:e2e', 'playwright-e2e'] }, diff --git a/packages/react-native/src/generators/application/application.spec.ts b/packages/react-native/src/generators/application/application.spec.ts index ca2343713d156..14db3d844993e 100644 --- a/packages/react-native/src/generators/application/application.spec.ts +++ b/packages/react-native/src/generators/application/application.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { Tree, getProjects, diff --git a/packages/react-native/src/generators/init/init.spec.ts b/packages/react-native/src/generators/init/init.spec.ts index 2e0939bc08788..85ccf173c18c1 100644 --- a/packages/react-native/src/generators/init/init.spec.ts +++ b/packages/react-native/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { Tree, readJson } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { reactNativeInitGenerator } from './init'; diff --git a/packages/react-native/src/generators/init/init.ts b/packages/react-native/src/generators/init/init.ts index be1331f55cf0e..0a094797a3bc1 100644 --- a/packages/react-native/src/generators/init/init.ts +++ b/packages/react-native/src/generators/init/init.ts @@ -1,6 +1,5 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -41,7 +40,6 @@ export async function reactNativeInitGeneratorInternal( if (schema.addPlugin) { await addPlugin( host, - await createProjectGraphAsync(), '@nx/react-native/plugin', createNodes, { diff --git a/packages/react/src/generators/application/application.spec.ts b/packages/react/src/generators/application/application.spec.ts index b834227e75145..a728645e03b8e 100644 --- a/packages/react/src/generators/application/application.spec.ts +++ b/packages/react/src/generators/application/application.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import { diff --git a/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts b/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts index ded692ebb0699..a2058bfe0d0e5 100644 --- a/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts +++ b/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.spec.ts @@ -472,9 +472,6 @@ describe('React:CypressComponentTestConfiguration', () => { buildTarget: 'my-app:build', }); }).resolves; - expect(require('@nx/devkit').createProjectGraphAsync).toHaveBeenCalledTimes( - 1 - ); }); it('should setup cypress config files correctly', async () => { diff --git a/packages/remix/src/generators/init/init.spec.ts b/packages/remix/src/generators/init/init.spec.ts index 92b66265770de..48fc5639d9bf1 100644 --- a/packages/remix/src/generators/init/init.spec.ts +++ b/packages/remix/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { readJson } from '@nx/devkit'; import initGenerator from './init'; diff --git a/packages/remix/src/generators/init/init.ts b/packages/remix/src/generators/init/init.ts index 80e7214b18b66..745c87e08cfa6 100644 --- a/packages/remix/src/generators/init/init.ts +++ b/packages/remix/src/generators/init/init.ts @@ -5,12 +5,8 @@ import { readNxJson, addDependenciesToPackageJson, runTasksInSerial, - createProjectGraphAsync, } from '@nx/devkit'; -import { - addPlugin, - generateCombinations, -} from '@nx/devkit/src/utils/add-plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; import { createNodes } from '../../plugins/plugin'; import { nxVersion, remixVersion } from '../../utils/versions'; import { type Schema } from './schema'; @@ -46,7 +42,6 @@ export async function remixInitGeneratorInternal(tree: Tree, options: Schema) { if (options.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/remix/plugin', createNodes, { diff --git a/packages/rollup/src/generators/init/init.spec.ts b/packages/rollup/src/generators/init/init.spec.ts index c01555d6892f8..327db9586dceb 100644 --- a/packages/rollup/src/generators/init/init.spec.ts +++ b/packages/rollup/src/generators/init/init.spec.ts @@ -1,27 +1,13 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { Tree, readJson, ProjectGraph } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { nxVersion } from '../../utils/versions'; import { rollupInitGenerator } from './init'; -let projectGraph: ProjectGraph; -jest.mock('@nx/devkit', () => ({ - ...jest.requireActual('@nx/devkit'), - createProjectGraphAsync: jest.fn().mockImplementation(async () => { - return projectGraph; - }), -})); - describe('rollupInitGenerator', () => { let tree: Tree; beforeEach(async () => { - projectGraph = { - nodes: {}, - dependencies: {}, - }; tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); }); diff --git a/packages/rollup/src/generators/init/init.ts b/packages/rollup/src/generators/init/init.ts index 59be1a26d7e3a..4419e93b9e046 100644 --- a/packages/rollup/src/generators/init/init.ts +++ b/packages/rollup/src/generators/init/init.ts @@ -1,6 +1,5 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, Tree, @@ -27,7 +26,6 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) { if (schema.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/rollup/plugin', createNodes, { diff --git a/packages/storybook/src/generators/init/init.spec.ts b/packages/storybook/src/generators/init/init.spec.ts index 36338752e5e42..3ddf5198e3767 100644 --- a/packages/storybook/src/generators/init/init.spec.ts +++ b/packages/storybook/src/generators/init/init.spec.ts @@ -1,27 +1,11 @@ -import { - readJson, - type NxJsonConfiguration, - type Tree, - ProjectGraph, -} from '@nx/devkit'; +import { readJson, type NxJsonConfiguration, type Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { initGenerator } from './init'; -let projectGraph: ProjectGraph; -jest.mock('@nx/devkit', () => ({ - ...jest.requireActual('@nx/devkit'), - createProjectGraphAsync: jest.fn().mockImplementation(async () => { - return projectGraph; - }), -})); describe('@nx/storybook:init', () => { let tree: Tree; beforeEach(() => { - projectGraph = { - nodes: {}, - dependencies: {}, - }; tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); }); diff --git a/packages/storybook/src/generators/init/init.ts b/packages/storybook/src/generators/init/init.ts index bc754beb73d24..498684ddce92e 100644 --- a/packages/storybook/src/generators/init/init.ts +++ b/packages/storybook/src/generators/init/init.ts @@ -1,6 +1,5 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, installPackagesTask, @@ -104,7 +103,6 @@ export async function initGeneratorInternal(tree: Tree, schema: Schema) { if (schema.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/storybook/plugin', createNodes, { diff --git a/packages/vite/src/generators/init/init.spec.ts b/packages/vite/src/generators/init/init.spec.ts index 66e70f9d6db7e..7d531c6ef0584 100644 --- a/packages/vite/src/generators/init/init.spec.ts +++ b/packages/vite/src/generators/init/init.spec.ts @@ -1,7 +1,6 @@ import { addDependenciesToPackageJson, NxJsonConfiguration, - ProjectGraph, readJson, readNxJson, Tree, @@ -10,14 +9,6 @@ import { import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { nxVersion } from '../../utils/versions'; -let projectGraph: ProjectGraph; -jest.mock('@nx/devkit', () => ({ - ...jest.requireActual('@nx/devkit'), - createProjectGraphAsync: jest.fn().mockImplementation(async () => { - return projectGraph; - }), -})); - import { initGenerator } from './init'; describe('@nx/vite:init', () => { @@ -25,10 +16,6 @@ describe('@nx/vite:init', () => { beforeEach(() => { tree = createTreeWithEmptyWorkspace(); - projectGraph = { - nodes: {}, - dependencies: {}, - }; }); describe('dependencies for package.json', () => { diff --git a/packages/vite/src/generators/init/init.ts b/packages/vite/src/generators/init/init.ts index 5d7fd3d60267a..4fa831342c537 100644 --- a/packages/vite/src/generators/init/init.ts +++ b/packages/vite/src/generators/init/init.ts @@ -1,5 +1,4 @@ import { - createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -62,7 +61,6 @@ export async function initGeneratorInternal( if (schema.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/vite/plugin', createNodes, { diff --git a/packages/web/src/generators/application/application.spec.ts b/packages/web/src/generators/application/application.spec.ts index 6d7a0f0de4479..af865248e0489 100644 --- a/packages/web/src/generators/application/application.spec.ts +++ b/packages/web/src/generators/application/application.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import { readProjectConfiguration, Tree } from '@nx/devkit'; diff --git a/packages/webpack/src/generators/init/init.spec.ts b/packages/webpack/src/generators/init/init.spec.ts index 5b76da0a742ed..114f329136fb6 100644 --- a/packages/webpack/src/generators/init/init.spec.ts +++ b/packages/webpack/src/generators/init/init.spec.ts @@ -1,5 +1,3 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; - import { readJson, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; diff --git a/packages/webpack/src/generators/init/init.ts b/packages/webpack/src/generators/init/init.ts index b991bf0efe32c..6b8649f4d2c0f 100644 --- a/packages/webpack/src/generators/init/init.ts +++ b/packages/webpack/src/generators/init/init.ts @@ -1,6 +1,5 @@ import { addDependenciesToPackageJson, - createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -25,7 +24,6 @@ export async function webpackInitGeneratorInternal(tree: Tree, schema: Schema) { if (schema.addPlugin) { await addPlugin( tree, - await createProjectGraphAsync(), '@nx/webpack/plugin', createNodes, { diff --git a/packages/workspace/src/generators/preset/preset.spec.ts b/packages/workspace/src/generators/preset/preset.spec.ts index b9f7e2dd8bd67..c3b811aee2f98 100644 --- a/packages/workspace/src/generators/preset/preset.spec.ts +++ b/packages/workspace/src/generators/preset/preset.spec.ts @@ -1,4 +1,4 @@ -import 'nx/src/internal-testing-utils/mock-project-graph'; +import 'nx/src/internal-testing-utils/mock-load-nx-plugin'; import { readProjectConfiguration, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';