nx: affected:lint with lint-staged hangs in nx monorepo

I am using affected:lint with lint-staged and noticed it hangs forever when I run npx lint-staged. wonder if I am doing something wrong.

The command npm run affected:lint -- --uncommitted --fix --parallel itself runs fine.

  "lint-staged": {
    "{apps,libs}/**/*.{ts,json,md,scss}": [
      "npm run affected:lint  -- --uncommitted --fix --parallel",
      "npm run format:write -- --uncommitted",
      "git add"
    ]
  },

Ref: https://github.com/okonet/lint-staged/issues/522

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 25 (12 by maintainers)

Most upvoted comments

Thanks @meeroslav for confirming the = is not necessary. To amend the previous solution there is no need to use the lint-staged js configuration the following should work, and long list of files that exceed the command length max will be chunked correctly

.husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged --relative 

.lintstagedrc.json

{
  "*": ["nx affected:lint --fix --files", "nx format:write --files"]
}

The above is not working for me I get the following error:

❯ yarn run lint-staged
yarn run v1.22.17
$ /Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/.bin/lint-staged
✔ Preparing lint-staged...
⚠ Running tasks for staged files...
  ❯ .lintstagedrc.json — 2 files
    ❯ * — 2 files
      ✖ nx affected:lint --fix --files [FAILED]
      ◼ nx format:write --files
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ nx affected:lint --fix --files:
nx affected:lint

Lint projects affected by changes

Run command using --base=[SHA1] (affected by the committed, uncommitted and untracked changes):
  --base  Base of the current branch (usually main)  [string]

or using --base=[SHA1] --head=[SHA2] (affected by the committed changes):
  --base  Base of the current branch (usually main)  [string]
  --head  Latest commit of the current branch (usually HEAD)  [string]

or using:
  --files        Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas  [array]
  --uncommitted  Uncommitted changes  [boolean]
  --untracked    Untracked changes  [boolean]

Options:
  --help           Show help  [boolean]
  --version        Show version number  [boolean]
  --parallel       Max number of parallel processes [default is 3]  [string]
  --all            All projects  [boolean]
  --exclude        Exclude certain projects from being processed  [array] [default: []]
  --runner         This is the name of the tasks runner configured in nx.json  [string]
  --skip-nx-cache  Rerun the tasks even when the results are available in the cache  [boolean] [default: false]
  --configuration  This is the configuration to use when performing tasks on projects  [string]
  --only-failed    Isolate projects which previously failed  [deprecated: The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.] [boolean] [default: false]
  --verbose        Print additional error stack trace on failure

Examples:
  affected:lint --parallel=5                     Run lint in parallel
  affected:lint --all                            Run the lint target for all projects
  affected:lint --files=libs/mylib/src/index.ts  Run lint for all the projects affected by changing the index.ts file
  affected:lint --base=main --head=HEAD          Run lint for all the projects affected by the changes between main and HEAD (e.g., PR)
  affected:lint --base=main~1 --head=main        Run lint for all the projects affected by the last commit on main

Find more information and examples at https://nx.dev/cli/affected-lint

RangeError: path should be a `path.relative()`d string, but got "/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/.lintstagedrc.json"
    at throwError (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/ignore/index.js:364:9)
    at checkPath (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/ignore/index.js:383:12)
    at Ignore._test (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/ignore/index.js:504:5)
    at Ignore.ignores (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/ignore/index.js:549:17)
    at /Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/@nrwl/workspace/src/core/file-utils.js:34:41
    at Array.filter (<anonymous>)
    at calculateFileChanges (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/@nrwl/workspace/src/core/file-utils.js:34:19)
    at projectsToRun (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/@nrwl/workspace/src/command-line/affected.js:91:108)
    at Object.<anonymous> (/Users/rrajaratnam/Tresors/Code/2022/sourcier-web/node_modules/@nrwl/workspace/src/command-line/affected.js:25:26)
    at Generator.next (<anonymous>)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
~/T/Code/2022/sourcier-web develop +3 ❯                                                         ▼ 02:30:38

EDIT: Updated solution here https://github.com/nrwl/nx/issues/869#issuecomment-1022345603

If anyone ends up here again lint-staged is using the full file name not the relative file name. Adding --relative https://github.com/okonet/lint-staged#command-line-flags when invoking lint-staged and using js config to concatenate the file names solve the issue for me

.husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged --relative 

lint-staged.config.js

module.exports = {
  '*': (filesArray) => {
    const files = filesArray.join();
    return [
      `nx affected:lint --fix --files=${files}`,
      `nx format:write --files=${files}`,
    ];
  },
};

I am a little concerned about the length of the command when making large changes, but this has worked for me a few times

If you want to try to figure out what is going on lint-staged has a --debug flag, from what I see when using the standard json config it is not sending the files for affected:lint but it is for format:write

@sourcier this happens if you omit the --relative flag?

@JoA-MoS, the signature --files= something is incorrect as variable value needs to follow equals sign if one is added. So this is not an issue in Nx. I’m wondering how format:write works though.

These are correct:

# with equals
--files=first,second,third

# without equals
--files first second third

The approach with concatenation or using function as mentioned by @iiroj should be a way to go.

I ended up back here and thought I would post an update. Adding a lint-staged config with these settings seems to work

{
  "*{.js,.ts}": "nx affected:lint --fix --files=",
  "*": "nx format:write --files="
}

here is how I think i solved this if anyone is interested

lint-staged.config.js

module.exports = {
  '*': (files) => [`nx format:write --files=${files.join(',')}`, `git add ${files.join(' ')}`],
};