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

21,582

Solution 1

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 for eslint only:

    parserOptions: {
        project: [
            './tsconfig.eslint.json',
            './packages/*/tsconfig.json',
        ],
    },

Solution 2

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

"ignorePatterns": [
    ".eslintrc.js"
],

Solution 3

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

Solution 4

Solved by adding .eslintignore

/*.*

Edit:

My final resolution was to add the ignorePatterns configuration to my .eslintrc.js file: -

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

Reason for this is to just get rid of the .eslintignore file (less workspace clutter).

Solution 5

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.

Share:
21,582
U4EA
Author by

U4EA

Updated on February 06, 2022

Comments

  • U4EA
    U4EA about 2 years

    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.