Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to lint all files in project? #91

Open
empz opened this issue Jun 22, 2016 · 77 comments
Open

How to lint all files in project? #91

empz opened this issue Jun 22, 2016 · 77 comments
Assignees
Labels
feature-request Request for new features or functionality
Milestone

Comments

@empz
Copy link

empz commented Jun 22, 2016

I noticed ESLint only shows errors of files you've opened. Is there a way to force it to scan all files in the project?

@dbaeumer dbaeumer added the feature-request Request for new features or functionality label Jun 22, 2016
@dbaeumer
Copy link
Member

Currently not. However we were thinking about adding that support.

@egamma
Copy link
Member

egamma commented Jun 22, 2016

@emzero - @dbaeumer is correct, but what you can do today is to create a task that runs eslint across all files.

@oshalygin
Copy link

Is the challenge reducing the overhead in constantly linting all files in the solution?

We can always run eslint from the command line but of course it would be ideal if all files were automatically linted as we work through a solution.

@johntmyers
Copy link

Would be nice for Python too. For example if I change the name of a module, nothing re-lints, etc until some code is changed and a file is re-saved.

@cesaraaron
Copy link

I need this badly (I come from standard and the plugin for vscode does this by default)
Any news?

@oshalygin
Copy link

Willing to help if someone can give direction on where to even start.

@llozesdome
Copy link

I understand that linting the whole project all the time could damage the performance. A dedicated action that could be run on demand would still be great.

@egamma
Copy link
Member

egamma commented Apr 20, 2017

Here is an example for how to do this for eslint using Tasks https://github.com/Microsoft/vscode-eslint/blob/master/eslint/README.md#using-the-extension-with-vs-codes-task-running

@avizov
Copy link

avizov commented Oct 2, 2017

the link is broken... can you please check?

@dbaeumer
Copy link
Member

dbaeumer commented Oct 2, 2017

https://github.com/Microsoft/vscode-eslint/blob/master/README.md#using-the-extension-with-vs-codes-task-running

@janaagaard75
Copy link

This answer here on Stack Overflow might also be helpful: https://stackoverflow.com/q/41702815/37147.

@avizov
Copy link

avizov commented Oct 2, 2017

Just to verify, the proposed "task" solution is it from vscode client side (an ability to run tasks in vscode editor)?
In our case we are not using vscode editor as our client but we are using some another editor tool. Is there option to run task using LSP protocol?

@i328638
Copy link

i328638 commented Oct 25, 2017

I created PR #323 for prviding a new project level diagnostics mode.
I used the existing initializationOptions: any which is part of the initialization parameters.
I added there a new property projectValidation that if true, diagnostics are maintained on the whole project level and not only for opened documents.
I tried to cover the changes in document life cycle for this new mode.
Please review this solution.

@renato
Copy link

renato commented Nov 14, 2017

I'm using a custom task to extract the eslint errors/warnings but the default $eslint-stylish problemMatcher doesn't add the "eslint" owner to the problems, so the [eslint] prefix is only added after I open a file, and when I close it the eslint problems are removed.

I understand that the errors are currently being cleared on close (#306) but is there a current workaround to both situations?

EDIT: I just checked and it's using the eslint owner but for some reason all messages are missing the prefix.

@dbaeumer
Copy link
Member

This is a bug on the VS Code side we are aware of.

@tvvignesh
Copy link

Hi guys. Any plans on making this available? Is it in the roadmap?

@dbaeumer
Copy link
Member

I started to work on this last week but nothing finished yet. The branch is https://github.com/Microsoft/vscode/tree/dbaeumer/47386

@stavalfi
Copy link

Hi, is there any progress on this issue?

Thanks in advance!!

@dbaeumer
Copy link
Member

No, nothing has happened. The original bug in VS Code microsoft/vscode#47386 got fixed for TS but ESLint doesn't profit from this.

I gave feedback on the PR however I am not sure if this should really be built into the extension or better be handled as a task.

@stavalfi
Copy link

shouldn't it be on a different issue than microsoft/vscode#47386?

@hadim
Copy link

hadim commented Sep 28, 2018

Any news on this?

@baconcheese113
Copy link

Yea, tried that and specified the .eslintrc location...but the issue was probably needing to specify the eslint bin location. Also created a custom task and got it to actually start up (according to the terminal) but it didn't do anything...

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "command": "cmd", // for windows "command": "cmd"
    "args": [
        "-c"
    ], // for windows "args": ["/C"]
    "tasks": [
        {
            "label": "eslint",
            "args": [
                "client/node_modules/eslint/bin/eslint.js ./src --ext .js" // To use locally installed eslint,  ./node_modules/eslint/bin/eslint.js .
            ],
            "problemMatcher": [
                "$eslint-stylish"
            ]
        }
    ]
}

Finally gave up and moved my .eslintrc, .eslintignore and eslint deps to my root directory. client is a create-react-app and has it's own eslint packages included. So now with these settings I'm still unable to get auto-fixing working in client but it's working in server 🤔 Any thoughts?

{
  "editor.formatOnSave": false,
  "eslint.lintTask.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
  },
  "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"],
  "editor.codeActionsOnSaveTimeout": 5000,
}

@dbaeumer
Copy link
Member

Do you have a GitHub repository I can clone?

@baconcheese113
Copy link

baconcheese113 commented Mar 19, 2020

Here ya go https://github.com/baconcheese113/god-help-me-lint
Explained it a bit in the readme, I'm not using the prettier extension since that seems to mess things up.

@dbaeumer
Copy link
Member

dbaeumer commented Mar 19, 2020

Can you please provide additional steps to reproduce the problem.

If I try to lint the client in the terminal ./node_modules/.bin/eslint client/src/index.js I get

Oops! Something went wrong! :(

ESLint: 6.8.0.

ESLint couldn't find the config "react-app" to extend from. Please check that the name of the config is correct.

The config "react-app" was referenced from the config file in "/home/dirkb/Projects/mseng/VSCode/Playgrounds/bugs/god-help-me-lint/client/package.json".

If you still have problems, please stop by https://gitter.im/eslint/eslint to chat with the team.

@baconcheese113
Copy link

@dbaeumer Sorry, didn't try to run it through the terminal since I could already tell eslint was working on some files (but not the exhaustive-deps rule)....

I think my issue is actually due to this #814 (comment) Currently I use eslint-plugin-react-hooks@2.3.0 at work and wasn't aware of the big change between it and 2.5.1 which prevents auto-fixing of the rule I was hoping for.

Do you know any ways to enable auto fixing for suggestions?

@dbaeumer
Copy link
Member

Suggestions by definition can't be autofix. But they will show up as a possible fix in the code action menu (when you click on the light bulb). Requires the latest version of the ESLint extension.

@mika76
Copy link

mika76 commented Mar 24, 2020

So @dbaeumer to get the original discussion back on track - what are your thoughts on a full-project-lint going forward? Do you think the caching feature would work/help?

@dbaeumer
Copy link
Member

@mika76 from the experiments I did I got the impression that they caching feature will not help a lot since you still need to validate all files in the project.

I still think that a task triggered by a user is currently the right approach for this.

@mbomb007
Copy link

If you want to lint the whole workspace set eslint.lintTask.enable to true and the extension will also contribute the eslint: lint whole folder task. There is no need anymore to define a custom task in tasks.json.

Source: https://github.com/Microsoft/vscode-eslint/blob/master/README.md#using-the-extension-with-vs-codes-task-running

@DanTup
Copy link

DanTup commented Apr 28, 2020

@dbaeumer

Currently only the opened changed files are linted. We experimented with linting all open files but that causes other confusions since people where assuming we are doing a dependency linting (which we actually can't do). So we stayed away from it.

Does this mean we're not likely to see this automatic whole workspace linting anytime soon? I was trying to migrate from tslint to eslint because I was getting warnings about it being deprecated, but just noticed I'm not seeing the errors anymore without files open. With tslint, I did get this (I'm not sure exactly through what mechanism - it might be that webpack is running onFolderOpen in watch mode and using ts-webpack-watch?).

@dbaeumer
Copy link
Member

To my knowledge the TSLint extension only validated the open files as well (@egamma can you confirm that). So if the whole workspace got linited it was very likley through a task which is also support in ESLint.

@DanTup
Copy link

DanTup commented Apr 28, 2020

I do have tasks that run on folderOpen (tsc --watch and webpack --watch) with the $ts-webpack-watch and $tsc-watch problem matchers which I suspect were doing this (the $ts-webpack-watch problem matcher is coming from the eamodio.tsl-problem-matcher extension) but they're all still running after moving to eslint. I couldn't find any info about whether (and if so how) this should work though 😟

@daiky00
Copy link

daiky00 commented Jun 4, 2020

wow this was open 4 years ago and is not resolved O.o

@dbaeumer
Copy link
Member

@daiky00 have you use the setting eslint.lintTask.enable

@aFcFzF
Copy link

aFcFzF commented Jun 16, 2020

npx tsc --noEmit

@ooyyloo
Copy link

ooyyloo commented Jun 29, 2020

I want to lint all files of the project when opening the project folder, and then show errors on opened files. Is this possible using APIs provided?

@Domiii
Copy link

Domiii commented Jul 19, 2020

Workaround

Since this is maybe low priority for the team for now, here is a possible workaround.

While we cannot directly "add all problems in project to the Problems tab", we can automatically open "all problematic files" in the editor, which in turn will actually make them show up in the Problems tab. Here is how:

  1. I have a monorepo with several packages, each having an src folder I want to lint.
  2. My eslint command looks like this: npx eslint **/src/**/*.js
  3. We can simply grep all problematic files like this: npx eslint **/src/**/*.js | grep ^[\\w/].*\\.js
    • Why does this work? Because for every line of output:
      • if the line is a file: starts with a character (on Windows) or a slash (any other OS)
      • if the line is an error: starts with spaces
  4. We can then open all problematic files in VSCode using the code command line tool: code $(npx eslint **/src/**/*.js | grep ^[\\w/].*\\.js)

I ended up adding the 3 following scripts to my package.json:

"eslint": "npx eslint **/src/**/*.js",
"eslint:files": "npx eslint **/src/**/*.js | grep ^[\\w/].*\\.js",
"eslint:files:open": "code $(npx eslint **/src/**/*.js | grep ^[\\w/].*\\.js)"

Hope that helps :)

Update for Windows

Since FunctionDJ asked below - here are some (of many) choices as how to run this on Windows:

  1. Git comes with the Unix toolset, but, by default, does not add them to %PATH%. Don't forget to tell it to do just that.
    • If you still don't use package managers, you probably install Git by downloading it from the website. During installation, don't forget this crucial step of adding Git and all Unix tools to %PATH%. -> Problem solved.
    • If you use Chocolatey, don't forget to pass the corresponding parameters to the git installer: choco install git.install --params "/GitAndUnixToolsOnPath" (as of May 2021)
    • If you install Git using Winget... I don't know. At this point, Winget is still in an early stage. Expect better documentation within a year or two.
  2. Git provides these tools by installing Cygwin for you. You can just install an independent copy of Cygwin manually, if you feel like it.
  3. More advanced options include use of WSL

@lukewlms
Copy link

This code works if you don't want to repeat the same command three times.

    "eslint": "eslint . --fix --config .eslintrc.commit.yaml --ext .ts,.tsx",
    "eslint:files": "yarn eslint | grep -e '^/.*tsx$' -e '^/.*ts$'",
    "eslint:files:open": "code $(yarn --silent eslint:files)",

--silent is needed so code doesn't try to open the command that yarn would otherwise echo.

@FunctionDJ
Copy link

Workaround

Since this is maybe low priority for the team for now, here is a possible workaround.
(...)

@Domiii

This sounds great. How can i change it to run on Windows? (grep not available)

@Domiii
Copy link

Domiii commented May 20, 2021

Workaround

Since this is maybe low priority for the team for now, here is a possible workaround.
(...)

@Domiii

This sounds great. How can i change it to run on Windows? (grep not available)

I updated my answer to address this Q as well :)

@FunctionDJ
Copy link

FunctionDJ commented Jul 26, 2021

My current solution: Tasks and problem matchers!
tasks.json:

{
  "label": "lint all",
  "script": "lint:all",
  "problemMatcher": ["$eslint-stylish"]
}

npm run lint:all is configured to run eslint --max-warnings 0 ..
This will pull the console output of the command into your problems view, which almost as good as opening all files for you :)

@piotrgredowski
Copy link

@ooyyloo
I had the same problem some time ago and I came with solution I desribed in this post.
It works like charm for me. One think to do here will be adding problem matcher for eslint. But yeah, as I see in @FunctionDJ message, there is one built in VSCode probably :).

@mudroljub
Copy link

mudroljub commented Nov 26, 2021

eslint . --ext .js

with auto fix:

eslint . --ext .js --fix

@FunctionDJ
Copy link

@mudroljub that's unrelated to the VSCode extension. the extension is there so that errors get shown in the editor itself, not just the console.

@alirezanet
Copy link

Workaround for Vue CLI
add this task to your tasks.json file:

      {
         "type": "npm",
         "label": "lint",
         "script": "lint",
         "problemMatcher": [
            {
               "owner": "eslint",
               "fileLocation": [
                  "relative",
                  "${workspaceFolder}"
               ],
               "pattern": {
                  "regexp": "^(.+):\\s(.+)\\((.+)\\)\\sat\\s(.+):(\\d+):(\\d+):$",
                  "severity": 1,
                  "message": 2,
                  "code": 3,
                  "file": 4,
                  "line": 5,
                  "column": 6
               }
            }
         ],
         "options": {
            "cwd": "${workspaceFolder}"
         },
      },

@soonsam123
Copy link

Currently it is supported to do this using

npx eslint --fix src

You can substitute src for the folder you want to analyze.

Got reference from this github issue.

@FunctionDJ
Copy link

@soonsam123 that's not the same. --fix automatically fixes the files. it's very different from seeing all linting problems, which you might want to review and fix manually instead of automatically.

@SystemDisc
Copy link

This is my .vscode/tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "label": "lint",
      "script": "lint",
      "problemMatcher": [
        {
          "owner": "eslint",
          "fileLocation": ["relative", "${workspaceFolder}"],
          "pattern": [
            {
              "regexp": "\\.\\/(.+)",
              "kind": "location",
              "file": 1
            },
            {
              "regexp": "^([0-9]+):([0-9]+)  (.+?): (.+?)(  (\\S+))?$",
              "severity": 3,
              "message": 4,
              "code": 6,
              "line": 1,
              "column": 9
            }
          ]
        }
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests