Skip to content

Commit 6f9f57e

Browse files
committed
fix(linter): install @eslint/eslintrc package as necessary (#29933)
When flat compat is necessary, we don't ensure that `@eslint/eslintrc` is installed, even though we import it. Depending on the package manager and hoisting, it may still work, but as we see with pnpm v10, it is not working without an explicit dependency in `package.json`. <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior Some app generators are broken is we use eslint compat. ## Expected Behavior App generators should work. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #29845
1 parent e50f4c2 commit 6f9f57e

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

packages/eslint/src/generators/utils/eslint-file.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,45 @@ describe('@nx/eslint:lint-file', () => {
120120
]);
121121
});
122122

123+
it('should install necessary dependencies', () => {
124+
// mock eslint version
125+
jest.spyOn(devkitInternals, 'readModulePackageJson').mockReturnValue({
126+
packageJson: { name: 'eslint', version: '9.0.0' },
127+
path: '',
128+
});
129+
tree.write('eslint.config.cjs', 'module.exports = {};');
130+
tree.write(
131+
'apps/demo/eslint.config.cjs',
132+
`const baseConfig = require("../../eslint.config.cjs");
133+
134+
module.exports = [
135+
...baseConfig,
136+
{
137+
files: [
138+
"**/*.ts",
139+
"**/*.tsx",
140+
"**/*.js",
141+
"**/*.jsx"
142+
],
143+
rules: {}
144+
},
145+
];`
146+
);
147+
148+
addExtendsToLintConfig(tree, 'apps/demo', {
149+
name: 'plugin:playwright/recommend',
150+
needCompatFixup: true,
151+
});
152+
153+
expect(readJson(tree, 'package.json').devDependencies)
154+
.toMatchInlineSnapshot(`
155+
{
156+
"@eslint/compat": "^1.1.1",
157+
"@eslint/eslintrc": "^2.1.1",
158+
}
159+
`);
160+
});
161+
123162
it('should add extends to flat config', () => {
124163
tree.write('eslint.config.cjs', 'module.exports = {};');
125164
tree.write(

packages/eslint/src/generators/utils/eslint-file.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import {
2020
useFlatConfig,
2121
} from '../../utils/flat-config';
2222
import { getInstalledEslintVersion } from '../../utils/version-utils';
23-
import { eslint9__eslintVersion, eslintCompat } from '../../utils/versions';
23+
import {
24+
eslint9__eslintVersion,
25+
eslintCompat,
26+
eslintrcVersion,
27+
} from '../../utils/versions';
2428
import {
2529
addBlockToFlatConfigExport,
2630
addFlatCompatToFlatConfig,
@@ -493,13 +497,19 @@ export function addExtendsToLintConfig(
493497
return addDependenciesToPackageJson(
494498
tree,
495499
{},
496-
{ '@eslint/compat': eslintCompat },
500+
{ '@eslint/compat': eslintCompat, '@eslint/eslintrc': eslintrcVersion },
497501
undefined,
498502
true
499503
);
500504
}
501505

502-
return () => {};
506+
return addDependenciesToPackageJson(
507+
tree,
508+
{},
509+
{ '@eslint/eslintrc': eslintrcVersion },
510+
undefined,
511+
true
512+
);
503513
} else {
504514
const plugins = (Array.isArray(plugin) ? plugin : [plugin]).map((p) =>
505515
typeof p === 'string' ? p : p.name

0 commit comments

Comments
 (0)