lint-staged: lint-staged hang for nx monorepo

Description

Steps to reproduce

checkout develop branch

git clone https://github.com/xmlking/ngx-starter-kit -b develop

run lint-staged

npx lint-staged

this command hang forever. you can run lint commands successfully e.g. npm run affected:lint -- --uncommitted --fix --parallel

if you remove --fix --parallel from lint-staged command then the lint-staged works

Debug Logs

expand to view
npx lint-staged -d
  lint-staged:bin Running `lint-staged@8.0.0` +0ms
  lint-staged:find-bin Loaded package.json using `process.cwd()` +0ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Developer/Work/SPA/ngx-starter-kit/package.json`:
  lint-staged { '{apps,libs}/**/*.{ts,json,md,scss}':
  lint-staged    [ 'npm run affected:lint  -- --uncommitted --fix --parallel',
  lint-staged      'npm run format:write -- --uncommitted',
  lint-staged      'git add' ] } +3ms
  lint-staged:cfg Normalizing config +0ms
  lint-staged:cfg Validating config +2ms
Running lint-staged with the following config:
{
  linters: {
    '{apps,libs}/**/*.{ts,json,md,scss}': [
      'npm run affected:lint  -- --uncommitted --fix --parallel',
      'npm run format:write -- --uncommitted',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose'
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Developer/Work/SPA/ngx-starter-kit` +1ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'package.json',
  lint-staged:run   'package-lock.json',
  lint-staged:run   'libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts',
  lint-staged:run   'libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts',
  lint-staged:run   'libs/chat-box/src/lib/services/speech-to-text.service.ts',
  lint-staged:run   'PLAYBOOK.md' ] +29ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +34ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '{apps,libs}/**/*.{ts,json,md,scss}',
  lint-staged:gen-tasks   commands:
  lint-staged:gen-tasks    [ 'npm run affected:lint  -- --uncommitted --fix --parallel',
  lint-staged:gen-tasks      'npm run format:write -- --uncommitted',
  lint-staged:gen-tasks      'git add' ],
  lint-staged:gen-tasks   fileList:
  lint-staged:gen-tasks    [ '/Developer/Work/SPA/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts',
  lint-staged:gen-tasks      '/Developer/Work/SPA/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts',
  lint-staged:gen-tasks      '/Developer/Work/SPA/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts' ] } +17ms
Stashing changes... [started]
Stashing changes... [skipped]
→ No partially staged files found...
Running linters... [started]
Running tasks for {apps,libs}/**/*.{ts,json,md,scss} [started]
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'npm run affected:lint  -- --uncommitted --fix --parallel', 'npm run format:write -- --uncommitted', 'git add' ] +0ms
  lint-staged:find-bin Resolving binary for command `npm run affected:lint  -- --uncommitted --fix --parallel` +206ms
  lint-staged:find-bin Binary for `npm run affected:lint  -- --uncommitted --fix --parallel` resolved to `/Developer/Work/SPA/ngx-starter-kit/node_modules/.bin/npm` +3ms
  lint-staged:task ✔  OS: darwin; File path chunking unnecessary +0ms
  lint-staged:find-bin Resolving binary for command `npm run format:write -- --uncommitted` +0ms
  lint-staged:find-bin Resolving binary for `npm` from cache +0ms
  lint-staged:task ✔  OS: darwin; File path chunking unnecessary +0ms
  lint-staged:find-bin Resolving binary for command `git add` +0ms
  lint-staged:find-bin Binary for `git add` resolved to `/usr/local/bin/git` +1ms
  lint-staged:task ✔  OS: darwin; File path chunking unnecessary +1ms
npm run affected:lint  -- --uncommitted --fix --parallel [started]
  lint-staged:task bin: /Developer/Work/SPA/ngx-starter-kit/node_modules/.bin/npm +0ms
  lint-staged:task args: [ 'run',
  lint-staged:task   'affected:lint',
  lint-staged:task   '--',
  lint-staged:task   '--uncommitted',
  lint-staged:task   '--fix',
  lint-staged:task   '--parallel',
  lint-staged:task   '/Developer/Work/SPA/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts',
  lint-staged:task   '/Developer/Work/SPA/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts',
  lint-staged:task   '/Developer/Work/SPA/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts' ] +0ms
  lint-staged:task opts: { reject: false } +1ms

Environment

  • OS:
  • Node.js: 10
  • lint-staged: v8.0.2

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 26 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Dear @iiroj , thanks for coming back with another idea. I tried your approach (i.e., moving the config to a dedicated file and not having it in the package.json).

This separate config file is used - i can see this, because i changed the glob-file-pattern (i.e., i removed the html type). However, it still shows the respective error.

However, when digging into the nx affected:lint command, i found a --files param, that takes a list of all files to be used - this can be used as a replacement for the --uncommited.

In this context, i changed your config file from above to this one here:

module.exports = {
  '{apps,libs}/**/*.{ts,json,md,scss,html}': files => {
    return [
      `npm run affected:lint -- --fix --parallel --files=${files.join(',')}`,
      'npm run format:write -- --uncommitted',
      `git add ${files.join(' ')}`,
    ];
  },
};

Note that for the affected:lint you need to separate the files by , and for the git command by ' '. For now, this command runs as expected… I tried a commit that previously failed and it now properly commits. I will keep an eye 👀 on this 😆

Thanks a lot for pointing me into the (hopefully) right direction! You’re awesome!

Hi all,

I’m not fully understanding why you nees to run the affected:lint script inside lintstaged. Lintstaged will provide the paths to the files that changed, so we only need to lint these files, not the whole projects affected by these paths, right? By the way, I don’t find any use case for affected:lint script. Could someone clarify this?

Enlightening! You saved my life!

  "scripts": {
+    "eslint:fix": "eslint --fix",
  },
  "lint-staged": {
    "**/*.{ts,tsx,js,jsx,json,md,scss,html}": [
-      "npm run affected:lint  -- --uncommitted --parallel -- --fix",
+      "npm run eslint:fix",
      "npm run format:write -- --uncommitted"
    ]

Lint like flight~~~

Hi all,

I’m not fully understanding why you nees to run the affected:lint script inside lintstaged. Lintstaged will provide the paths to the files that changed, so we only need to lint these files, not the whole projects affected by these paths, right? By the way, I don’t find any use case for affected:lint script. Could someone clarify this?

Hi guys, I can confirm I have the same issue with almost the same setup, mine just includes stylelint as an additional linter. What do you need me to provide you?

Running into the same issue. Strange because I have my company monorepo with the same configuration and it’s not a problem at all. This is a new package and it just hangs and hangs at the end of the run. Showing complete when I run with -d.

I think npm run affected:lint --fix --file is a bad candidate for linting on commit because it could unwrap a list of changed files into all files in the project.

Dear @dsebastien ,

i did something similar. My code, however, looks like this:

const path = require('path');

module.exports = {
  '{apps,libs}/**/*.{ts,json,md,scss,html}': files => {
    const cwd = process.cwd();
    const relPaths = files.map(file => {
      return path.relative(cwd, file);
    });

    return [
      `npm run affected:lint -- --fix --parallel --files=${relPaths.join(',')}`,
      'npm run format:write -- --uncommitted',
      `git add ${files.join(' ')}`,
    ];
  },
};

Note, that my lint-staged script also adds them back to the git. However, the git add command still needs the absolute paths, so i need both… kind of stupid, but whatever 😄

I could fix it with the following changes:

// lint-staged configuration: https://github.com/okonet/lint-staged
// Workaround for issue with NX: https://github.com/okonet/lint-staged/issues/522

const path = require("path");
const cwd = process.cwd();

console.log("Executing lint-staged based on lint-staged.config.js");

module.exports = {
  "{apps,libs}/**/*.{ts,json,md,scss,html}": absolutePaths => {
    const relativePaths = absolutePaths.map(file => path.relative(cwd, file))
    const filesList = relativePaths.join(",");

    const lintCommand = `npm run affected:lint -- --fix --parallel --files=${filesList}`;
    console.debug("Lint command that will be executed: ", lintCommand);
    const formatCommand = "npm run format:all";
    console.debug("Format command that will be executed: ", formatCommand);

    return [lintCommand, formatCommand];
  },
};

@johannesschobel You are welcome! A lot of issues with Angular seem to be solvable by using the new javascript syntax for configuration.

Yes, npm run affected:lint -- --uncommited --fix --parallel works, but if you append it with the absolute filepaths of staged files (as lint-staged does), it will fail because ng treats them as flags.

So after staging changes in ./libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts, this fails:

npm run affected:lint -- --uncommitted --fix --parallel /git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts

Can you try running it with the example configuration I provided?

This might be because the ng command treats the staged filepaths added by lint-staged as flags rather than input files.

For what it’s worth, when trying to run the command found in the debug log it also fails:

View logs
~/git/ngx-starter-kit develop*
❯ npm run affected:lint -- --uncommitted --fix --parallel /git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts /git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts /git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts

> ngx-starter-kit@3.0.0 affected:lint /git/ngx-starter-kit
> nx affected:lint "--uncommitted" "--fix" "--parallel" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts"


>  NX  Running target lint for projects:

  - chat-box
  - dashboard
  - webapp
  
  With flags: /git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts /git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts /git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts --fix=true


———————————————————————————————————————————————


> ngx-starter-kit@3.0.0 ng /git/ngx-starter-kit
> ng "lint" "chat-box" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"


> ngx-starter-kit@3.0.0 ng /git/ngx-starter-kit
> ng "lint" "dashboard" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"


> ngx-starter-kit@3.0.0 ng /git/ngx-starter-kit
> ng "lint" "webapp" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"

Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ngx-starter-kit@3.0.0 ng: `ng "lint" "chat-box" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ngx-starter-kit@3.0.0 ng 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!     /.npm/_logs/2019-09-17T15_35_42_080Z-debug.log
Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ngx-starter-kit@3.0.0 ng: `ng "lint" "webapp" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ngx-starter-kit@3.0.0 ng 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!     /.npm/_logs/2019-09-17T15_35_42_140Z-debug.log
Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts'
Unknown option: '/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ngx-starter-kit@3.0.0 ng: `ng "lint" "dashboard" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts" "--fix=true"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ngx-starter-kit@3.0.0 ng 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!     /.npm/_logs/2019-09-17T15_35_42_358Z-debug.log


———————————————————————————————————————————————


>  NX   ERROR  Running target "lint" for affected projects failed

  Failed projects:
  
  - chat-box
  - dashboard
  - webapp

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ngx-starter-kit@3.0.0 affected:lint: `nx affected:lint "--uncommitted" "--fix" "--parallel" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/router-link-match/router-link-match.directive.spec.ts" "/git/ngx-starter-kit/libs/ngx-utils/src/lib/directives/ng-let/ng-let.directive.spec.ts" "/git/ngx-starter-kit/libs/chat-box/src/lib/services/speech-to-text.service.ts"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ngx-starter-kit@3.0.0 affected: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!     /.npm/_logs/2019-09-17T15_35_42_384Z-debug.log

I’d wager to get it working you should write the lint-staged config in JS and omit the filenames:

// lint-staged.config.js
module.exports = {
  '{apps,libs}/**/*.{ts,json,md,scss}': files => {
    const fileArg = files.join(' ')
    return [
      "npm run affected:lint  -- --uncommitted --fix --parallel", // <-- no files
      "npm run format:write -- --uncommitted", // <-- no files
      `git add ${fileArg}`
    ]
  }
}

This is of course assuming the --uncommitted flag means that the command itself finds the files it will lint and format.