Why a Stop Hook JSON Error Came from printf %b
๐ Originally published (in Japanese) at forge.workstyle.tech . TL;DR The Stop hook reported Unterminated string because printf with %b emitted a raw newline inside a JSON string. The hook also used a cd path fixed to an earlier environment. Its change count missed staged and untracked files when it checked only git diff --name-only . TL;DR The Stop hook error Hook output looks like a JSON objectโฆ
The Stop hook reported an "Unterminated string" error due to the printf commandโs %b format specifier emitting raw newlines inside a JSON string. This occurred because the hook still used a hardcoded cd path from an earlier environment, causing the count of Git changes to miss staged and untracked files. The error message "JSON Parse error: Unterminated string" indicated that the output, although resembling a JSON object, was not valid JSON.
To resolve the issue, the hook was modified to generate JSON using Pythonโs json.dumps function, and its execution location was determined at runtime instead of using a hard-coded cd command. Additionally, the change count was adjusted to include staged and untracked files using git status --porcelain=v1 -z. The revised hook also ensured that the output was valid JSON, even when filenames contained special characters like newlines, quotation marks, backslashes, Japanese text, and spaces.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.