Vscode-eslint: Error when creating new file, but disappear on window reload

Created on 21 Aug 2019  路  15Comments  路  Source: microsoft/vscode-eslint

I am getting this error every time I created a new TypeScript file:

Parsing error: If "parserOptions.project" has been set for @typescript-eslint/parser, /home/tkesgar/git/charaverse/admiral-hipper/src/mods/abc.ts must be included in at least one of the projects provided.

The error disappears if I run "Developer: Reload Window".

The error does not appear if I deleted a TypeScript file and created a new file with the same name.

Visual Studio Code version:

Version: 1.37.1
Commit: f06011ac164ae4dc8e753a3fe7f9549844d15e35
Date: 2019-08-15T16:17:25.463Z
Electron: 4.2.7
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Linux x64 4.15.0-58-generic snap

Extension:

Name: ESLint
Id: dbaeumer.vscode-eslint
Description: Integrates ESLint JavaScript into VS Code.
Version: 1.9.1
Publisher: Dirk Baeumer
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

.eslintrc.json:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint"
  ]
}

package.json:

{
  "name": "admiral-hipper",
  "version": "0.0.0",
  "repository": "https://github.com/charaverse/admiral-hipper",
  "author": "Ted Kesgar <[email protected]>",
  "license": "Apache-2.0",
  "scripts": {
    "start": "node dist/server.js",
    "watch:server": "nodemon",
    "watch:ts": "tsc --watch",
    "watch": "run-p watch:*",
    "lint": "eslint --ext '.js,.ts' 'src/**/*.ts'",
    "format": "prettier --write 'src/**/*.ts'",
    "test": "jest",
    "coverage": "jest --coverage",
    "clean": "del-cli dist/",
    "build": "tsc"
  },
  "dependencies": {
    "dotenv": "^6.2.0",
    "express": "^4.17.1",
    "pino": "^5.13.2"
  },
  "devDependencies": {
    "@types/dotenv": "^6.1.1",
    "@types/express": "^4.17.1",
    "@types/jest": "^24.0.18",
    "@types/node": "^12.7.2",
    "@types/pino": "^5.8.9",
    "@typescript-eslint/eslint-plugin": "^2.0.0",
    "@typescript-eslint/parser": "^2.0.0",
    "del-cli": "^2.0.0",
    "eslint": "^6.2.1",
    "eslint-config-prettier": "^6.1.0",
    "eslint-plugin-prettier": "^3.1.0",
    "husky": "^3.0.4",
    "jest": "^24.9.0",
    "lint-staged": "^9.2.3",
    "nodemon": "^1.19.1",
    "npm-run-all": "^4.1.5",
    "pino-pretty": "^3.2.1",
    "prettier": "1.18.2",
    "ts-jest": "^24.0.2",
    "typescript": "^3.5.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "ts",
      "js"
    ],
    "testEnvironment": "node",
    "testMatch": [
      "<rootDir>/src/tests/**/*.ts"
    ],
    "transform": {
      "^.+\\.ts$": "ts-jest"
    }
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,ts}": [
      "eslint --fix",
      "prettier --write",
      "git add"
    ]
  }
}

Repository:
https://github.com/charaverse/admiral-hipper/tree/7315ff1144

TypeScript bug upstream

Most helpful comment

I've solved trouble used VSCode workspace setting eslint.options to add parserOptions.

"eslint.options": {
    "parserOptions": {
      "project": [
        "${workspaceFolder}/tsconfig.json"
      ],
    }
  }

All 15 comments

The problem is that the new file is not on disk but we ask to validate it. I don't know if the TS parsing can be tweaked that way.

Another option would be to only validate files on disk in this setup. What exactly does parserOptions.project do?

I'm following the README from @typescript-eslint/parser when creating the config:

project - default undefined. This option allows you to provide a path to your project's tsconfig.json. This setting is required if you want to use rules which require type information. You may want to use this setting in tandem with the tsconfigRootDir option below.

I even get this type of error in my webpack.config.js file.
Screenshot
.eslintrc.json:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "env": {
    "node": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ]
}

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "resolveJsonModule": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "dist"]
}

@yegorgunko does this also happen when you validate from the terminal in the workspace folder ?

@dbaeumer well, on webpack.config.js - yes. But on the other files (in src for example) - no. Fixed by using .TS version of webpack config.

Setting the parser option "createDefaultProgram": true will circumvent the included check in typescript/eslint (with a performance penalty)
I suspect they might have a caching issue eg. if they store a map of all asts/files to lint. It could be that the map is not updated using watch when used in long running instances webpack/vs code eslint extension etc.

Setting the parser option "createDefaultProgram": true will circumvent the included check in typescript/eslint (with a performance penalty)

I tried this on the referenced commit and it indeed works :+1:

We have the same problem on https://github.com/HospitalRun/hospitalrun-server/tree/next. If we rename a file or add a new one, eslint flags it as "not watched" by tsconfig.json project. We mitigated it adding "createDefaultProgram": true to our parserOptions.
The issue occurs only in VSCode: restarting VSCode fixes the problem and it works as expected in terminal.

@HosseinAgha thanks for pointing to the typescript eslint issue.

As state over there this is best fixed on the typescript eslint side since it affects all integrations of the parser (WebStorm, watch mode, ...)

I will mark the issue as upstream.

I've solved trouble used VSCode workspace setting eslint.options to add parserOptions.

"eslint.options": {
    "parserOptions": {
      "project": [
        "${workspaceFolder}/tsconfig.json"
      ],
    }
  }

Closing since the fix needs to come from the typescript plugin.

It looks like it already has been fixed on the typescript plugin side?
https://github.com/typescript-eslint/typescript-eslint/issues/864
https://github.com/typescript-eslint/typescript-eslint/pull/973

Is there anything that needs to be changed on the vscode-eslint side?

I've got the new parser 2.5.0 installed, and the error still occurs in VSC.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NickHeiner picture NickHeiner  路  4Comments

LinusU picture LinusU  路  7Comments

kumarharsh picture kumarharsh  路  3Comments

MuhammadJamaluddin picture MuhammadJamaluddin  路  4Comments

bacsonvv picture bacsonvv  路  6Comments