IRB initialization changes global `$stdout` encoding, causing `Encoding::UndefinedConversionError`
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 lib/irb/init.rb 中的 set_encoding 开始,然后通过 IRB::Context 和 IRB::RelineInputMethod 追踪其调用方。确保 IRB 在不改变全局标准流的情况下保留所需的 encoding 行为,并验证 IRB.setup/IRB::Irb.new 和 binding.irb 都不再导致 ASCII-8BIT 输出示例失败。
由索引模型根据 Issue 内容生成。
描述
Related Issue
This issue is related to #1048 (IRB leaking out Reline changes globally). In addition to Reline settings, IRB also leaks $stdout, $stdin, and $stderr encoding changes globally.
Description
When IRB is initialized, it calls $stdout.set_encoding('UTF-8') (and similarly for $stdin and $stderr). This affects all code that runs after IRB initialization, not just IRB itself.
This causes Encoding::UndefinedConversionError when code tries to output ASCII-8BIT strings to $stdout.
Minimal Reproducible Example
Example 1: Using IRB.setup and IRB::Irb.new
require 'irb'
puts "Before: $stdout.external_encoding = #{$stdout.external_encoding.inspect}"
# IRB initialization
IRB.setup(nil)
IRB::Irb.new
puts "After: $stdout.external_encoding = #{$stdout.external_encoding.inspect}"
# This raises Encoding::UndefinedConversionError
ascii_str = "日本語".force_encoding('ASCII-8BIT')
puts ascii_str
Output
Before: $stdout.external_encoding = nil
After: $stdout.external_encoding = #<Encoding:UTF-8>
-e:12:in 'IO#write': "\xE3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
Example 2: Using binding.irb
This is a more common scenario that affects everyday debugging:
puts "Before: $stdout.external_encoding = #{$stdout.external_encoding.inspect}"
binding.irb # Enter IRB session, then type 'exit' to continue
puts "After: $stdout.external_encoding = #{$stdout.external_encoding.inspect}"
ascii_str = "日本語".force_encoding('ASCII-8BIT')
puts ascii_str
Output
Before: $stdout.external_encoding = nil
irb(main):001> exit
After: $stdout.external_encoding = #<Encoding:UTF-8>
-e:8:in 'IO#write': "\xE6" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
This means any code that runs after binding.irb is affected. Users debugging their applications with binding.irb may encounter unexpected encoding errors in code that worked fine before the IRB session.
Real-World Impact
This issue affects users of the debug gem with irb_console true setting.
Scenario
- User has
~/.rdbgrcwithconfig set irb_console true - User runs
rails notes(or any Rails command) debuggem loads and initializes IRB due to the setting- IRB changes
$stdout.external_encodingto UTF-8 rails notesreads files withEncoding::BINARYand tries to outputEncoding::UndefinedConversionErroris raised for non-ASCII content
Stack Trace
IRB::Irb#initialize (irb.rb:91)
↓
IRB::Context#initialize (context.rb:95)
↓
IRB::RelineInputMethod#initialize (input-method.rb:261)
↓
IRB.set_encoding (init.rb:528)
↓
$stdout.set_encoding (init.rb:529) ← Global change!
Root Cause
In lib/irb/init.rb, the set_encoding method changes global I/O streams:
def set_encoding(extern, intern = nil, override: true)
Encoding.default_external = extern
Encoding.default_internal = intern
[$stdin, $stdout, $stderr].each do |io|
io.set_encoding(extern, intern) # ← This affects all code, not just IRB
end
# ...
end
Proposed Solution
Instead of modifying global $stdin, $stdout, $stderr, IRB could use duplicated I/O streams for its own operations:
def set_encoding(extern, intern = nil, override: true)
Encoding.default_external = extern
Encoding.default_internal = intern
# Use duplicated streams for IRB, don't modify global ones
@irb_stdin = $stdin.dup.tap { |io| io.set_encoding(extern, intern) }
@irb_stdout = $stdout.dup.tap { |io| io.set_encoding(extern, intern) }
@irb_stderr = $stderr.dup.tap { |io| io.set_encoding(extern, intern) }
# ...
end
This way, IRB maintains its encoding consistency without affecting other code.
Environment
- Ruby: 3.3.6 / 3.4.7
- IRB: 1.16.0
- OS: macOS (LANG=ja_JP.UTF-8)
- 主要语言
- Ruby
- 星标
- 477
- 派生
- 165
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/irb 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 45/100
维护者通常 2 天内回复
-
Proposal: public hook for external documentation providers in show_doc and the completion doc dialog可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 5/5 一周以上 新手友好度 38/100
维护者通常 2 天内回复
-
难度 5/5 一周以上 新手友好度 35/100
维护者通常 2 天内回复
相似的 Issue
-
Pro request form loses all text when the subject is too long可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug f:request-creation professional
难度 2/5 1-3 小时 新手友好度 64/100
mysociety/alaveteli#9733 · 1 条评论 ·
维护者通常 1 天内回复
-
needs verification
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 13 天内回复
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
难度 2/5 1-3 小时 新手友好度 76/100
betagouv/conseillers-entreprises#4808 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 78/100