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.
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)
I’m not sure what you mean by this. As it is written - it is non-sensical because:
node_modulesprettier->node_modules/eslint-config-prettier, loads it, merges the configs together and thus forgets that the config ever came fromnode_modules.tsconfigRootDironly applies to our parser if and only ifparserOptions.project != null.tsconfigRootDirisprocess.cwd()- so explicitly setting it toprocess.cwd()is the same as not setting it at all.The only solution currently is to use
tsconfigRootDir: __dirnameso that we know the exact path to your project root. You should be doing this regardless because:project: true- we know exactly when to stop traversingproject: 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 settsconfigRootDirand!filePath.includes(cwd), what should we do?I can only see two options:
tsconfigRootDirto__dirname.”./(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
tsconfigRootDirfrom within a shared config and only ever from within a.eslintrc.jsfile. EG: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 VSCode
tsconfigRootDirbecomes 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 configdirectory.length >= parseSettings.tsconfigRootDir.lengthbecomes totally wrong. Should be more likely: