diff --git a/packages/eslint/src/executors/lint/lint.impl.spec.ts b/packages/eslint/src/executors/lint/lint.impl.spec.ts index 67d0ce07b7237..1167155779176 100644 --- a/packages/eslint/src/executors/lint/lint.impl.spec.ts +++ b/packages/eslint/src/executors/lint/lint.impl.spec.ts @@ -683,10 +683,10 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this jest.spyOn(fs, 'existsSync').mockReturnValue(true); await lintExecutor(createValidRunBuilderOptions(), mockContext); expect(mockResolveAndInstantiateESLint).toHaveBeenCalledWith( - undefined, + '/root/apps/proj/eslint.config.js', { lintFilePatterns: [], - eslintConfig: null, + eslintConfig: 'apps/proj/eslint.config.js', fix: true, cache: true, cacheLocation: 'cacheLocation1/proj', diff --git a/packages/eslint/src/executors/lint/lint.impl.ts b/packages/eslint/src/executors/lint/lint.impl.ts index 10da5c87b3c13..62598177bec8a 100644 --- a/packages/eslint/src/executors/lint/lint.impl.ts +++ b/packages/eslint/src/executors/lint/lint.impl.ts @@ -22,20 +22,14 @@ export default async function run( process.chdir(systemRoot); const projectName = context.projectName || ''; + const projectRoot = + context.projectsConfigurations.projects[context.projectName].root; const printInfo = options.format && !options.silent; if (printInfo) { console.info(`\nLinting ${JSON.stringify(projectName)}...`); } - /** - * We want users to have the option of not specifying the config path, and let - * eslint automatically resolve the `.eslintrc.json` files in each folder. - */ - let eslintConfigPath = options.eslintConfig - ? resolve(systemRoot, options.eslintConfig) - : undefined; - options.cacheLocation = options.cacheLocation ? joinPathFragments(options.cacheLocation, projectName) : undefined; @@ -52,6 +46,23 @@ export default async function run( joinPathFragments(workspaceRoot, 'eslint.config.js') ); + // while standard eslint uses by default closest config to the file, if otherwise not specified, + // the flat config would always use the root config, so we need to explicitly set it to the local one + if (hasFlatConfig && !normalizedOptions.eslintConfig) { + const eslintConfigPath = joinPathFragments(projectRoot, 'eslint.config.js'); + if (existsSync(eslintConfigPath)) { + normalizedOptions.eslintConfig = eslintConfigPath; + } + } + + /** + * We want users to have the option of not specifying the config path, and let + * eslint automatically resolve the `.eslintrc.json` files in each folder. + */ + let eslintConfigPath = normalizedOptions.eslintConfig + ? resolve(systemRoot, normalizedOptions.eslintConfig) + : undefined; + const { eslint, ESLint } = await resolveAndInstantiateESLint( eslintConfigPath, normalizedOptions, @@ -89,8 +100,7 @@ export default async function run( (pattern) => { return interpolate(pattern, { workspaceRoot: '', - projectRoot: - context.projectsConfigurations.projects[context.projectName].root, + projectRoot, projectName: context.projectName, }); }