Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

srsatt
Copy link
Contributor

@srsatt srsatt commented Aug 7, 2025

fixes #1607

Copy link

changeset-bot bot commented Aug 7, 2025

🦋 Changeset detected

Latest commit: 5100cac

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
lint-staged Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@iiroj
Copy link
Member

iiroj commented Aug 7, 2025

Thanks for the PR! The fix makes sense, but would you be willing to add a test for it so that it doesn't break in the same way in the future?

@srsatt
Copy link
Contributor Author

srsatt commented Aug 8, 2025

Thanks for the PR! The fix makes sense, but would you be willing to add a test for it so that it doesn't break in the same way in the future?

Thanks for the suggestion! In this particular case, adding an automated test would be impractical — the scenario is very specific and would require an extensive amount of mocking to reproduce. That would end up testing the mocks rather than the actual behavior, and it’s likely to be fragile and prone to flakiness. I think in this case it’s safer to rely on code review and manual verification for this edge case.

@iiroj
Copy link
Member

iiroj commented Aug 8, 2025

@srsatt is that an AI reply? 👀 This repo has a lot of integration tests that run in isolated environments without needing any mocking, pretty easy to run lint-staged by supplying the GIT_WORK_TREE env variable, for example copying this test as a starting point: https://github.com/lint-staged/lint-staged/blob/main/test/integration/git-submodules.test.js

@skoch13
Copy link

skoch13 commented Aug 8, 2025

@iiroj I'll try to write a test, but just to make sure: you refered to GIT_WORKTREE env var which is not mentioned in the spec. I guess that was a typo and the env var is GIT_WORK_TREE, isn't it?

@iiroj
Copy link
Member

iiroj commented Aug 8, 2025

@skoch13 yes, oops, you are correct.

@iiroj
Copy link
Member

iiroj commented Aug 8, 2025

I can also release this as a patch without the test if you promise to contribute one separately, given that all the existing tests pass as-is. 👍

@skoch13
Copy link

skoch13 commented Aug 8, 2025

@iiroj I'm failing to reproduce the behavior I described, however my test still green

  test.only(
    'handles monorepo git worktrees',
    withGitIntegration(async ({ appendFile, cwd, execGit, gitCommit, readFile }) => {
           const processEnvBackup = process.env.GIT_DIR

      // creating a separate worktree outside of main worktree directory
      const workTreeDir = await createTempDir()
      const workTreeModuleDir = path.resolve(workTreeDir, 'frontend')
      await execGit(['worktree', 'add', '-b', 'test', workTreeDir])
      await appendFile('.lintstagedrc.json', JSON.stringify(prettierListDifferent), workTreeDir)
      process.chdir(workTreeDir)

      await appendFile('test.js', fileFixtures.prettyJS, workTreeModuleDir)
      await execGit(['add', 'test.js'], { cwd: workTreeModuleDir })
      process.env.GIT_DIR = path.resolve(cwd, '.git', 'worktrees', path.basename(workTreeDir))

      await gitCommit({ lintStaged: { debug: true } }, workTreeModuleDir)

      // Nothing is wrong, so a new commit is created
      expect(await execGit(['rev-list', '--count', 'HEAD'], { cwd: workTreeDir })).toEqual('2')
      expect(await execGit(['log', '-1', '--pretty=%B'], { cwd: workTreeDir })).toMatch('test')
      expect(await readFile('test.js', workTreeModuleDir)).toEqual(fileFixtures.prettyJS)
      process.env.GIT_DIR = processEnvBackup
    })
  )
Screenshot 2025-08-08 at 16 31 09

@iiroj iiroj merged commit 4e3ce22 into lint-staged:main Aug 8, 2025
22 checks passed
@github-actions github-actions bot mentioned this pull request Aug 8, 2025
@iiroj
Copy link
Member

iiroj commented Aug 8, 2025

@skoch13 now that I take fresh look, I think for integration tests one can't directly pass environment variables. Maybe setting the cwd to another directly outside of the test's default one, while having the .git directory be in there, will align the stars correctly.

Unfortunately, I don' use this kind of setups myself so not that familiar with it.

@iiroj
Copy link
Member

iiroj commented Sep 1, 2025

Hello @skoch13 and @srsatt, I'm still waiting for that extra integration test! 👍

@skoch13
Copy link

skoch13 commented Sep 3, 2025

Hello @skoch13 and @srsatt, I'm still waiting for that extra integration test! 👍

Hey @iiroj,
I will, just didn't have spare time for that, the case is tricky.

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

Successfully merging this pull request may close these issues.

Worktree monorepo path resolution bug
3 participants