Skip to content

Keep backslashes in the content of insert_into_file - #950

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
Asimmirzac:insert-into-file-backslashes
Dec 4, 2026
Merged

rafaelfranca merged 1 commit into
rails:mainfrom
Asimmirzac:insert-into-file-backslashes

Conversation

@Asimmirzac

Copy link
Copy Markdown
Contributor

🌈

insert_into_file passes the content to gsub! as a replacement string, so backslash sequences in the content are read as backreferences:

insert_into_file "f", %q(  x.gsub(/(a)/, "\\\\1")) + "\n", after: "top\n"
# expected:   x.gsub(/(a)/, "\\1")
# inserted:   x.gsub(/(a)/, "\1")

\0 inserts the matched flag, \1 disappears when the flag has no group, and \\ becomes \. Because the written text then differs from the given content, running the action again inserts it a second time, and revoking it removes nothing.

The content is now escaped before the substitution. prepend_to_file, append_to_file, inject_into_class and inject_into_module go through the same code.

One behavior change to be aware of: content that used \1 on purpose to refer to a group in the :before/:after regexp is now inserted literally. This is not documented or covered by the specs, and such content already breaks the "already present" check and revoking.

The full spec suite passes; the two new specs fail without the change.

Prepared with AI assistance.

🤖 Generated with Claude Code

The content was used as a gsub replacement string, so sequences such as
\\, \0 and \1 in it were read as backreferences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaelfranca
rafaelfranca merged commit 7028654 into rails:main Dec 4, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants