Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint-staged failing with lerna/yarn workspaces since v12.3.3 #1105

Closed
bolloga opened this issue Feb 14, 2022 · 7 comments
Closed

lint-staged failing with lerna/yarn workspaces since v12.3.3 #1105

bolloga opened this issue Feb 14, 2022 · 7 comments

Comments

@bolloga
Copy link

bolloga commented Feb 14, 2022

Lint-staged started erroring out since we upgraded to v12.3.3. Lerna runs lint-staged in each package within our monorepo. At times it throws a git error for large merges/diffs, but since this version lint-staged fails on every run. I'd like to avoid adding a concurrency rule to keep the pre-commit hook snappy.

I was able to fix the issue by adding a cwd . arg to each individual package.json files, but this feels hacky. I'm not sure I fully understand the original fix introducing the issue for us (#1091), but I'm under the impression that for monorepos it always makes sense to run in the current working directory?

Steps to reproduce

  • Repo structure:
├── package.json # root package.json
└── packages/
    └── components/
        ├── my-component/
        │   └── package.json # my-component-package.json
        └── my-other-component/
            └── package.json # my-other-component-package.json
  • Root package.json
{
  "name": "root-package.json"
  "scripts": {
    "lint:pre-commit": "lerna run lint:pre-commit --"
  },
  "workspaces": [
    "packages/components/*"
  ],
  "devDependencies": {
    "lint-staged": "^12.3.4"
  }
}
  • Children package.json
  "name": "my-other-component-package.json,
  "scripts": {
    "lint:fix": "eslint --fix 'src/**/*.ts?(x)'",
    "lint:pre-commit": "lint-staged --cwd ." // fails without --cwd
  },
  "lint-staged": {
    "**/*.ts?(x)": [
      "yarn lint:fix"
    ]
  }
}

Debug Logs

