typescript-eslint: Bug: more informatively explain when tsconfigRootDir doesn't include the cwd

Before You File a Bug Report Please Confirm You Have Done The Following…

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

typescript-estree

Playground Link

No response

Repro Code

The logic in getProjectConfigFiles compares the length of parseSettings.filePath (dirname) and parseSettings.tsconfigRootDir to stop looking for the config.

But when tsconfigRootDir is not set in the .eslintrc.js config, they can be totally different paths, for example, depending where VSCode was installed (value used for tsconfigRootDir) for tooling.

Then, it is correct to stop looking for config “only” if the parseSettings.filePath starts with tsconfigRootDir, otherwise just keep looking up to the root.

https://github.com/typescript-eslint/typescript-eslint/blob/a4b633b9ab106e5889c2372c018b1b937ff0afbb/packages/typescript-estree/src/parseSettings/getProjectConfigFiles.ts#L53

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/<rule-name>": ["error", ...<options>],
  },
  parserOptions: {
    project: true
  }
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

The config file should be found.

Actual Result

The config file is not found because it stops prematurely.

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 5.58.0
@typescript-eslint/parser 5.58.0
@typescript-eslint/scope-manager 5.58.0
@typescript-eslint/typescript-estree 5.58.0
@typescript-eslint/type-utils 5.58.0
@typescript-eslint/utils 5.58.0
TypeScript 5.0.4
ESLint 8.38.0
node 18.14.1

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Using tsconfigRootDir: process.cwd() works a bit better than tsconfigRootDir: __dirname when the .eslintrc.js is extending another config file that might be inside node_modules as a NPM package.

I’m not sure what you mean by this. As it is written - it is non-sensical because:

  • ESLint fully resolves the config completely disconnected from the existence of node_modules
    • it resolves prettier -> node_modules/eslint-config-prettier, loads it, merges the configs together and thus forgets that the config ever came from node_modules.
  • tsconfigRootDir only applies to our parser if and only if parserOptions.project != null.
  • The default value for tsconfigRootDir is process.cwd() - so explicitly setting it to process.cwd() is the same as not setting it at all.

The only solution currently is to use tsconfigRootDir: __dirname so that we know the exact path to your project root. You should be doing this regardless because:

  • it’s a performance optimisation when project: true - we know exactly when to stop traversing
  • it fixes weirdness with project: string | string[] because it means relative paths are resolved absolutely, regardless of the CWD.

I’m not exactly sure - TBH - this is really into “undefined” territory.

If you set project: true, but DON’T set tsconfigRootDir and !filePath.includes(cwd), what should we do?

I can only see two options:

  1. Crash out and say “hey you’re being weird - please set tsconfigRootDir to __dirname.”.
    • This is, in essence, what we implicitly do right now.
  2. Ignore the CWD and treat it as / (i.e. traverse all the way to the disk root)

(2) feels bad, IMO - it feels like it’s setting up the user for slow lint runs by default. I’d personally prefer just crashing out so that we can enforce a better standard.

@artola to clarify - you should never set tsconfigRootDir from within a shared config and only ever from within a .eslintrc.js file. EG:

module.exports = {
  extends: ['local-shared'],
  parserOptions: {
    tsconfigRootDir: __dirname,
  },
};

This is because, as mentioned before, the intention behind the option is that it gives us a physical root to orient ourselves relative to your project root. Setting it within a shared config that lives god knows where is entirely useless, as you’ve mentioned.

@bradzacher When using VSCodetsconfigRootDir becomes the path where VSCode is installed (e.g., /myuser/applications/vscode) and the open file is totally different (e.g., /myuser/somerepo/main.ts. Therefore the condition used to stop looking for config directory.length >= parseSettings.tsconfigRootDir.length becomes totally wrong. Should be more likely:

  } while (
      directory.startsWith(parseSettings.tsconfigRootDir)
        ?  directory.length >= parseSettings.tsconfigRootDir.length
        :   directory.length > 1
  );