Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is @typescript-eslint/parser including files outside of those configured in my tsconfig.json?

I am getting the error: -

Parsing error: "parserOptions.project" has been set for u/typescript-eslint/parser.

The file does not match your project config: .eslintrc.js.

The file must be included in at least one of the projects provided.

IDE - WebStorm 20.1.1

Project structure: -

node_modules

src

--- testfile.js

.eslintrc.js

.prettierrc.js

baseeslint.js

package.json

tsconfig.json

.eslintrc.js: -

module.exports = {
extends: ['./baseeslint'],
parserOptions: {
project: './tsconfig.json'
},
rules: {}
}

baseeslint.js: -

module.exports = {

parser: '@typescript-eslint/parser'

}

tsconfig.json: -

{

"include": [

"/src/**/*.ts",

"/src/**/*.tsx"

],

"exclude": ["node_modules"],

"compilerOptions": {

"outDir": "./lib",

"types": ["styled-components", "react", "@types/react-router-dom", "reactstrap"]

}

}

I have absolutely no idea why this is happening? I would assume it would ignore files in the root, especially with include specified in my tsconfg.json?

Any help would be greatly appreciated.

EDIT: -

My solution, should it help others...

I added the ignorePatterns configuration to my .eslintrc.js file: -

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

This can also be done via .eslintignore (see Anton Bessonov's response below).

Another thing relating to this which maybe of use to others here is the VScode config I used (I switched from WebStorm to VSCode after posting the original question). I am a big fan of running eslint fix on save but I do not want it attempting to do this with all files. My project files (and only my project files) are all .ts/.tsx. The options below in settings.json allow fix on save without it including every file in my repo: -

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.probe": [
        "typescript",
        "typescriptreact"
    ],
}

If eslint.probe is not specified, it defaults to: -

["javascript", "javascriptreact", "typescript", "typescriptreact", "html", "vue"]

More about the VSCode ESLint extension settings can be found here.

like image 722
U4EA Avatar asked May 22 '20 13:05

U4EA


People also ask

How do you fix parsing error Parseroptions project has been set for TypeScript ESLint parser?

The easiest way to solve the error is to add the files in which you get it to your . eslintignore file, which should be located in the root directory of your project, right next to . eslintrc. js .

What does TypeScript ESLint parser do?

An ESLint parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code.

Does ESLint use Tsconfig?

If you intend on running ESLint from directories other than the project root, you should consider using tsconfigRootDir . Accepted values: // path project: './tsconfig.

Can not read file Tsconfig?

To solve the error "Parsing Error: Cannot read file 'tsconfig. json'", update your . eslintrc. js file to set the tsconfigRootDir option to __dirname to force eslint to resolve your project configuration relative to the folder where .


4 Answers

Why this happens?

I'm not entirely sure, but it seems like @typescript-eslint/parser tries to compare files covered in the eslint configuration (for example by extension) with files included in tsconfig.json. Since the .js files are covered by @typescript-eslint/parser and depending on your eslint configuration, the file is probably included in the eslint run. But because the file isn't included in tsconfig, you get this error.

How to fix that?

Depending on your case, you can select one of them:

  1. You can ignore it in .eslintignore. This is what @U4EA did.
  2. If can add it to the includes in tsconfig.json:
    "include": [
        ".eslintrc.js",
        // another includes
    ]
  1. If you don't have a "right" tsconfig.json, because you use a monorepo, then you can create a new file tsconfig.eslint.json:
{
    "include": [
        ".eslintrc.js"
    ]
}

and use this file inside .eslintrc.js for eslint only:

    parserOptions: {
        project: [
            './tsconfig.eslint.json',
            './packages/*/tsconfig.json',
        ],
    },
like image 188
Anton Bessonov Avatar answered Oct 21 '22 20:10

Anton Bessonov


You can also ignore .eslintrc.js in .eslintrc.js itself, saving you an additional file:

"ignorePatterns": [
    ".eslintrc.js"
],
like image 23
Christoph Avatar answered Oct 21 '22 22:10

Christoph


can be helpful for VS Code users, uninstalling or disabling ESLint extension may solve this issue, it worked for me :)

like image 4
aatif shaikh Avatar answered Oct 21 '22 21:10

aatif shaikh


Extension

I had a similar issue and I disabled the eslint extension on vs code and that was it for me. I'm assuming there was some kind of conflict between the global eslint and the one for my project.

like image 3
NG-Joseph Avatar answered Oct 21 '22 21:10

NG-Joseph