Skip to content

hook: backslash-newline is collapsed inside single quotes and comments, changing the command bash runs #5090

Open

Description

@alfredopons

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_RE and collapse_line_continuations are 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

Activity

  1. seragAhmed commented on Nov 14, 2026

    @seragAhmed

    Hi, I'd like to take this. Plan: replace the regex in collapse_line_continuations with 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 plain git log \⏎ --oneline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions