lint-staged: Doesn't work in angular/cli 7 project
Description
Linting does not work in an angular/cli 7 project
Steps to reproduce
Create a project with the angular cli 7. This is my config in project.json
"lint-staged": {
"linters": {
"*.ts": "ng lint PROJECTNAME --files"
},
"relative": true
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
Debug Logs
× ng lint PROJECTNAME --files found some errors. Please fix them and try committing again.
Linting "PROJECTNAME "...
Unknown option: 'src\app\myservice\my.service.ts'
``
### Environment
- **OS:** Windows 10
- **Node.js:** v10.13.0
- **`lint-staged`:** v8.1.0
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 35 (3 by maintainers)
Commits related to this issue
- docs: fix example with relative paths (#547) an investigation commented here: https://github.com/okonet/lint-staged/issues/547#issuecomment-534191338 — committed to AndreiShostik/lint-staged by AndreiShostik 5 years ago
Hi @whiteblackkeys I realized late sorry for that. I changed to this and its working.
"lint-staged": { "linters": { "*.ts": [ "tslint --fix", "git add" ], "ignore": [ "**/dist/*.min.js" ] }, "relative": true },
@okonet I think this issue should be reopened 😃
We are having the same issue, we have a monorepo with different libs and apps so my workaround at the moment is to use tslint directly with specifying the tslint config for each project instead of using the angular cli:
Seems to work not sure if I miss out on any important things which are provided by the angular cli.
@michaeljota I’m agree with you. But… this is angular and the angular team for this kind of issue is much less reactive than an open source project like lint-staged IMHO. And in fact I think we can wait a certain time before something happened on the angular side 😦
Well, for me it’s working. I have this with
husky
and it does lint as expected the committed files.At least on Windows, the following works:
By the way, if you repeat the
--files
option, @angular/cli handles it successfully as follows:angular 7.2.1 lint --files require comma
i create bash script fix:
Bash script(lint.sh):
.lintstagedrc
@okonet It is great that PR #534 provides relative path options to use lint-staged with ng lint but pointing to the PR is not pretty helpful for this issue.
I run into the same issue here like @whiteblackkeys with exactly the same setup as described in the README section.
I don’t know why, but after a bunch of failing tests, this worked:
"*.ts": "ng lint PROJECTNAME -- --files"
See the extra double dashes. Without these dashes, ng lint seems to resolve the filenames exposed by lint-staged as another option flag.
@whiteblackkeys could you try and confirm?
guys, please, avoid angular-cli hidden issues.
tldr; the only one workable solutions with
ng lint
is a enumeration of files with--files
option:>ng lint --files src/app/app.module.ts --files src/app/shared/shared.module.ts
and relative path only! you can use the example below. but better to usetslint
directly it is really much faster. once again I convinced that angular-cli sucks@michaeljota please be careful with your solution it just runs linting without actually checking like in option
2.
in the example below.if you really-really want to use
ng lint
there is the correct workaround:>lint-staged -d
click it to see an investigation process...
investigation process:
https://angular.io/cli/lint#options - no info about how to use
--files
https://github.com/angular/angular-cli/wiki/lint - the same no infono matter how are you sending files via
--files
parameter it doesn’t work properly in most cases.>ng lint
shows me errors which I need:>ng lint --files src/app/app.module.ts src/app/shared/shared.module.ts
- a space between files>ng lint --files src/app/app.module.ts,src/app/shared/shared.module.ts
- comma between files>ng lint --files=["src/app/app.module.ts","src/app/shared/shared.module.ts"]
- an arraywtf?
>ng lint project --files "src/app/app.module.ts" "src/app/shared/shared.module.ts"
- let’s add a project maybe this will workugh, c`mon
>ng lint project --files "D:/project/src/app/app.module.ts" --files "D:/project/src/app/shared/shared.module.ts"
- maybe project and absolute paths?ah, f*** off angular-cli…
and as I already said using
tslint
directly is much faster:>lint-staged -d | gnomon --medium=5 --high=10 --real-time=false
Just happen anyone who looking around and insisted to use Angular CLI, @elunic just created ng-lint-staged to support Angular CLI’s “files” arguments. Thanks to him.
@michaeljota that worked for me
Ok. This is a working example of how to do this with the new feature of lint staged.
With this you will only lint the staged files, or the list of files provided by
lint-staged
.@okonet @whiteblackkeys Do you find this solution acceptable?
But this makes the usage of lint-staged pointless. You could just use husky for that.
Using
tslint
directly works well for me. Thanks @saiprasad2595.I tried
ng-lint-staged
and that worked, but theng lint
command is really slow for me (~8s). Usingtslint
directly is much faster (~800ms). As this is a pre-commit hook 8 seconds is way too long.@eggp @okon3 comma-separated files did not work for me, so modified the script as follows:
Are you sure about the comma? If I run
ng lint MY_PROJECT--ts-config src/tsconfig.app.json --files src\app\dynam\states\reducers\ReportsPageLayoutState.ts,src\app\dynam\states\AppEvents.ts
it tells meAll files pass linting
but if I run (note the multiple --files argument)
ng lint MY_PROJECT--ts-config src/tsconfig.app.json --files src\app\dynam\states\reducers\ReportsPageLayoutState.ts --files src\app\dynam\states\AppEvents.ts
it correctly tells me there are errors@bnjjj I open an issue in Angular about that angular/angular-cli#13078. I think that is actually Angular the one who is broken, but, I guess we have to find a way to workaround that limitation. 😕