How to make Visual Studio Code check entire project for errors?
JavascriptTypescriptVisual Studio-CodeJavascript Problem Overview
I'm using VS Code for TypeScript/JavaScript development. When I open a file it will check that file for errors. The problem is if I'm refactoring (like I move some shared code to a new location or change a name) it won't show me the errors this caused until I open the file with the problem. ...so if I want to do extensive refactoring I have to open every file just to make it scan the file for errors.
How can I make VS Code scan the whole project for errors without having to open each file one by one manually?
Javascript Solutions
Solution 1 - Javascript
VS Code (v1.44) has an experimental feature, that allows project wide error reporting in TS. Try it out:
// put this line in settings.json
"typescript.tsserver.experimental.enableProjectDiagnostics": true
Solution 2 - Javascript
Figured it out. Note this answer is specific to TypeScript, which is what I am using. Here it is:
Make sure typescript is installed globally (I just had mine installed locally apparently):
npm install -g typescript
Then in VS Code press Shift+Ctrl+B. If you don't have a task runner set up it will ask what you want. I selected typescript and the tasks.json file will look like this:
{
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
Then pressing Shift+Ctrl+B (or Shift+Command+B in macOS) will check the entire project for problems and they will be reported in your "problems" panel.
Solution 3 - Javascript
If you don't want to install TypeScript globally, you can do the following:
-
Install TypeScript locally on the project, that is
yarn add --dev typescript
ornpm install --save-dev typescript
. -
Add a check-types run script to
./package.json
.--noEmit
means that the compiler will won't generate any JavaScript files.
{
"scripts": {
"check-types": "tsc --noEmit"
}
}
3. Let VSCode know about the run script in /.vscode/tasks.json
.
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "check-types",
"problemMatcher": [
"$tsc"
]
}
]
}
4. To run the tasks hit the F1 key and select 'Run Task', and then 'npm: check-types'.
-
If you add the following lines to the task, pressing
Ctrl+B
will run it."group": { "kind": "build", "isDefault": true }
Solution 4 - Javascript
For the most recent version of tasks.json
this is the correct json, following deprecations in version 1.14. Create this as /.vscode/tasks.json
{
"version": "2.0.0",
"command": "tsc",
"type": "shell",
"args": [
"-p",
"."
],
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$tsc"
}
Solution 5 - Javascript
Once you have open your project in vs code, open the vs code terminal and run:
node_modules/.bin/tsc --noEmit
Solution 6 - Javascript
UPDATE.
My answer below does not answer the original question, but if you're like me and have found this thread searching for how to turn on // @ts-check
project-wide in VSCode so that you don't need to add // @ts-check
to every file then my answer below is what you need. I have searched and searched and kept getting this thread as my top result so hopefully this helps others as well
I'm on vscode version 1.52.1 and the answer is so simple and right on the vscode website:
https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript
scroll down to the "Using jsconfig or tsconfig" section
Add a jsconfig.json in your project root and add "checkJs": true
{
"compilerOptions": {
"checkJs": true
},
"exclude": ["node_modules", "**/node_modules/*"]
}
You might need to restart vscode once you add it in
Solution 7 - Javascript
- Go to View menu > Extensions and make sure the Microsoft VS Code ESLint extension is installed.
- In Settings, search for "ESLint > Lint Task: Enable", and enable that setting (docs).
- In the Terminal menu, choose Run Task… >
eslint: lint whole folder
.
Solution 8 - Javascript
Edit: Since updating to 1.52.0 this no longer works. It will instead replace the current project files with what you selected...
===
I've tried every solution I can find and I think it's safe to say the answer is: You can't*
The best I've found is still technically opening each file manually, but at least it's not one-by-one:
- You can't drag/drop a directory but you can select multiple files (including directories) and drag/drop. The directories will be ignored and any files you had selected will be opened in tabs.
- Next, you need to give each tab focus so it triggers eslint to run. (Holding down the next tab keyboard shortcut works.)
This is terrible and I beg someone to prove me wrong.
*I haven't found a way that works as well as opening files manually. Each method -- experimental vscode feature and tsc task -- has its own set of drawbacks. The vscode feature is clearly the solution but without a way to ignore node_modules, etc. it's too painful to use.