Skip to content

Strip CR from source code read from CRLF files - #1286

Open
yppp wants to merge 1 commit into
ruby:masterfrom
yppp:strip-cr-from-show-source
Open

yppp wants to merge 1 commit into
ruby:masterfrom
yppp:strip-cr-from-show-source

Conversation

@yppp

@yppp yppp commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #1037.

When a method is defined in a file with CRLF line endings, show_source displayed a ^M at the end of every line:

irb(main):001> show_source "desc"

From: /mnt/r/crlf.rb:1

def desc^M
  "this file has CRLF newline"^M
end^M

The parser treats CR as whitespace, but the source was read from the file verbatim and colorized as-is.

The same happened in the code listing shown when a session starts on binding.irb (and in whereami), which comes from WorkSpace#code_around_binding.

This change normalizes CRLF to LF in the two places that read source code for display:

  • IRB::SourceFinder::Source#file_content (used by show_source)
  • WorkSpace#code_around_binding, before colorizing

Line numbers are unaffected, since CRLF and LF split into the same lines.

Tests:

  • TestIRB::SourceFinderTest#test_colorized_content_strips_cr_from_crlf_file (new file test/irb/test_source_finder.rb)
  • TestIRB::WorkSpaceTest#test_code_around_binding_with_crlf_file

Fixes ruby#1037.

When a method is defined in a file with CRLF line endings, show_source
displayed a ^M at the end of every line, because SourceFinder read the
file content verbatim.  The code listing shown when a session starts
(and by whereami) had the same problem in WorkSpace#code_around_binding.

Normalize CRLF to LF when reading the file in SourceFinder::Source and
before colorizing in WorkSpace#code_around_binding.  Line numbers are
unaffected.

This branch has not been deployed

No deployments
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.

strip end of line CR from show_source

1 participant