lint-staged: Running in a subdirectory of a git repo incorrectly runs in other directories

Description

Running lint-staged in a subdirectory of a monorepo incorrectly runs scripts in other parts of the monorepo. Based on a quick look at recent issues and PRs I suspect it might be related to this change, but I’m not positive: https://github.com/okonet/lint-staged/pull/1093

This bug does not exist in lint-staged@11.2.6, but does in 12.3.7 (latest).

Steps to reproduce

Basic reproduction: https://github.com/alex-grover/lint-staged-bug

See the run.sh script for steps. It’s a repo that looks like the following:

lint-staged-bug/
├── parent.txt
├── package.json
├── a/
│   ├── a.txt
│   └── package.json
└── b/
    ├── b.txt
    └── package.json

Each package.json has a lint-staged configuration. If you make and stage changes in every directory, then run lint-staged from inside of a or b, all 3 scripts run rather than just the one in the current child directory.

Debug Logs

expand to view
✔ Preparing lint-staged...
✔ Running tasks for staged files...
✔ Applying modifications from tasks...
✔ Cleaning up temporary files...

→ echo "a modified":
a modified /Users/alexgrover/Developer/lint-staged-bug/a/a.txt

→ echo "parent modified":
parent modified /Users/alexgrover/Developer/lint-staged-bug/parent.txt

→ echo "b modified":
b modified /Users/alexgrover/Developer/lint-staged-bug/b/b.txt

Environment

  • OS: macOS 12.3.1 (M1)
  • Node.js: v14.19.1
  • lint-staged: 12.3.7

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 21 (10 by maintainers)

Most upvoted comments

The monorepo section in the README indicates that cwd only is the expected behavior. That’s how we (and I’m assuming many others) are using the tool, with different lint configurations per directory.

Thanks for confirming, @alex-grover! I’ll add some missing unit test coverage and try to get it released soon.

Can you open a separate issue of the duplicate log output? It might be because of an update to Listr2 or something; not intentional.

I can leave out configs outside the cwd, but I’m pretty sure the behaviour pre-monorepo features would have found the “root config” from a parent directory.

So I think this might work:

  1. search all configs from git
  2. Ignore those outside cwd
  3. If no configs left, auto-discover from cwd (this would be the previous behavior)
  4. If still no configs, throw