lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Invalid escaping of {staged_files}

Open stevebeauge opened this issue 6 months ago • 16 comments

:wrench: Summary

If file names contains some utf8 (I guess) characters, lefthook fails because of invalid escaping.

error replacing {staged_files}: CreateFile C:\code\repro\lefthook-wrong-escape\"files\KO \303\251.json": La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.

(part of the message is in French, probably the system error lefthook trapped).

Lefthook version

1.7.7

Running on Windows 10. Both reproed using PowerShell 7 and legacy cmd console.

Steps to reproduce

Clone this repo: https://github.com/stevebeauge/repro-lefthook-wrong-escape

git clone https://github.com/stevebeauge/repro-lefthook-wrong-escape
cd repro-lefthook-wrong-escape
pnpm i

Edit the file files/KO é.json. Set whatever you want in the file. If you are using a powershell console you can run

Get-Date | ConvertTo-Json | Set-Content -LiteralPath '.\files\KO é.json' -Encoding utf8 

Then try to commit the changes:

git add .
git commit -m "Something"

Expected results

It should work whatever the file name is (assuming it's a valid filename for the system).

Actual results

It fails because utf8 character is escaped, but the escape character is itself not properly escaped:

error replacing {staged_files}: CreateFile C:\code\repro\lefthook-wrong-escape"files\KO \303\251.json": La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.

Possible Solution

Logs / Screenshots

Running pnpm lefthook run pre-commit -v in a powershell console generates this logs:

│ [lefthook] cmd: [git rev-parse --show-toplevel]
│ [lefthook] err: <nil>
│ [lefthook] out: C:/code/repro/lefthook-wrong-escape

│ [lefthook] cmd: [git rev-parse --git-path hooks]
│ [lefthook] err: <nil>
│ [lefthook] out: .git/hooks

│ [lefthook] cmd: [git rev-parse --git-path info]
│ [lefthook] err: <nil>
│ [lefthook] out: .git/info

│ [lefthook] cmd: [git rev-parse --git-dir]
│ [lefthook] err: <nil>
│ [lefthook] out: .git

│ [lefthook] cmd: [git hash-object -t tree /dev/null]
│ [lefthook] err: <nil>
│ [lefthook] out: 4b825dc642cb6eb9a060e54bf8d69288fbee4904

╭──────────────────────────────────────╮
│ 🥊 lefthook v1.7.7  hook: pre-commit │
╰──────────────────────────────────────╯
│ [lefthook] cmd: [git status --short --porcelain]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out: M  "files/KO \303\251.json"

│ [lefthook] cmd: [git diff --name-only --cached --diff-filter=ACMR]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out: "files/KO \303\251.json"

│  prettier (skip) error replacing {staged_files}: CreateFile C:\code\repro\lefthook-wrong-escape\"files\KO \303\251.json": La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.
│ [lefthook] cmd: [git stash list]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out:

  ────────────────────────────────────
summary: (done in 0.28 seconds)
🥊  prettier: error replacing {staged_files}: CreateFile C:\code\repro\lefthook-wrong-escape\"files\KO \303\251.json": La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.
╭

As a comparison, modifying files/ok.json is working. It's verbose output is:

│ [lefthook] cmd: [git rev-parse --show-toplevel]
│ [lefthook] err: <nil>
│ [lefthook] out: C:/code/repro/lefthook-wrong-escape

│ [lefthook] cmd: [git rev-parse --git-path hooks]
│ [lefthook] err: <nil>
│ [lefthook] out: .git/hooks

│ [lefthook] cmd: [git rev-parse --git-path info]
│ [lefthook] err: <nil>
│ [lefthook] out: .git/info

│ [lefthook] cmd: [git rev-parse --git-dir]
│ [lefthook] err: <nil>
│ [lefthook] out: .git

│ [lefthook] cmd: [git hash-object -t tree /dev/null]
│ [lefthook] err: <nil>
│ [lefthook] out: 4b825dc642cb6eb9a060e54bf8d69288fbee4904

╭──────────────────────────────────────╮
│ 🥊 lefthook v1.7.7  hook: pre-commit │
╰──────────────────────────────────────╯
│ [lefthook] cmd: [git status --short --porcelain]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out: M  files/ok.json

│ [lefthook] cmd: [git diff --name-only --cached --diff-filter=ACMR]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out: files/ok.json

│ [lefthook] files before filters:
[files/ok.json]
│ [lefthook] files after filters:
[files/ok.json]
│ [lefthook] files after escaping:
[files/ok.json]
│ [lefthook] executing: pnpm prettier files/ok.json
┃  prettier ❯

"2024-07-25T18:29:38.9212089+02:00"

│ [lefthook] cmd: [git stash list]
│ [lefthook] dir: C:/code/repro/lefthook-wrong-escape
│ [lefthook] err: <nil>
│ [lefthook] out:

  ────────────────────────────────────
summary: (done in 1.38 seconds)
✔️  prettier

stevebeauge avatar Jul 25 '24 16:07 stevebeauge