expand to view
lerna ERR! yarn run lint:pre-commit stdout:
$ lint-staged --debug
[STARTED] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[SKIPPED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SKIPPED] 
[SKIPPED]   ✖ lint-staged failed due to a git error.
[STARTED] Cleaning up temporary files...
[SKIPPED] 
[SKIPPED]   ✖ lint-staged failed due to a git error.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
lerna ERR! yarn run lint:pre-commit stderr:
2022-02-14T07:46:06.539Z lint-staged:bin Running `lint-staged@12.3.3`
2022-02-14T07:46:06.540Z lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  cwd: undefined,
  debug: true,
  maxArgLength: 131072,
  quiet: false,
  relative: false,
  shell: false,
  stash: true,
  verbose: false
}
2022-02-14T07:46:06.541Z lint-staged:validateOptions Validating options...
2022-02-14T07:46:06.541Z lint-staged:validateOptions Validated options!
2022-02-14T07:46:06.541Z lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`)
2022-02-14T07:46:06.542Z lint-staged:runAll Running all linter scripts...
2022-02-14T07:46:06.542Z lint-staged:runAll Using working directory `repo_path/components/performance`
2022-02-14T07:46:06.542Z lint-staged:resolveGitRepo Resolving git repo from `repo_path/components/performance`
2022-02-14T07:46:06.542Z lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`)
2022-02-14T07:46:06.542Z lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`)
2022-02-14T07:46:06.542Z lint-staged:execGit Running git command [ 'rev-parse', '--show-prefix' ]
2022-02-14T07:46:06.571Z lint-staged:resolveGitRepo Resolved git directory to be `repo_path`
2022-02-14T07:46:06.572Z lint-staged:resolveGitRepo Resolved git config directory to be `repo_path/.git`
2022-02-14T07:46:06.572Z lint-staged:execGit Running git command [ 'log', '-1' ]
2022-02-14T07:46:06.594Z lint-staged:execGit Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only', '-z' ]
2022-02-14T07:46:06.619Z lint-staged:runAll Loaded list of staged files in git:
[
  'repo_path/package.json',
  'repo_path/components/background-container/src/constants.ts',
  'repo_path/components/some-component/package.json',
  'repo_path/components/some-component/src/ui/error-screen/index.tsx'
]
2022-02-14T07:46:06.619Z lint-staged:getConfigGroups Grouping configuration files...
2022-02-14T07:46:06.619Z lint-staged:getConfigGroups Grouping staged files by their directories...
2022-02-14T07:46:06.619Z lint-staged:getConfigGroups Grouped staged files into 4 directories:
2022-02-14T07:46:06.620Z lint-staged:getConfigGroups {
  'repo_path': [ 'repo_path/package.json' ],
  'repo_path/components/background-container/src': [ 'repo_path/components/background-container/src/constants.ts' ],
  'repo_path/components/some-component': [ 'repo_path/components/some-component/package.json' ],
  'repo_path/components/some-component/src/ui/error-screen': [ 'repo_path/components/some-component/src/ui/error-screen/index.tsx' ]
}
2022-02-14T07:46:06.621Z lint-staged:getConfigGroups Searching config files...
2022-02-14T07:46:06.621Z lint-staged:loadConfig Searching for configuration from `repo_path/components/performance`...
2022-02-14T07:46:06.622Z lint-staged:loadConfig Successfully loaded config from `repo_path/components/performance/package.json`:
{ '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write' ] }
2022-02-14T07:46:06.622Z lint-staged:getConfigGroups Found new config "repo_path/components/performance/package.json" from "repo_path/components/performance"!
2022-02-14T07:46:06.623Z lint-staged:validateConfig Validating config from `repo_path/components/performance/package.json`...
2022-02-14T07:46:06.623Z lint-staged:validateConfig Validated config from `repo_path/components/performance/package.json`:
2022-02-14T07:46:06.623Z lint-staged:validateConfig {
  '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write' ]
}
2022-02-14T07:46:06.623Z lint-staged:loadConfig Searching for configuration from `repo_path`...
2022-02-14T07:46:06.624Z lint-staged:loadConfig Searching for configuration from `repo_path/components/background-container/src`...
2022-02-14T07:46:06.624Z lint-staged:loadConfig Searching for configuration from `repo_path/components/some-component`...
2022-02-14T07:46:06.624Z lint-staged:loadConfig Searching for configuration from `repo_path/components/some-component/src/ui/error-screen`...
2022-02-14T07:46:06.627Z lint-staged:loadConfig Successfully loaded config from `repo_path/components/some-component/package.json`:
{ '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write' ] }
2022-02-14T07:46:06.627Z lint-staged:getConfigGroups Found new config "repo_path/components/some-component/package.json" from "repo_path/components/some-component"!
2022-02-14T07:46:06.627Z lint-staged:validateConfig Validating config from `repo_path/components/some-component/package.json`...
2022-02-14T07:46:06.627Z lint-staged:validateConfig Validated config from `repo_path/components/some-component/package.json`:
2022-02-14T07:46:06.627Z lint-staged:validateConfig {
  '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write' ]
}
2022-02-14T07:46:06.629Z lint-staged:loadConfig Successfully loaded config from `repo_path/components/background-container/package.json`:
{
  '**/*.ts?(x)': [
    'yarn lint:fix',
    'yarn prettier --write --no-error-on-unmatched-pattern'
  ]
}
2022-02-14T07:46:06.629Z lint-staged:getConfigGroups Found new config "repo_path/components/background-container/package.json" from "repo_path/components/background-container/src"!
2022-02-14T07:46:06.629Z lint-staged:validateConfig Validating config from `repo_path/components/background-container/package.json`...
2022-02-14T07:46:06.629Z lint-staged:validateConfig Validated config from `repo_path/components/background-container/package.json`:
2022-02-14T07:46:06.629Z lint-staged:validateConfig {
  '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write --no-error-on-unmatched-pattern' ]
}
2022-02-14T07:46:06.630Z lint-staged:getConfigGroups Found no config from "repo_path"!
2022-02-14T07:46:06.631Z lint-staged:loadConfig Successfully loaded config from `repo_path/components/some-component/package.json`:
{ '**/*.ts?(x)': [ 'yarn lint:fix', 'yarn prettier --write' ] }
2022-02-14T07:46:06.631Z lint-staged:getConfigGroups Found existing config "repo_path/components/some-component/package.json" from "repo_path/components/some-component/src/ui/error-screen"!
2022-02-14T07:46:06.631Z lint-staged:getConfigGroups Grouped staged files into 3 groups!
2022-02-14T07:46:06.631Z lint-staged:chunkFiles Resolved an argument string length of 0 characters from 0 files
2022-02-14T07:46:06.631Z lint-staged:chunkFiles Creating 0 chunks for maxArgLength of 131072
2022-02-14T07:46:06.631Z lint-staged:chunkFiles Resolved an argument string length of 170 characters from 2 files
2022-02-14T07:46:06.631Z lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 131072
2022-02-14T07:46:06.631Z lint-staged:generateTasks Generating linter tasks
2022-02-14T07:46:06.634Z lint-staged:generateTasks Generated task: 
{
  pattern: '**/*.ts?(x)',
  commands: [ 'yarn lint:fix', 'yarn prettier --write' ],
  fileList: [
    'repo_path/components/some-component/src/ui/error-screen/index.tsx'
  ]
}
2022-02-14T07:46:06.635Z lint-staged:makeCmdTasks Creating listr tasks for commands [ 'yarn lint:fix', 'yarn prettier --write' ]
2022-02-14T07:46:06.636Z lint-staged:resolveTaskFn cmd: yarn
2022-02-14T07:46:06.636Z lint-staged:resolveTaskFn args: [ 'lint:fix' ]
2022-02-14T07:46:06.636Z lint-staged:resolveTaskFn execaOptions: {
  cwd: 'repo_path/components/some-component',
  preferLocal: true,
  reject: false,
  shell: false
}
2022-02-14T07:46:06.637Z lint-staged:resolveTaskFn cmd: yarn
2022-02-14T07:46:06.637Z lint-staged:resolveTaskFn args: [ 'prettier', '--write' ]
2022-02-14T07:46:06.637Z lint-staged:resolveTaskFn execaOptions: {
  cwd: 'repo_path/components/some-component',
  preferLocal: true,
  reject: false,
  shell: false
}
2022-02-14T07:46:06.637Z lint-staged:chunkFiles Resolved an argument string length of 93 characters from 1 files
2022-02-14T07:46:06.637Z lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 131072
2022-02-14T07:46:06.637Z lint-staged:generateTasks Generating linter tasks
2022-02-14T07:46:06.638Z lint-staged:generateTasks Generated task: 
{
  pattern: '**/*.ts?(x)',
  commands: [
    'yarn lint:fix',
    'yarn prettier --write --no-error-on-unmatched-pattern'
  ],
  fileList: [
    'repo_path/components/background-container/src/constants.ts'
  ]
}
2022-02-14T07:46:06.638Z lint-staged:makeCmdTasks Creating listr tasks for commands [ 'yarn lint:fix', 'yarn prettier --write --no-error-on-unmatched-pattern' ]
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn cmd: yarn
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn args: [ 'lint:fix' ]
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn execaOptions: {
  cwd: 'repo_path/components/background-container',
  preferLocal: true,
  reject: false,
  shell: false
}
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn cmd: yarn
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn args: [ 'prettier', '--write', '--no-error-on-unmatched-pattern' ]
2022-02-14T07:46:06.638Z lint-staged:resolveTaskFn execaOptions: {
  cwd: 'repo_path/components/background-container',
  preferLocal: true,
  reject: false,
  shell: false
}
2022-02-14T07:46:06.639Z lint-staged:chunkFiles Resolved an argument string length of 187 characters from 2 files
2022-02-14T07:46:06.639Z lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 131072
2022-02-14T07:46:06.643Z lint-staged:GitWorkflow Backing up original state...
2022-02-14T07:46:06.643Z lint-staged:GitWorkflow Getting partially staged files...
2022-02-14T07:46:06.643Z lint-staged:execGit Running git command [ 'status', '-z' ]
2022-02-14T07:46:06.739Z lint-staged:GitWorkflow Found partially staged files: []
2022-02-14T07:46:06.739Z lint-staged:GitWorkflow Backing up merge state...
2022-02-14T07:46:06.739Z lint-staged:file Reading file `repo_path/.git/MERGE_HEAD`
2022-02-14T07:46:06.739Z lint-staged:file Reading file `repo_path/.git/MERGE_MODE`
2022-02-14T07:46:06.739Z lint-staged:file Reading file `repo_path/.git/MERGE_MSG`
2022-02-14T07:46:06.740Z lint-staged:file File `repo_path/.git/MERGE_HEAD` doesn't exist, ignoring...
2022-02-14T07:46:06.740Z lint-staged:file File `repo_path/.git/MERGE_MODE` doesn't exist, ignoring...
2022-02-14T07:46:06.740Z lint-staged:file File `repo_path/.git/MERGE_MSG` doesn't exist, ignoring...
2022-02-14T07:46:06.740Z lint-staged:GitWorkflow Done backing up merge state!
2022-02-14T07:46:06.740Z lint-staged:GitWorkflow Getting deleted files...
2022-02-14T07:46:06.740Z lint-staged:execGit Running git command [ 'ls-files', '--deleted' ]
2022-02-14T07:46:06.767Z lint-staged:GitWorkflow Found deleted files: []
2022-02-14T07:46:06.767Z lint-staged:execGit Running git command [ 'stash', 'create' ]

  ✖ lint-staged failed due to a git error.
  Any lost modifications can be restored from a git stash:

    > git stash list
    stash@{0}: automatic lint-staged backup
    > git stash apply --index stash@{0}

error Command failed with exit code 1.
lerna ERR! yarn run lint:pre-commit exited 1 in '@package-namespace/performance'
lerna WARN complete Waiting for 3 child processes to exit. CTRL-C to exit immediately.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Command failed: node /usr/local/Cellar/yvm/4.1.4/versions/v1.22.17/bin/yarn.js lint:pre-commit

Environment

  • OS: macOS Monterey
  • Node.js: v16.13.1
  • lint-staged: v12.3.3
@iiroj
Copy link
Member

iiroj commented Feb 14, 2022

Instead of running lint-staged multiple times in your packages, you should run it once in the root directory.

@bolloga
Copy link
Author

bolloga commented Feb 14, 2022

I see. I'm probably missing something, but when I run lint-staged from the root directory, it's not aware of the yarn scripts defined in the component's package.json:

⚠ Running tasks for staged files...
  ❯ packages/components/my-other-component/.lintstagedrc — 1 file
    ❯ **/*.ts?(x) — 1 file
      ✖ yarn lint:fix [FAILED]
...
✖ yarn lint:fix:
error Command "lint:fix" not found.

I don't have this problem if I replace the yarn script with just eslint --fix, but it won't work if I start specifying paths:

⚠ Running tasks for staged files...
  ❯ packages/components/my-other-component/package.json — 1 file
    ❯ **/*.ts?(x) — 1 file
      ✖ prettier ./src/**/* [FAILED]
...
[error] No files matching the pattern were found: "./src/**/*".

@iiroj
Copy link
Member

iiroj commented Feb 14, 2022

What's the cwd in that case? Maybe the debug logs show it.

Anyway, the problem with running lint-staged in parallel is that git has locking operations, and then fails on those.

@iiroj
Copy link
Member

iiroj commented Feb 15, 2022

This will probably help: #1106

@bolloga
Copy link
Author

bolloga commented Feb 15, 2022

Yes! I checked out your branch and can confirm lint-staged now runs yarn scripts from the right path.

@iiroj
Copy link
Member

iiroj commented Mar 5, 2022

Version 12.3.5 is now released and hopefully fixed this issue. Can you confirm?

jpveooys added a commit to Royal-Navy/design-system that referenced this issue Mar 18, 2022
jpveooys added a commit to Royal-Navy/design-system that referenced this issue Mar 18, 2022
`lint-staged` should be run once from the root directory, and therefore that's where it should be installed.

See https://github.com/okonet/lint-staged#how-to-use-lint-staged-in-a-multi-package-monorepo for more details.

Additionally, running it in parallel causes Git lock conflicts: lint-staged/lint-staged#1105 (comment).
@simonlovesyou
Copy link

@iiroj 12.3.5 fixed it for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants