evilmartians / lefthook

Fast and powerful Git hooks manager for any type of projects.
MIT License
4.93k stars 215 forks source link

Unable to process files with a $ in their path #651

Closed eikowagenknecht closed 7 months ago

eikowagenknecht commented 8 months ago

:wrench: Summary

Mostly the same as #303, but instead of No files matching the pattern "some/path/to..tsx" were found. I get No files matching the pattern "'some/path/$file.tsx'" were found.

I'm using Windows.

Lefthook version

1.6.3 0845abb2105d2e6870744915df8445af00afd0c0

Steps to reproduce

  1. Have a file with $ in its name.
  2. Run lefthook

lefthook.yml:

pre-commit:
  parallel: false
  commands:
    eslint:
      glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx}"
      run: npx eslint {staged_files}

Expected results

File is processed.

Actual results

Oops! Something went wrong! :(

ESLint: 8.57.0

No files matching the pattern "'src/routes/manage/$cardId.tsx'" were found.
Please check for typing mistakes in the pattern.

Possible Solution

It seems like there are too many quotes arount the staged files. From the console:

Logs / Screenshots

...
│ [lefthook] out: lefthook.yml
src/routes/manage/$cardId.tsx

│ [lefthook] files before filters:
[lefthook.yml src/routes/manage/$cardId.tsx]
│ [lefthook] files after filters:
[src/routes/manage/$cardId.tsx]
│ [lefthook] files after escaping:
['src/routes/manage/$cardId.tsx']
│ [lefthook] executing: npx eslint 'src/routes/manage/$cardId.tsx'
⠇ waiting: eslint-fix
Oops! Something went wrong! :(

ESLint: 8.57.0

No files matching the pattern "'src/routes/manage/$cardId.tsx'" were found.
Please check for typing mistakes in the pattern.
...

I especially notice the line [lefthook] executing: npx eslint 'src/routes/manage/$cardId.tsx', which is exactly what I run from the console. And there it works?

This issue documents the problem for eslint only, but I have the same problem with Biome for example, where a " × The system cannot find the path specified. (os error 3)" error is thrown instead of the message eslint gives. For this as well, running npx biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true 'src/routes/manage/$cardId.tsx' from the console works fine.

The suggested workaround

    eslint-fix:
      glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx}"
      run: node_modules/.bin/eslint {staged_files}

does not work for me and only gives the error message 'node_modules' is not recognized as an internal or external command, operable program or batch file.

mrexox commented 8 months ago

Thank you for the issue. I think the problem occurs because for windows the argument includes the quotes, while on Linux/macOS those quotes are treated differently because the actual command is wrapped with sh. Currently I don't have a workaround for this issue, but I will try to figure it out.

wonu commented 7 months ago

"{staged_files}" works, '{staged_files}' causes the same error.

mrexox commented 7 months ago

@eikowagenknecht , does wrapping {staged_files} in " helps?

pre-commit:
  parallel: false
  commands:
    eslint:
      glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx}"
      run: npx eslint "{staged_files}"
eikowagenknecht commented 7 months ago

Indeed, "{staged_files}" fixes it :-)

mrexox commented 7 months ago

I'm closing this issue since there's a workaround found. Thank you, @wonu!