Skip to content

fix(hook): keep backslash-newline inside quotes and comments - #5117

Open
seragAhmed wants to merge 2 commits into
rtk-ai:developfrom
seragAhmed:fix/5090-line-continuation-quotes
Open

seragAhmed wants to merge 2 commits into
rtk-ai:developfrom
seragAhmed:fix/5090-line-continuation-quotes

Conversation

@seragAhmed

Copy link
Copy Markdown

Summary

Fixes #5090

  • collapse_line_continuations is now a small scanner with quote and comment state instead of a regex. It joins backslash-newline (with the blanks around it) into one space only outside single quotes, $'...' and comments. Inside double quotes it drops just the backslash and the newline and keeps the string's own whitespace, which is what bash does. So git commit -m 'one \⏎two' && ls keeps its message, and git commit -m "a \⏎ b" becomes "a b" rather than "a b".
  • A comment that ends in a backslash-newline (ls # note \⏎ls -la) now passes the whole command through. Keeping the newline alone is not enough here: the lexer has no comment state, reads that backslash as escaping the newline, and puts the next line in the comment's segment. With the newline kept, bash would run that line while the hook allows a rewrite that no deny or ask rule was checked against (with git push:* denied and * allowed, ls # note \⏎git push --force gets an Allow verdict). Before this change the rewrite happened to defuse it, because the collapse turned git push into comment text, which also meant it silently changed what ran. Passing through hands the command to the agent's own permission handling.
  • The wrapper permission check keeps joining every continuation (join_all_line_continuations, the old regex). The script of bash -c 'git \⏎push' is parsed again by the inner shell, where that backslash-newline is a continuation, so a deny rule must still see git push (test_line_continuation_is_joined_before_the_wrapper_check).

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all --no-fail-fast. Clippy is clean.
    • All discover/hooks unit tests pass.
    • Two lib tests fail on Windows (find_cmd::run_from_args_propagates_find_exit_status, stream::test_read_lines_lossy_preserves_lines_after_invalid_utf8), and they fail on unmodified develop too.
    • rewrite_shell_wrapper_test and go_test_pipe_test each had one failing test per run here. It was a different test on each run, and it happens on develop too, so these are Windows-specific and flaky.
  • New tests:
    • Collapse: kept inside '...', $'...' and comments (LF and CRLF). Double quotes drop only \⏎. Escaped \', \# and \$' open nothing, and # inside a word is not a comment.
    • Rewrite: the three cases from the issue, the double-quote case, and a plain git log \⏎ --oneline that still joins.
    • Decision: ls # note \⏎git push --force under an Allow verdict is Defer, not AllowRewrite.
    • With the old regex put back, 5 of the 6 new tests fail. The unquoted-join test passes either way.
  • Manual, rtk rewrite on Windows (Git Bash for the inputs):
Input Result
git commit -m 'one \⏎two' && ls rtk git commit -m 'one \⏎two' && rtk ls
ls # note \⏎ls -la no rewrite
git log \⏎ --oneline -n 3 rtk git log --oneline -n 3
git commit -m "a \⏎ b" && git status rtk git commit -m "a b" && rtk git status
echo $'x \⏎y' && git status echo $'x \⏎y' && rtk git status

I couldn't run the issue's bash script itself (it needs stub binaries on a POSIX PATH), so the bash side of the table is from bash's quoting rules, not from a run.

🤖 Generated with Claude Code

`collapse_line_continuations` replaced every backslash-newline with a space,
including inside single quotes, `$'...'` and comments, where bash keeps it.
A single-quoted commit message lost its line break, and a comment ending in
a backslash swallowed the next line, which bash runs as its own command.

- The collapse is now a scanner with quote and comment state: it joins only
  outside single quotes, `$'...'` and comments; inside double quotes it drops
  the backslash and newline and keeps the string's own whitespace, as bash does.
- A comment that ends in a backslash-newline passes the command through. The
  lexer has no comment state and would join the next line into the comment's
  segment, so the hook would otherwise allow a rewrite whose second line no
  deny or ask rule was checked against.
- The wrapper permission check keeps joining every continuation
  (`join_all_line_continuations`): the script of `bash -c 'git \<NL>push'` is
  parsed again by the inner shell, where that is a continuation.

Fixes rtk-ai#5090

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🔴 Risk high

Summary

Rewrites the shell line-continuation collapser as a quote/comment-aware scanner instead of a blind regex, so backslash-newlines inside single quotes, $'...' strings, and comments are preserved rather than collapsed, matching bash's actual behavior. It also detects comments that end in a backslash-newline and defers to the agent's own permission handling instead of letting the rewrite silently misinterpret the following line, while keeping the separate wrapper-check join function unchanged for i…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #5090


Analyzed automatically by wshm · This is an automated analysis, not a human review.

The wrapper check joins every continuation; the rewrite joins only those bash joins (rtk-ai#5090).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant