diff --git a/packages/eslint/src/generators/init/init.spec.ts b/packages/eslint/src/generators/init/init.spec.ts index 69d46b6b0a..faedbaec1c 100644 --- a/packages/eslint/src/generators/init/init.spec.ts +++ b/packages/eslint/src/generators/init/init.spec.ts @@ -5,11 +5,17 @@ import { lintInitGenerator } from './init'; describe('@nx/eslint:init', () => { let tree: Tree; + let envV3: string | undefined; beforeEach(() => { + envV3 = process.env.NX_PCV3; tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); }); + afterEach(() => { + process.env.NX_PCV3 = envV3; + }); + it('should generate the global eslint config', async () => { await lintInitGenerator(tree, { linter: Linter.EsLint, @@ -97,4 +103,31 @@ describe('@nx/eslint:init', () => { ] `); }); + + it('should add @nx/eslint/plugin in subsequent step', async () => { + updateJson(tree, 'nx.json', (json) => { + json.namedInputs ??= {}; + json.namedInputs.production = ['default']; + return json; + }); + + await lintInitGenerator(tree, {}); + expect( + readJson(tree, 'nx.json').plugins + ).not.toBeDefined(); + + process.env.NX_PCV3 = 'true'; + await lintInitGenerator(tree, {}); + expect(readJson(tree, 'nx.json').plugins) + .toMatchInlineSnapshot(` + [ + { + "options": { + "targetName": "lint", + }, + "plugin": "@nx/eslint/plugin", + }, + ] + `); + }); }); diff --git a/packages/eslint/src/generators/init/init.ts b/packages/eslint/src/generators/init/init.ts index c155b30c4e..10e8514437 100644 --- a/packages/eslint/src/generators/init/init.ts +++ b/packages/eslint/src/generators/init/init.ts @@ -18,6 +18,7 @@ import { Linter } from '../utils/linter'; import { findEslintFile } from '../utils/eslint-file'; import { getGlobalEsLintConfiguration } from './global-eslint-config'; import { EslintPluginOptions } from '../../plugins/plugin'; +import { hasEslintPlugin } from '../utils/plugin'; export interface LinterInitOptions { linter?: Linter; @@ -94,7 +95,16 @@ function updateVSCodeExtensions(tree: Tree) { * Initializes ESLint configuration in a workspace and adds necessary dependencies. */ function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback { - if (findEslintFile(tree)) { + const addPlugins = process.env.NX_PCV3 === 'true'; + const hasPlugin = hasEslintPlugin(tree); + const rootEslintFile = findEslintFile(tree); + + if (rootEslintFile && addPlugins && !hasPlugin) { + addPlugin(tree); + return () => {}; + } + + if (rootEslintFile) { return () => {}; } @@ -111,7 +121,6 @@ function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback { updateProductionFileset(tree); - const addPlugins = process.env.NX_PCV3 === 'true'; if (addPlugins) { addPlugin(tree); } else { diff --git a/packages/eslint/src/generators/lint-project/lint-project.ts b/packages/eslint/src/generators/lint-project/lint-project.ts index e7257c53d2..7813fdd956 100644 --- a/packages/eslint/src/generators/lint-project/lint-project.ts +++ b/packages/eslint/src/generators/lint-project/lint-project.ts @@ -35,6 +35,7 @@ import { baseEsLintConfigFile, baseEsLintFlatConfigFile, } from '../../utils/config-file'; +import { hasEslintPlugin } from '../utils/plugin'; interface LintProjectOptions { project: string; @@ -73,12 +74,7 @@ export async function lintProjectGenerator( lintFilePatterns.push(`{projectRoot}/package.json`); } - const nxJson = readNxJson(tree); - const hasPlugin = nxJson.plugins?.some((p) => - typeof p === 'string' - ? p === '@nx/eslint/plugin' - : p.plugin === '@nx/eslint/plugin' - ); + const hasPlugin = hasEslintPlugin(tree); if (hasPlugin) { if ( lintFilePatterns && diff --git a/packages/eslint/src/generators/utils/plugin.ts b/packages/eslint/src/generators/utils/plugin.ts new file mode 100644 index 0000000000..a98fb12984 --- /dev/null +++ b/packages/eslint/src/generators/utils/plugin.ts @@ -0,0 +1,10 @@ +import { Tree, readNxJson } from '@nx/devkit'; + +export function hasEslintPlugin(tree: Tree): boolean { + const nxJson = readNxJson(tree); + return nxJson.plugins?.some((p) => + typeof p === 'string' + ? p === '@nx/eslint/plugin' + : p.plugin === '@nx/eslint/plugin' + ); +}