Skip to content

Commit

Permalink
fix(linter): flat config should always set path to config when using …
Browse files Browse the repository at this point in the history
…API (#20867)
  • Loading branch information
meeroslav authored Dec 22, 2023
1 parent d819e6c commit 148ddfb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/eslint/src/executors/lint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 20 additions & 10 deletions packages/eslint/src/executors/lint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
});
}
Expand Down

0 comments on commit 148ddfb

Please sign in to comment.