From 6ba03c7e3120056eb4a20a4f7e39ed69f83c58c9 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 14 May 2024 18:01:30 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"fix(core):=20addPlugin=20should=20not?= =?UTF-8?q?=20conflict=20on=20project.json=20targ=E2=80=A6=20(#23391)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …ets (#23264)" This reverts commit 85c89160 ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- .../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 +- .../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 +- 58 files changed, 239 insertions(+), 291 deletions(-) delete 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 f0b7c8d612c2c..514129d1c5b6b 100644 --- a/packages/angular/src/generators/application/application.spec.ts +++ b/packages/angular/src/generators/application/application.spec.ts @@ -1,5 +1,3 @@ -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 6ac46bee0c621..0a318c4b0c246 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,5 +1,3 @@ -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 9130e19eaa3a0..ab04beb3eaa2e 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 083210fb64849..5dfa64c650a92 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 34359a5c444fa..df0332ea9fa92 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 f159db6447b66..7148489abe09b 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 bbea30387b050..72b2e3ea32d4c 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 95b153a0ca750..8e2fbb78685ae 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 e52338334287e..87e89d3007395 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 009844cf90c52..5a7491a203e0e 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 3d8f829150179..fb18673bf58f2 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 079afc6491a51..7d67280605dba 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 4d2ed4e01f63f..5f07c2ebfdd03 100644 --- a/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts +++ b/packages/angular/src/generators/storybook-configuration/storybook-configuration.spec.ts @@ -1,5 +1,3 @@ -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 1b03930a61a4e..fc298a45d93ab 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 d2e65afe5a213..707a28f9edb71 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 f1d08de0c1504..9be54c6387f12 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 f4193b02d6e57..46b5d45687dfb 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 9e70d26c354b0..3c5967bd06bcf 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 555b0af4a42c5..5da3ccf68b663 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,5 +1,3 @@ -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 35a404f7af7e3..8635ee5f9664c 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, false); + await addPlugin(tree, projectGraph, false); } const nxJson = readNxJson(tree); diff --git a/packages/cypress/src/generators/init/init.ts b/packages/cypress/src/generators/init/init.ts index a7ed8ce00c7b1..f4d633d87049d 100644 --- a/packages/cypress/src/generators/init/init.ts +++ b/packages/cypress/src/generators/init/init.ts @@ -1,17 +1,23 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, + ProjectGraph, readNxJson, removeDependenciesFromPackageJson, runTasksInSerial, Tree, updateNxJson, } from '@nx/devkit'; -import { addPlugin as _addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { + addPlugin as _addPlugin, + generateCombinations, +} 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); @@ -54,9 +60,14 @@ function updateDependencies(tree: Tree, options: Schema) { return runTasksInSerial(...tasks); } -export function addPlugin(tree: Tree, updatePackageScripts: boolean) { +export function addPlugin( + tree: Tree, + graph: ProjectGraph, + updatePackageScripts: boolean +) { return _addPlugin( tree, + graph, '@nx/cypress/plugin', createNodes, { @@ -107,7 +118,11 @@ export async function cypressInitGeneratorInternal( nxJson.useInferencePlugins !== false; if (options.addPlugin) { - await addPlugin(tree, options.updatePackageScripts); + await addPlugin( + tree, + await createProjectGraphAsync(), + options.updatePackageScripts + ); } else { setupE2ETargetDefaults(tree); } diff --git a/packages/detox/src/generators/init/init.ts b/packages/detox/src/generators/init/init.ts index 1340eb644088c..8753d6ecf9836 100644 --- a/packages/detox/src/generators/init/init.ts +++ b/packages/detox/src/generators/init/init.ts @@ -1,5 +1,6 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -7,8 +8,11 @@ import { runTasksInSerial, Tree, } from '@nx/devkit'; -import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../plugins/plugin'; +import { + addPlugin, + generateCombinations, +} from '@nx/devkit/src/utils/add-plugin'; +import { createNodes, DetoxPluginOptions } from '../../plugins/plugin'; import { detoxVersion, nxVersion } from '../../utils/versions'; import { Schema } from './schema'; @@ -34,6 +38,7 @@ 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 034a84210c661..6a8135d929330 100644 --- a/packages/devkit/src/utils/add-plugin.spec.ts +++ b/packages/devkit/src/utils/add-plugin.spec.ts @@ -1,50 +1,17 @@ -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 () => { @@ -53,6 +20,39 @@ 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,148 +84,41 @@ describe('addPlugin', () => { }); describe('adding the plugin', () => { - 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': '', - }); + 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 = {}; - // ACT await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, { - targetName: ['build', 'build1'], + targetName: ['build', 'build1', 'build2'], }, true ); - // ASSERT - expect( - readJson(tree, 'nx.json').plugins.find( - (p) => p.plugin === '@nx/next/plugin' - ) - ).toEqual({ + expect(readJson(tree, 'nx.json').plugins).toContainEqual({ plugin: '@nx/next/plugin', options: { - 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', + targetName: 'build2', }, }); }); }); it('should throw an error if no non-conflicting options are provided', async () => { - 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': '', - }); + graph.nodes.app1.data.targets.build = {}; try { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -254,6 +147,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -291,6 +185,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -339,6 +234,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/cypress/plugin', createNodes, @@ -363,6 +259,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -403,6 +300,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -444,6 +342,7 @@ describe('addPlugin', () => { await addPlugin( tree, + graph, '@nx/next/plugin', createNodes, @@ -485,6 +384,7 @@ 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 627e94b6b22c1..79fcf70e0743b 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 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,10 +15,6 @@ import { } from 'nx/src/devkit-exports'; import { LoadedNxPlugin, - loadNxPlugins, - isCompatibleTarget, - mergeTargetConfigurations, - deepEquals, ProjectConfigurationsError, retrieveProjectConfigurations, } from 'nx/src/devkit-internals'; @@ -29,6 +25,7 @@ import { */ export async function addPlugin( tree: Tree, + graph: ProjectGraph, pluginName: string, createNodesTuple: CreateNodes, options: Partial< @@ -36,12 +33,11 @@ export async function addPlugin( >, shouldUpdatePackageJsonScripts: boolean ): Promise { + const graphNodes = Object.values(graph.nodes); const nxJson = readNxJson(tree); let pluginOptions: PluginOptions; - let projConfigsWithExistingNxJsonPlugins: ConfigurationResult; - let projConfigsWithNewPlugin: ConfigurationResult; - let projConfigsWithCorePlugins: ConfigurationResult; + let projConfigs: ConfigurationResult; const combinations = generateCombinations(options); optionsLoop: for (const _pluginOptions of combinations) { pluginOptions = _pluginOptions as PluginOptions; @@ -58,21 +54,8 @@ export async function addPlugin( return; } global.NX_GRAPH_CREATION = true; - - const [nxJsonPlugins] = await loadNxPlugins( - nxJson.plugins, - tree.root, - true - ); - projConfigsWithExistingNxJsonPlugins = - await retrieveProjectConfigurationsWithPartialResult( - nxJsonPlugins, - tree.root, - nxJson - ); - - projConfigsWithNewPlugin = - await retrieveProjectConfigurationsWithPartialResult( + try { + projConfigs = await retrieveProjectConfigurations( [ new LoadedNxPlugin( { @@ -88,38 +71,27 @@ 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; - const [corePlugins] = await loadNxPlugins([], tree.root); - projConfigsWithCorePlugins = - await retrieveProjectConfigurationsWithPartialResult( - corePlugins, - tree.root, - nxJson + for (const projConfig of Object.values(projConfigs.projects)) { + const node = graphNodes.find( + (node) => node.data.root === projConfig.root ); - global.NX_GRAPH_CREATION = false; + if (!node) { + continue; + } - for (const projConfig of Object.values(projConfigsWithNewPlugin.projects)) { for (const targetName in projConfig.targets) { - 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] - ) - )) - ) { + if (node.data.targets[targetName]) { // Conflicting Target Name, check the next one pluginOptions = null; continue optionsLoop; @@ -144,7 +116,7 @@ export async function addPlugin( updateNxJson(tree, nxJson); if (shouldUpdatePackageJsonScripts) { - updatePackageScripts(tree, projConfigsWithNewPlugin); + updatePackageScripts(tree, projConfigs); } } @@ -381,26 +353,3 @@ 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 4d4126b6af1fe..c00e45fe28f70 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,5 +1,3 @@ -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 552b65d8f363c..bcf161b287aef 100644 --- a/packages/eslint/src/generators/init/init.spec.ts +++ b/packages/eslint/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 d756eddcaff35..dfe622ea8cae5 100644 --- a/packages/eslint/src/generators/init/init.ts +++ b/packages/eslint/src/generators/init/init.ts @@ -61,6 +61,8 @@ export async function initEsLint( const hasPlugin = hasEslintPlugin(tree); const rootEslintFile = findEslintFile(tree); + const graph = await createProjectGraphAsync(); + const lintTargetNames = [ 'lint', 'eslint:lint', @@ -73,6 +75,7 @@ export async function initEsLint( if (rootEslintFile && options.addPlugin && !hasPlugin) { await addPlugin( tree, + graph, '@nx/eslint/plugin', createNodes, { @@ -93,6 +96,7 @@ 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 64efd74987ba6..b40251d9a146d 100644 --- a/packages/expo/src/generators/init/init.spec.ts +++ b/packages/expo/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 b3290d32a9d88..da4a087481624 100644 --- a/packages/expo/src/generators/init/init.ts +++ b/packages/expo/src/generators/init/init.ts @@ -38,6 +38,7 @@ 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 eb59d466d95da..6841ce4218131 100644 --- a/packages/jest/src/generators/init/init.spec.ts +++ b/packages/jest/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 77772298daa0b..78c6fed32061d 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -103,6 +103,7 @@ 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 094e47f8ddb3a..a0b3497ed2420 100644 --- a/packages/next/src/generators/init/init.spec.ts +++ b/packages/next/src/generators/init/init.spec.ts @@ -1,12 +1,23 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; -import { readJson, Tree } from '@nx/devkit'; +import { readJson, Tree, ProjectGraph } 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 4f1e7be26a621..7608702ef4b0b 100644 --- a/packages/next/src/generators/init/init.ts +++ b/packages/next/src/generators/init/init.ts @@ -55,6 +55,7 @@ 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 2ccad9b543f28..3d130a7a8ebad 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 814777b34d100..b5885a2deecab 100644 --- a/packages/nuxt/src/generators/init/init.spec.ts +++ b/packages/nuxt/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 f72542e33fc94..819b22f609a5c 100644 --- a/packages/nuxt/src/generators/init/init.ts +++ b/packages/nuxt/src/generators/init/init.ts @@ -8,6 +8,7 @@ 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 847793e152004..1deeefad1430b 100644 --- a/packages/nx/src/devkit-internals.ts +++ b/packages/nx/src/devkit-internals.ts @@ -4,16 +4,12 @@ * 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, - isCompatibleTarget, -} from './project-graph/utils/project-configuration-utils'; +export { readProjectConfigurationsFromRootMap } from './project-graph/utils/project-configuration-utils'; export { splitTarget } from './utils/split-target'; export { combineOptionsForExecutor } from './utils/params'; export { sortObjectByKeys } from './utils/object-sort'; @@ -27,9 +23,6 @@ export { findProjectForPath, } from './project-graph/utils/find-project-for-path'; export { retrieveProjectConfigurations } from './project-graph/utils/retrieve-workspace-files'; -export { - LoadedNxPlugin, - loadNxPlugins, -} from './project-graph/plugins/internal-api'; +export { LoadedNxPlugin } 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 deleted file mode 100644 index 90fd25f7d51e6..0000000000000 --- a/packages/nx/src/internal-testing-utils/mock-load-nx-plugin.ts +++ /dev/null @@ -1,6 +0,0 @@ -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 83718d392ece6..874f750d66f7f 100644 --- a/packages/nx/src/project-graph/plugins/internal-api.ts +++ b/packages/nx/src/project-graph/plugins/internal-api.ts @@ -98,8 +98,7 @@ export const nxPluginCache: Map< export async function loadNxPlugins( plugins: PluginConfiguration[], - root = workspaceRoot, - skipDefaultPlugins = false + root = workspaceRoot ): Promise<[LoadedNxPlugin[], () => void]> { const result: Promise[] = []; @@ -108,9 +107,7 @@ export async function loadNxPlugins( ? loadNxPluginInIsolation : loadNxPlugin; - plugins = skipDefaultPlugins - ? plugins ?? [] - : await normalizePlugins(plugins, root); + 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 893dbc4bdb5ba..d61a11cef9a4b 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -16,7 +16,6 @@ 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 8c1edc2f9cf65..c4dea0c653753 100644 --- a/packages/playwright/src/generators/init/init.spec.ts +++ b/packages/playwright/src/generators/init/init.spec.ts @@ -1,12 +1,24 @@ -import { readNxJson, Tree, updateNxJson } from '@nx/devkit'; +import { ProjectGraph, 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(); }); @@ -31,7 +43,7 @@ describe('@nx/playwright:init', () => { it('should not overwrite existing plugins', async () => { updateNxJson(tree, { - plugins: ['@nx/vite/plugin'], + plugins: ['foo'], }); await initGenerator(tree, { skipFormat: true, @@ -41,7 +53,7 @@ describe('@nx/playwright:init', () => { const nxJson = readNxJson(tree); expect(nxJson.plugins).toMatchInlineSnapshot(` [ - "@nx/vite/plugin", + "foo", { "options": { "targetName": "e2e", diff --git a/packages/playwright/src/generators/init/init.ts b/packages/playwright/src/generators/init/init.ts index a75695deff38a..ae90ae3a95556 100644 --- a/packages/playwright/src/generators/init/init.ts +++ b/packages/playwright/src/generators/init/init.ts @@ -47,6 +47,7 @@ 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 14db3d844993e..ca2343713d156 100644 --- a/packages/react-native/src/generators/application/application.spec.ts +++ b/packages/react-native/src/generators/application/application.spec.ts @@ -1,3 +1,5 @@ +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 85ccf173c18c1..2e0939bc08788 100644 --- a/packages/react-native/src/generators/init/init.spec.ts +++ b/packages/react-native/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 0a094797a3bc1..be1331f55cf0e 100644 --- a/packages/react-native/src/generators/init/init.ts +++ b/packages/react-native/src/generators/init/init.ts @@ -1,5 +1,6 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -40,6 +41,7 @@ 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 a728645e03b8e..b834227e75145 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import { diff --git a/packages/remix/src/generators/init/init.spec.ts b/packages/remix/src/generators/init/init.spec.ts index 48fc5639d9bf1..92b66265770de 100644 --- a/packages/remix/src/generators/init/init.spec.ts +++ b/packages/remix/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 745c87e08cfa6..80e7214b18b66 100644 --- a/packages/remix/src/generators/init/init.ts +++ b/packages/remix/src/generators/init/init.ts @@ -5,8 +5,12 @@ import { readNxJson, addDependenciesToPackageJson, runTasksInSerial, + createProjectGraphAsync, } from '@nx/devkit'; -import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { + addPlugin, + generateCombinations, +} from '@nx/devkit/src/utils/add-plugin'; import { createNodes } from '../../plugins/plugin'; import { nxVersion, remixVersion } from '../../utils/versions'; import { type Schema } from './schema'; @@ -42,6 +46,7 @@ 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 327db9586dceb..c01555d6892f8 100644 --- a/packages/rollup/src/generators/init/init.spec.ts +++ b/packages/rollup/src/generators/init/init.spec.ts @@ -1,13 +1,27 @@ +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 4419e93b9e046..59be1a26d7e3a 100644 --- a/packages/rollup/src/generators/init/init.ts +++ b/packages/rollup/src/generators/init/init.ts @@ -1,5 +1,6 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, Tree, @@ -26,6 +27,7 @@ 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 3ddf5198e3767..36338752e5e42 100644 --- a/packages/storybook/src/generators/init/init.spec.ts +++ b/packages/storybook/src/generators/init/init.spec.ts @@ -1,11 +1,27 @@ -import { readJson, type NxJsonConfiguration, type Tree } from '@nx/devkit'; +import { + readJson, + type NxJsonConfiguration, + type Tree, + ProjectGraph, +} 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 498684ddce92e..bc754beb73d24 100644 --- a/packages/storybook/src/generators/init/init.ts +++ b/packages/storybook/src/generators/init/init.ts @@ -1,5 +1,6 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, installPackagesTask, @@ -103,6 +104,7 @@ 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 7d531c6ef0584..66e70f9d6db7e 100644 --- a/packages/vite/src/generators/init/init.spec.ts +++ b/packages/vite/src/generators/init/init.spec.ts @@ -1,6 +1,7 @@ import { addDependenciesToPackageJson, NxJsonConfiguration, + ProjectGraph, readJson, readNxJson, Tree, @@ -9,6 +10,14 @@ 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', () => { @@ -16,6 +25,10 @@ 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 4fa831342c537..5d7fd3d60267a 100644 --- a/packages/vite/src/generators/init/init.ts +++ b/packages/vite/src/generators/init/init.ts @@ -1,4 +1,5 @@ import { + createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -61,6 +62,7 @@ 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 af865248e0489..6d7a0f0de4479 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; 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 114f329136fb6..5b76da0a742ed 100644 --- a/packages/webpack/src/generators/init/init.spec.ts +++ b/packages/webpack/src/generators/init/init.spec.ts @@ -1,3 +1,5 @@ +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 6b8649f4d2c0f..b991bf0efe32c 100644 --- a/packages/webpack/src/generators/init/init.ts +++ b/packages/webpack/src/generators/init/init.ts @@ -1,5 +1,6 @@ import { addDependenciesToPackageJson, + createProjectGraphAsync, formatFiles, GeneratorCallback, readNxJson, @@ -24,6 +25,7 @@ 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 7ff5c1fea99ac..78eb588458e32 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-load-nx-plugin'; +import 'nx/src/internal-testing-utils/mock-project-graph'; import { readProjectConfiguration, Tree } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';