Skip to content

Commit

Permalink
fix(linter): only set flat config env for eslint v9+ (#25189)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

The `ESLINT_USE_FLAT_CONFIG` env flag was set unnecessarily even if the
installed version of ESLint is < 9.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The `ESLINT_USE_FLAT_CONFIG` env flag is only set if the value is
different than the default in the installed version of ESLint.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored May 22, 2024
1 parent 80702b5 commit dc5f91b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"eslint": "^8.0.0 || ^9.0.0",
"semver": "^7.5.3",
"tslib": "^2.3.0",
"typescript": "~5.4.2"
},
Expand Down
27 changes: 0 additions & 27 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": ".",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -183,9 +180,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/my-app",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -227,9 +221,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/my-app",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -343,9 +334,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/my-app",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand All @@ -371,9 +359,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "libs/my-lib",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -459,9 +444,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/my-app",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand All @@ -488,9 +470,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "libs/my-lib",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -534,9 +513,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/myapp",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down Expand Up @@ -585,9 +561,6 @@ describe('@nx/eslint/plugin', () => {
],
"options": {
"cwd": "apps/myapp/nested/mylib",
"env": {
"ESLINT_USE_FLAT_CONFIG": "false",
},
},
"outputs": [
"{options.outputFile}",
Expand Down
16 changes: 13 additions & 3 deletions packages/eslint/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
isFlatConfig,
} from '../utils/config-file';
import { resolveESLintClass } from '../utils/resolve-eslint-class';
import { gte } from 'semver';

export interface EslintPluginOptions {
targetName?: string;
Expand Down Expand Up @@ -67,6 +68,7 @@ export const createNodes: CreateNodes<EslintPluginOptions> = [
const excludePatterns = dedupedProjectRoots.map((root) => `${root}/**/*`);

const ESLint = await resolveESLintClass(isFlatConfig(configFilePath));
const eslintVersion = ESLint.version;
const childProjectRoots = new Set<string>();

await Promise.all(
Expand Down Expand Up @@ -101,6 +103,7 @@ export const createNodes: CreateNodes<EslintPluginOptions> = [
projects: getProjectsUsingESLintConfig(
configFilePath,
uniqueChildProjectRoots,
eslintVersion,
options,
context
),
Expand All @@ -111,6 +114,7 @@ export const createNodes: CreateNodes<EslintPluginOptions> = [
function getProjectsUsingESLintConfig(
configFilePath: string,
childProjectRoots: string[],
eslintVersion: string,
options: EslintPluginOptions,
context: CreateNodesContext
): CreateNodesResult['projects'] {
Expand Down Expand Up @@ -142,6 +146,7 @@ function getProjectsUsingESLintConfig(
projects[projectRoot] = {
targets: buildEslintTargets(
eslintConfigs,
eslintVersion,
projectRoot,
context.workspaceRoot,
options,
Expand All @@ -155,6 +160,7 @@ function getProjectsUsingESLintConfig(

function buildEslintTargets(
eslintConfigs: string[],
eslintVersion: string,
projectRoot: string,
workspaceRoot: string,
options: EslintPluginOptions,
Expand Down Expand Up @@ -191,9 +197,13 @@ function buildEslintTargets(

// Always set the environment variable to ensure that the ESLint CLI can run on eslint v8 and v9
const useFlatConfig = eslintConfigs.some((config) => isFlatConfig(config));
targetConfig.options.env = {
ESLINT_USE_FLAT_CONFIG: useFlatConfig ? 'true' : 'false',
};
// Flat config is default for 9.0.0+
const defaultSetting = gte(eslintVersion, '9.0.0');
if (useFlatConfig !== defaultSetting) {
targetConfig.options.env = {
ESLINT_USE_FLAT_CONFIG: useFlatConfig ? 'true' : 'false',
};
}

targets[options.targetName] = targetConfig;

Expand Down

0 comments on commit dc5f91b

Please sign in to comment.