eslinttypescript-eslint

How to include files for eslint that are not in my project's tsconfig.json file?


In a project I'm trying to include certain files (tests, build scripts) in my eslint flat configuration, without having to add them to my normal project configuration (tsconfig.json). For that I created a separate configuration file:

{
    "extends": "./tsconfig.json",
    "include": [
        "build",
        "src",
        "tests",
        "vitest.config.ts",
        "eslint.config.mjs"
    ]
}

which extends my project's tsconfig.json. I then used that in my eslint flat config:

import eslint from "@eslint/js";
import tslint from "typescript-eslint";
import stylistic from "@stylistic/eslint-plugin";
import jsdoc from "eslint-plugin-jsdoc";
import preferArrow from "eslint-plugin-prefer-arrow";
import importPlugin from "eslint-plugin-import";

export default tslint.config(
    eslint.configs.recommended,
    ...tslint.configs.strictTypeChecked,
    ...tslint.configs.stylisticTypeChecked,
    jsdoc.configs["flat/recommended"],
    {
        // This must be in its own object to avoid a bug in the ESLint parser.
        ignores: ["src/generated/*"],
    },
    {
        plugins: {
            "@stylistic": stylistic,
            "jsdoc": jsdoc,
            "prefer-arrow": preferArrow,
            "import": importPlugin,
        },
        languageOptions: {
            parser: tslint.parser,
            parserOptions: {
                project: "./eslint.tsconfig.json",
                tsconfigRootDir: import.meta.dirname,
                createDefaultProgram: true,
                projectService: {
                    allowDefaultProject: ["eslint.config.mjs", "./build/*.ts"],
                },
                sourceType: "module",
            },
        },
        rules: {
        }
    },
);

However, this still ignores the ts files in build/. Even more weird: I have to mention eslint.config.mjs in the allowedDefaultProject setting or I will get the same error:

Parsing error: /eslint.config.mjs was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject.

like I do for the ts files in build/. And it gets weirder: when I remove tests/ from the eslint.tsconfig.json file the files in tests/ are still being linted (I restarted VS Code to make sure the config was updated). So in summary:

  1. eslint gives an error for certain files that are included by its config file.
  2. eslint works nicely for files that are not included in its config file.

My project structure is simple:

<root>
    build/
    src/
    tests/
    tsconfig.json
    eslint.tsconfig.json
    eslint.config.mjs
    vitest.config.ts

Versions used:

    "devDependencies": {
        "@eslint/js": "9.17.0",
        "@stylistic/eslint-plugin": "2.12.1",
        "@stylistic/eslint-plugin-ts": "2.12.1",
        "@types/eslint__js": "8.42.3",
        "@typescript-eslint/eslint-plugin": "8.18.1",
        "@typescript-eslint/parser": "8.18.1",
        "eslint": "9.17.0",
        "eslint-plugin-import": "2.31.0",
        "eslint-plugin-jsdoc": "50.6.1",
        "eslint-plugin-prefer-arrow": "1.2.3",
        "typescript": "5.7.2",
        "typescript-eslint": "8.18.1",
        "vitest": "2.1.8"
    },


Solution

  • The solution is not to have a leading ./ part. I thought I had this tested before and found it not to work, but while testing different approach suddenly it did. Don't forget to reload VS Code after each config change or at least restart the ESLint server (via command pallet). This is what I have currently and it does the job:

            languageOptions: {
                parser: tslint.parser,
                parserOptions: {
                    projectService: {
                        allowDefaultProject: ["eslint.config.mjs", "build/*.ts"],
                        defaultProject: "tsconfig.json",
                    },
                    tsconfigRootDir: import.meta.dirname,
                    sourceType: "module",
                },
            },