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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed files with specific filenames in Windows cannot be staged with with stage_fixed #505

Closed
ntsim opened this issue Jun 17, 2023 · 1 comment 路 Fixed by #506
Closed

Fixed files with specific filenames in Windows cannot be staged with with stage_fixed #505

ntsim opened this issue Jun 17, 2023 · 1 comment 路 Fixed by #506
Labels
bug Something isn't working

Comments

@ntsim
Copy link

ntsim commented Jun 17, 2023

馃敡 Summary

When using the stage_fixed option in Windows, Lefthook will fail to commit files with that have been fixed.

This only occurs when trying to commit with file patterns that need escaping like [slug].js (a common Next.js filename convention).

Here is my config:

pre-commit:
  commands:
    prettier:
      glob: '*.{js,jsx,ts,tsx,css,scss,md,html,json,yml}'
      run: npx prettier --write "{staged_files}"
      stage_fixed: true

Note that I've already ran into some trouble with getting Prettier to actually run on staged_files, hence the need to double-quote them. This was inferred from the issue on Windows escaping issues - #314.

Lefthook version

1.4.2

Steps to reproduce

  1. Clone repo in Windows - https://github.com/ntsim/lefthook-windows-bug
  2. Install dependencies - npm ci
  3. Change [slug].js in some way.
  4. Commit changes. This will succeed, but will output something like the following:
RUNNING HOOK: pre-commit

  EXECUTE > prettier
[slug].js 46ms

Couldn't stage fixed files:exit status 128

SUMMARY: (done in 1.62 seconds)
鉁旓笍  prettier
[main e89357b] Test
 1 file changed, 1 insertion(+), 1 deletion(-)
  1. Run git status and you will see that [slug].js has been modified, but the commit doesn't contain Prettier's fixes.

Optional steps:

  1. Repeat formatting changes with index.js.
  2. Commit again and fixes to index.js will be part of the commit.

Expected results

Files with patterns like [slug].js should be committed when they are fixed by a hook using tools like Prettier.

Actual results

The fixes are not committed, but the commit itself still succeeds.

Possible Solution

It seems like filenames are being passed to git add using single-quotes (see detailed log below), however, this doesn't work in Windows.

I think a simple fix for this would be to just use always use double-quotes to escape filenames, or at least use them when Windows is detected as the OS?

Perhaps some of the changes from #314 may also be useful for fixing this?

Logs / Screenshots

Running with LEFTHOOK_VERBOSE=true, I get the following log when changing [slug].js:

RUNNING HOOK: pre-commit
[lefthook] cmd: [git status --short]
[lefthook] err: <nil>
[lefthook] out: M  [slug].js

[lefthook] cmd: [git diff --name-only --cached --diff-filter=ACMR]
[lefthook] err: <nil>
[lefthook] out: [slug].js

[lefthook] files before filters:
[[slug].js]
[lefthook] files after filters:
[[slug].js]
[lefthook] files after escaping:
['[slug].js']
[lefthook] executing: npx prettier --write "[slug].js"

  EXECUTE > prettier
[slug].js 48ms

[lefthook] cmd: [git add '[slug].js']
[lefthook] err: exit status 128
[lefthook] out: fatal: pathspec ''[slug].js'' did not match any files

Couldn't stage fixed files:exit status 128
[lefthook] cmd: [git stash list]
[lefthook] err: <nil>
[lefthook] out: stash@{0}: WIP on main: 5f16ae5 Initial commit


SUMMARY: (done in 1.63 seconds)
鉁旓笍  prettier
@ntsim ntsim added the bug Something isn't working label Jun 17, 2023
@mrexox
Copy link
Member

mrexox commented Jun 19, 2023

Hey! Thank you for reporting. I could reproduce this bug on my Linux machine, so I'll try to fix it. Unfortunately, I don't have an access to Windows machine right now, so the fix might not work for you. If so, please come back to this issue and add a comment about it.

The fix will be in version 1.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants