Skip to content

fix(linter): speed up inferred plugin node processing #31281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 38 additions & 37 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,43 +518,45 @@ describe('@nx/eslint/plugin', () => {
`);
});

it('should not create nodes for nested projects without a root level eslint config when all files are ignored (.eslintignore)', async () => {
createFiles({
'apps/my-app/.eslintrc.json': `{}`,
'apps/my-app/.eslintignore': `**/*`,
'apps/my-app/project.json': `{}`,
'apps/my-app/index.ts': `console.log('hello world')`,
'libs/my-lib/.eslintrc.json': `{}`,
'libs/my-lib/.eslintignore': `**/*`,
'libs/my-lib/project.json': `{}`,
'libs/my-lib/index.ts': `console.log('hello world')`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, { targetName: 'lint' })
).toMatchInlineSnapshot(`
{
"projects": {},
}
`);
});
// This is intentionally disabled, since we should always create a node for project that contains eslint config
// it('should not create nodes for nested projects without a root level eslint config when all files are ignored (.eslintignore)', async () => {
// createFiles({
// 'apps/my-app/.eslintrc.json': `{}`,
// 'apps/my-app/.eslintignore': `**/*`,
// 'apps/my-app/project.json': `{}`,
// 'apps/my-app/index.ts': `console.log('hello world')`,
// 'libs/my-lib/.eslintrc.json': `{}`,
// 'libs/my-lib/.eslintignore': `**/*`,
// 'libs/my-lib/project.json': `{}`,
// 'libs/my-lib/index.ts': `console.log('hello world')`,
// });
// expect(
// await invokeCreateNodesOnMatchingFiles(context, { targetName: 'lint' })
// ).toMatchInlineSnapshot(`
// {
// "projects": {},
// }
// `);
// });

it('should not create nodes for nested projects without a root level eslint config when all files are ignored (ignorePatterns in .eslintrc.json)', async () => {
createFiles({
'apps/my-app/.eslintrc.json': `{ "ignorePatterns": ["**/*"] }`,
'apps/my-app/project.json': `{}`,
'apps/my-app/index.ts': `console.log('hello world')`,
'libs/my-lib/.eslintrc.json': `{ "ignorePatterns": ["**/*"] }`,
'libs/my-lib/project.json': `{}`,
'libs/my-lib/index.ts': `console.log('hello world')`,
});
expect(
await invokeCreateNodesOnMatchingFiles(context, { targetName: 'lint' })
).toMatchInlineSnapshot(`
{
"projects": {},
}
`);
});
// This is intentionally disabled, since we should always create a node for project that contains eslint config
// it('should not create nodes for nested projects without a root level eslint config when all files are ignored (ignorePatterns in .eslintrc.json)', async () => {
// createFiles({
// 'apps/my-app/.eslintrc.json': `{ "ignorePatterns": ["**/*"] }`,
// 'apps/my-app/project.json': `{}`,
// 'apps/my-app/index.ts': `console.log('hello world')`,
// 'libs/my-lib/.eslintrc.json': `{ "ignorePatterns": ["**/*"] }`,
// 'libs/my-lib/project.json': `{}`,
// 'libs/my-lib/index.ts': `console.log('hello world')`,
// });
// expect(
// await invokeCreateNodesOnMatchingFiles(context, { targetName: 'lint' })
// ).toMatchInlineSnapshot(`
// {
// "projects": {},
// }
// `);
// });
});

describe('root eslint config and nested eslint configs', () => {
Expand Down Expand Up @@ -721,7 +723,6 @@ describe('@nx/eslint/plugin', () => {
it('should handle multiple levels of nesting and ignored files correctly', async () => {
createFiles({
'.eslintrc.json': '{ "root": true, "ignorePatterns": ["**/*"] }',
'apps/myapp/.eslintrc.json': '{ "extends": "../../.eslintrc.json" }', // no lintable files, don't create task
'apps/myapp/project.json': '{}',
'apps/myapp/index.ts': 'console.log("hello world")',
'apps/myapp/nested/mylib/.eslintrc.json': JSON.stringify({
Expand Down
33 changes: 22 additions & 11 deletions packages/eslint/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context';
import { gte } from 'semver';
import type { ESLint as ESLintType } from 'eslint';
import {
baseEsLintConfigFile,
BASE_ESLINT_CONFIG_FILENAMES,
Expand Down Expand Up @@ -186,6 +187,7 @@ const internalCreateNodes = async (
};

const internalCreateNodesV2 = async (
ESLint: typeof ESLintType,
configFilePath: string,
options: EslintPluginOptions,
context: CreateNodesContextV2,
Expand All @@ -195,10 +197,6 @@ const internalCreateNodesV2 = async (
hashByRoot: Map<string, string>
): Promise<CreateNodesResult> => {
const configDir = dirname(configFilePath);

const ESLint = await resolveESLintClass({
useFlatConfigOverrideVal: isFlatConfig(configFilePath),
});
const eslintVersion = ESLint.version;

const projects: CreateNodesResult['projects'] = {};
Expand All @@ -212,15 +210,21 @@ const internalCreateNodesV2 = async (
return;
}

const eslint = new ESLint({
cwd: join(context.workspaceRoot, projectRoot),
});
let hasNonIgnoredLintableFiles = false;
for (const file of lintableFilesPerProjectRoot.get(projectRoot) ?? []) {
if (!(await eslint.isPathIgnored(join(context.workspaceRoot, file)))) {
hasNonIgnoredLintableFiles = true;
break;
if (configDir !== projectRoot || projectRoot === '.') {
const eslint = new ESLint({
cwd: join(context.workspaceRoot, projectRoot),
});
for (const file of lintableFilesPerProjectRoot.get(projectRoot) ?? []) {
if (
!(await eslint.isPathIgnored(join(context.workspaceRoot, file)))
) {
hasNonIgnoredLintableFiles = true;
break;
}
}
} else {
hasNonIgnoredLintableFiles = true;
}

if (!hasNonIgnoredLintableFiles) {
Expand Down Expand Up @@ -286,9 +290,16 @@ export const createNodesV2: CreateNodesV2<EslintPluginOptions> = [
projectRoots.map((r, i) => [r, hashes[i]])
);
try {
if (eslintConfigFiles.length === 0) {
return [];
}
const ESLint = await resolveESLintClass({
useFlatConfigOverrideVal: isFlatConfig(eslintConfigFiles[0]),
});
return await createNodesFromFiles(
(configFile, options, context) =>
internalCreateNodesV2(
ESLint,
configFile,
options,
context,
Expand Down
Loading