lint-staged is running ng -lint for all the changed files instead of only staged files

Viewed 3009

I am trying to use husky with lint-staged for pre commit git hooks in angular5. Prettier formats only the staged files. But when I run ng lint command it runs on all the changed files instead of running on only staged files(I have 4 files modified, but only 2 files added to staging area using git add command. But all 4 files are checked for linting which is not what I expect)

Here is the configuration in .lintstagedrc

  "*.{ts,json}": [
    "prettier --write",
    "ng lint myProjName --files",
    "git add"
  ],
  "*.less": [
    "prettier --write",
    "npm run stylelint",
    "git add"
  ]
}```

I debugged the issue to some extent. --files takes only the files which have been staged into the account. But still when the linters task completes, I get errors for non staged files as well.
2 Answers

The --files flag was added in Angular v7 (maybe v7.1.2, I cannot find any reference in changelogs except this comment).

Versions before v7 do not support that flag, or at least it's not documented.

Angular CLI comes with a built in caching mechanism for ng lint via a --cache flag. I recommend using this instead. As it maintains and monitors your changed files without having to setup something like lint staged.

ng lint --cache
Related