lint-staged / lint-staged

🚫💩 — Run linters on git staged files

Home Page:https://www.npmjs.com/package/lint-staged

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project ... does not exist

Berkmann18 opened this issue · comments

Description

Steps to reproduce

  • Create an Angular project
  • npm i -D husky prettier lint-staged
  • npx husky install
  • Add to package.json
  "lint-staged": {
    "*.{ts,js}": [
      "prettier --write",
      "npm run lint"
    ],
    "*.{scss,html,json}": "prettier --write"
  }
  • Modify a TS file and commit.
  • See an error along the lines of An unhandled exception occurred: Project '/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts' does not exist..

Here's a repo where I tried to get hooks to work (note the branch): https://github.com/Berkmann18/ng-git-hooks/tree/husky-ls

Debug Logs

expand to view
  lint-staged:bin Running `lint-staged@10.5.4` +0ms
  lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 65536,
  stash: true,
  quiet: false,
  relative: false,
  shell: false,
  verbose: false
} +1ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/home/maxie/Projects/ng-git-hooks/package.json`:
  lint-staged {
  lint-staged   '*.{ts,js}': [ 'prettier --write', 'npm run lint' ],
  lint-staged   '*.{scss,html,json}': 'prettier --write'
  lint-staged } +12ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.{ts,js}': [
    'prettier --write',
    'npm run lint'
  ],
  '*.{scss,html,json}': 'prettier --write'
}
  lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`) +2ms
  lint-staged:run Running all linter scripts +0ms
  lint-staged:resolveGitRepo Resolving git repo from `/home/maxie/Projects/ng-git-hooks` +0ms
  lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`) +0ms
  lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`) +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:resolveGitRepo Resolved git directory to be `/home/maxie/Projects/ng-git-hooks` +11ms
  lint-staged:resolveGitRepo Resolved git config directory to be `/home/maxie/Projects/ng-git-hooks/.git` +1ms
  lint-staged:git Running git command [ 'log', '-1' ] +12ms
  lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only', '-z' ] +3ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'src/app/app.component.ts' ] +20ms
  lint-staged:chunkFiles Resolved an argument string length of 58 characters from 1 files +0ms
  lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 65536 +0ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks {
  lint-staged:gen-tasks   pattern: '*.{ts,js}',
  lint-staged:gen-tasks   commands: [ 'prettier --write', 'npm run lint' ],
  lint-staged:gen-tasks   fileList: [ '/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts' ]
  lint-staged:gen-tasks } +3ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks {
  lint-staged:gen-tasks   pattern: '*.{scss,html,json}',
  lint-staged:gen-tasks   commands: 'prettier --write',
  lint-staged:gen-tasks   fileList: []
  lint-staged:gen-tasks } +0ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'prettier --write', 'npm run lint' ] +0ms
  lint-staged:task cmd: prettier +0ms
  lint-staged:task args: [ '--write' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:task cmd: npm +0ms
  lint-staged:task args: [ 'run', 'lint' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands 'prettier --write' +2ms
  lint-staged:task cmd: prettier +1ms
  lint-staged:task args: [ '--write' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:chunkFiles Resolved an argument string length of 58 characters from 1 files +7ms
  lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 65536 +0ms
[STARTED] Preparing...
  lint-staged:git Backing up original state... +0ms
  lint-staged:git Getting partially staged files... +0ms
  lint-staged:git Running git command [ 'status', '-z' ] +15ms
  lint-staged:git Found partially staged files: [] +4ms
  lint-staged:git Backing up merge state... +1ms
  lint-staged:file Reading file `/home/maxie/Projects/ng-git-hooks/.git/MERGE_HEAD` +0ms
  lint-staged:file Reading file `/home/maxie/Projects/ng-git-hooks/.git/MERGE_MODE` +0ms
  lint-staged:file Reading file `/home/maxie/Projects/ng-git-hooks/.git/MERGE_MSG` +0ms
  lint-staged:file File `/home/maxie/Projects/ng-git-hooks/.git/MERGE_HEAD` doesn't exist, ignoring... +1ms
  lint-staged:file File `/home/maxie/Projects/ng-git-hooks/.git/MERGE_MODE` doesn't exist, ignoring... +0ms
  lint-staged:file File `/home/maxie/Projects/ng-git-hooks/.git/MERGE_MSG` doesn't exist, ignoring... +0ms
  lint-staged:git Done backing up merge state! +1ms
  lint-staged:git Getting deleted files... +0ms
  lint-staged:git Running git command [ 'ls-files', '--deleted' ] +6ms
  lint-staged:git Found deleted files: [] +5ms
  lint-staged:git Running git command [ 'stash', 'create' ] +5ms
  lint-staged:git Running git command [
  'stash',
  'store',
  '--quiet',
  '--message',
  'lint-staged automatic backup',
  '85cecc1bdb737fa2a8702c405246999fe66e993d'
] +6ms
  lint-staged:git Done backing up original state! +11ms
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.{ts,js}
[STARTED] Running tasks for *.{scss,html,json}
[SKIPPED] No staged files match *.{scss,html,json}
[STARTED] prettier --write
[SUCCESS] prettier --write
[STARTED] npm run lint
[FAILED] npm run lint [FAILED]
[FAILED] npm run lint [FAILED]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
  lint-staged:git Restoring original state... +965ms
  lint-staged:git Running git command [ 'reset', '--hard', 'HEAD' ] +970ms
  lint-staged:git Running git command [ 'stash', 'list' ] +5ms
  lint-staged:git Running git command [ 'stash', 'apply', '--quiet', '--index', 'refs/stash@{0}' ] +5ms
  lint-staged:git Restoring merge state... +22ms
  lint-staged:git Done restoring merge state! +0ms
  lint-staged:file Removing file `/home/maxie/Projects/ng-git-hooks/.git/lint-staged_unstaged.patch` +1s
  lint-staged:file File `/home/maxie/Projects/ng-git-hooks/.git/lint-staged_unstaged.patch` doesn't exist, ignoring... +1ms
  lint-staged:git Done restoring original state! +1ms
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
  lint-staged:git Dropping backup stash... +0ms
  lint-staged:git Running git command [ 'stash', 'list' ] +13ms
  lint-staged:git Running git command [ 'stash', 'drop', '--quiet', 'refs/stash@{0}' ] +4ms
  lint-staged:git Done dropping backup stash! +10ms
[SUCCESS] Cleaning up...

✖ npm run lint:
An unhandled exception occurred: Project '/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts' does not exist.
See "/tmp/ng-TUhhkl/angular-errors.log" for further details.
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ng-git-hooks@0.0.0 lint: `ng lint "/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts"`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ng-git-hooks@0.0.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/maxie/.npm/_logs/2021-02-12T21_11_30_310Z-debug.log

> ng-git-hooks@0.0.0 lint /home/maxie/Projects/ng-git-hooks
> ng lint "/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts"

Environment

  • OS: ArchLinux
  • Node.js: v14.11.0
  • lint-staged: 10.5.4

I have the same issue.

As your error is coming from one of your scripts, I’m not sure if it’s related to lint-staged.

✖ npm run lint:
An unhandled exception occurred: Project '/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts' does not exist.
See "/tmp/ng-TUhhkl/angular-errors.log" for further details.
npm ERR! code ELIFECYCLE

@okonet You're possibly right, running npm run lint leads to the following output:

> ng lint

TSLint's support is discontinued and we're deprecating its support in Angular CLI.
To opt-in using the community driven ESLint builder, see: https://github.com/angular-eslint/angular-eslint#migrating-an-angular-cli-project-from-codelyzer-and-tslint.
Linting "ng-git-hooks"...
/home/maxie/Projects/ng-git-hooks/src/app/app.component.ts:3:19
ERROR: 3:19  quotemark   " should be '
ERROR: 3:28  quotemark   " should be '
ERROR: 3:37  quotemark   " should be '
ERROR: 3:42  quotemark   " should be '
ERROR: 3:73  whitespace  missing whitespace
ERROR: 3:74  semicolon   Missing semicolon

Lint errors found in the listed files.

It may well be a husky error.

@wadeV12 Do you get the same error using husky@5.0.9?

I have the same issue, anyone figure out a solution?

@gleisonkz I tried two (well one that actually uses lint-staged), the first one running husky@4.2.1 and pretty-quick (so no involvement of lint-staged) and one using husky@6.0.0 and lint-staged@10.5.4 although that required having the hooks as files in the .huskyrc folder following https://oscarmarion.dev/articles/husky-git-hooks.

I didn't get anything working the Husky@4 way.

Also I noticed you’re having npm run lint which is probably linting all files and not taking advantage of lint-staged. Something worth checking.

@okonet Good point, it only mentions files that were actually staged so I would think it's not that 🤔.

@gleisonkz I tried two (well one that actually uses lint-staged), the first one running husky@4.2.1 and pretty-quick (so no involvement of lint-staged) and one using husky@6.0.0 and lint-staged@10.5.4 although that required having the hooks as files in the .huskyrc folder following https://oscarmarion.dev/articles/husky-git-hooks.

I didn't get anything working the Husky@4 way.

I tried to use this setup but lint-staged is not getting triggered when I commit. Unfortunately, The only way I get it working was by linting all files.

@gleisonkz Were you running on the same versions and using npx?

So I have absolutely the same issue as @Berkmann18 (on macos 11.2.3).

for linting only it seems...

this version executes ng lint and errors

...
  "lint-staged": {
      "*.ts": [
          "ng lint"
      ],
      "*{.ts,.html,.scss,.json,.js}": [
          "npm run prettier:verify"
      ]
  },
...

the error:

✖ ng lint:
An unhandled exception occurred: Project '/xxx/xxxx.ts' does not exist.
See "/xxx/angular-errors.log" for further details.

this version doesn't execute ng lint - [SKIPPED] No staged files match *{.ts}

...
  "lint-staged": {
      "*{.ts}": [
          "ng lint"
      ],
      "*{.ts,.html,.scss,.json,.js}": [
          "npm run prettier:verify"
      ]
  },
...

Funny enough if I have *{.ts,.ts} ng lint is executed and it errors

EDIT: check #951 (comment)

@jose-barata I believe *.{ts} doesn't work as it only contains a single value. It seems to be feature of https://github.com/micromatch/braces

❯ node -e "console.log(require('braces')('{x,y}', { expand:true }))"
[ 'x', 'y' ]

❯ node -e "console.log(require('braces')('{x}', { expand:true }))" 
[ '{x}' ]

EDIT: I opened an issue regarding this, lets see: micromatch/braces#32

It seems that lint-staged doesn't play very well with angular and ng lint (I think that's the specific and underlying issue).

Might help some people (I took ideas out of it) - #547

As for me I'm using as of time of writting (if it matters):
angular 11.2.10
husky 6.0.0
lint-staged 10.5.4
macos 11.2.3

In my particular case, I migrated to eslint (since tslint was deprecated). So I thought about running eslint directly instead of running eslint through ng lint

I managed to fix all issues with:

My husky pre-commit command
npx lint-staged --config lint-staged.config.js

lint-staged.config.js

module.exports = {
    'src/**/*.ts': (filesPath) => {
        return [`node_modules/.bin/eslint -c .eslintrc.json ${filesPath.join(' ')}`];
    },
    'projects/design/**/*.ts': (filesPath) => {
        return [`node_modules/.bin/eslint -c projects/design/.eslintrc.json  ${filesPath.join(' ')}`];
    },
    '*{.ts,.html,.scss,.json,.js}': ['npm run prettier:verify'],
};

Note: I have a monorepo structure with 'src' and 'projects' folders both with their own eslint rules

@Berkmann18 and others who see "Project ... does not exist" with angular:

ng lint needs a project name (see here: https://angular.io/cli/lint), so try to use ng lint <YOUR_PROJECT_NAME>. Otherwise, ng lint tries to use the staged files returned by lint-staged as project name what will not work for obvious reasons.

Please note: This will probably only fix the "Project... does not exist" error, afterwards there are probably some more angular specific issues to consider, see @jose-barata 's comment and #547

@musicEnfanthen this seems like a good example to add to the readme! Could you open a PR, or provide an example config on how to use ng lint? Thanks!

I think ng lint should not be used with lint-stages anyways since it lints the whole project. Just add it to pre-commit

@iiroj Since this is quite specific to angular syntax, I am not sure if @okonet would like to see this included in the README of lint-staged. For example configs, please have a look at @jose-barata 's comment above and at different workarounds proposed in #547. (As @okonet pointed out, at the moment there is no real benefit of using ng lint within lint-staged pipeline.)

Works for me. It just seems to be a common issue, so maybe the README could state in the FAQ section that ng lint should be used directly in the pre-commit hook, and not through lint-staged.

Any update on this? Is it possible to use lint-staged with ng-lint?
I’m still getting an error on angular 13

PR are welcome! Let's add a section to the FAQ that says ng lint should not be used through lint-staged, but directly in the git hook.

For now I'll close this ticket.

Is it working now?. I am having the lint-staged@12.6.7 but facing the same error

commented

Same here

It is possible but only if you use https://www.npmjs.com/package/ng-lint-staged
It's a real bummer and annoying that ng lint can't work with individual files, this is something they should fix to remove the dependence on a third-party fix

@michahell I follow the steps in https://www.npmjs.com/package/ng-lint-staged but also doesn't work.
Any example please?

@michahell I follow the steps in https://www.npmjs.com/package/ng-lint-staged but also doesn't work. Any example please?

Can you provide sample code to re create the issue?

I tried to use like following code which has worked.

 "lint-staged": {
    "src/**/*.{ts,scss,html}": "npx eslint src"
  }

@thengstermanndev But this way you will lint whole project instead of only staged files.