Repository navigation
Description
What happens
rtk hook claude and rtk rewrite replace every backslash-newline in the command with one space before rewriting. The replacement is a regex over the whole string (LINE_CONTINUATION_RE / collapse_line_continuations in src/discover/registry.rs), with no quote or comment state. In bash a backslash-newline is a line continuation only outside single quotes, outside $'...' and outside a comment. In those contexts the rewritten command is not equivalent to the input:
- inside single quotes bash keeps the backslash and the newline in the string; the rewrite drops both (for example in a
git commit -m '...'message); - inside
$'...', the same; - a comment ends at the newline, so the next line is a separate command; after the collapse that line is comment text and does not run.
The collapsed text only reaches the output when some segment is rewritten. A command with no rewritable segment is passed through unchanged, and commands with a heredoc or $(( are not rewritten at all.
Environment
- rtk 1.2.0, release binary
rtk-x86_64-unknown-linux-musl, Debian 13, GNU bash 5.2.37. - rtk 1.1.0 gives byte-identical output for every case below.
LINE_CONTINUATION_REandcollapse_line_continuationsare unchanged on develop (2a786dc).
Steps
The script below asks rtk rewrite for the rewrite, then runs the input and the rewrite (with rtk removed) in bash against stub git/ls that print their argv:
#!/bin/bash
set -u
d=$(mktemp -d)
for p in git ls; do
printf '#!/bin/bash\nprintf "%%q " "${0##*/}" "$@"; echo\n' > "$d/$p"
chmod +x "$d/$p"
done
run() { env -i PATH="$d" /bin/bash -c "$1"; }
check() {
local cmd=$1 rw
rw=$(rtk rewrite "$cmd")
printf -- '--- input: %q\n rewritten: %q\n' "$cmd" "$rw"
echo ' bash runs input:'; run "$cmd" | sed 's/^/ /'
echo ' bash runs rewrite:'; run "${rw//rtk /}" | sed 's/^/ /'
}
check "git commit -m 'one \\
two' && ls"
check "ls # note \\
ls -la"
check "git log \\
--oneline -n 3"
rm -rf "$d"Expected
Either no rewrite, or a rewrite that bash runs the same way as the input.
Actual
--- input: $'git commit -m \'one \\\ntwo\' && ls'
rewritten: rtk\ git\ commit\ -m\ \'one\ two\'\ \&\&\ rtk\ ls
bash runs input:
git commit -m $'one \\\ntwo'
ls
bash runs rewrite:
git commit -m one\ two
ls
--- input: $'ls # note \\\nls -la'
rewritten: rtk\ ls\ #\ note\ ls\ -la
bash runs input:
ls
ls -la
bash runs rewrite:
ls
--- input: $'git log \\\n --oneline -n 3'
rewritten: rtk\ git\ log\ --oneline\ -n\ 3
bash runs input:
git log --oneline -n 3
bash runs rewrite:
git log --oneline -n 3
The last case is a real continuation and is equivalent, as intended by #1572.
Other measured inputs (same output on 1.1.0 and 1.2.0):
Input (⏎ = newline) |
Rewrite | Equivalent in bash |
|---|---|---|
git commit -m 'l1\⏎l2' |
rtk git commit -m 'l1 l2' |
no |
echo $'a \⏎b' && ls |
echo $'a b' && rtk ls |
no (bash prints a \ and b) |
git status # check \⏎git log -n 1 |
rtk git status # check git log -n 1 |
no (git log does not run, exit 0) |
ls a\<TAB>\⏎b |
rtk ls a\ b |
no (argument a b instead of a<TAB>b) |
echo "a \⏎b" && ls |
echo "a b" && rtk ls |
yes |
[[ -n x \⏎]] && ls |
[[ -n x ]] && rtk ls |
yes |
If the host runs the hook's updatedInput.command as the hook returns it (assumed here for Claude Code, not measured inside it), a commit message is altered or a command after a comment is skipped without any error.
Cause
rewrite_command_precompiled calls collapse_line_continuations(cmd) before trim(), has_heredoc and the lexer. LINE_CONTINUATION_RE ((?m)[ \t\x0B\x0C]*\\\r?\n[ \t\x0B\x0C]*) matches anywhere in the string, and the collapsed string is what rewrite_single / rewrite_compound emit. The regex also consumes horizontal whitespace before the backslash, which is how an escaped tab is lost.
Suggested fix
Collapse only the continuations bash honours: scan the command with quote and comment state (unquoted or inside double quotes, not in a comment, odd run of backslashes) and remove only those \⏎. A conservative alternative: if a \⏎ appears inside single quotes, $'...' or a comment, return no rewrite. The rows above could join the round-trip property from #4132 (removing every inserted rtk gives back the input), which currently has no backslash-newline cases.
Related
- PR refactor(cmdline): one borrowed token type with separator and newline kinds #4341 (open): replaces
collapse_line_continuationsand its two regexes with a lexer-leveljoin_continuations. Its description says a\⏎is joined only outside single quotes and that a\at the end of a comment escapes nothing, which are the single-quote and comment cases above; it does not mention$'...'. Not tested here. - collapse_line_continuations fuses commands when the backslash before a newline is escaped (\\<newline>) #3190 and PR fix(discover): preserve escaped line continuations #3192: an even run of backslashes before the newline (
\\⏎); the odd-run check does not cover quotes or comments. - hooks: permission gate should read the rewrite's own wrapper scan (follow-up to #4530) #4545 (item 3) and PR fix(hook): gate reads the rewrite's own wrapper scan; assignment redirects and continuation joins match bash #4554: bash deletes
\⏎while rtk joins with a space; per its description fix(hook): gate reads the rewrite's own wrapper scan; assignment redirects and continuation joins match bash #4554 switches to deleting it, still without quote or comment state (not tested here). - fix(hook): collapse bash line continuations before matching (#1564) #1572 (fix for Hook matcher: leading backslash-newline defeats command rewrite #1564) introduced the collapse.
Activity
seragAhmed commentedon Nov 14, 2026
Hi, I'd like to take this. Plan: replace the regex in
collapse_line_continuationswith a small scanner that tracks quote and comment state. It would join backslash-newline only outside'...',$'...'and comments, so a comment still ends at the newline. Deny/ask matching (permissions.rs) uses the same function, so both paths would get the fix. Tests would cover the three cases from your script plus a plaingit log \⏎ --oneline.docs(hook): describe where line continuations are joined