IRB initialization changes global `$stdout` encoding, causing `Encoding::UndefinedConversionError`

Offen
#1,162 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
45/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
ruby
Bereich
cli

Rechercherichtung

Beginnen Sie in lib/irb/init.rb bei set_encoding und verfolgen Sie dann dessen Aufrufer durch IRB::Context und IRB::RelineInputMethod. Stellen Sie sicher, dass IRB sein erforderliches Encoding-Verhalten beibehält, ohne die globalen Standard-Streams zu ändern, und überprüfen Sie, dass sowohl IRB.setup/IRB::Irb.new als auch binding.irb das Ausgabe-Beispiel mit ASCII-8BIT nicht mehr fehlschlagen lassen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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
  1. User has ~/.rdbgrc with config set irb_console true
  2. User runs rails notes (or any Rails command)
  3. debug gem loads and initializes IRB due to the setting
  4. IRB changes $stdout.external_encoding to UTF-8
  5. rails notes reads files with Encoding::BINARY and tries to output
  6. Encoding::UndefinedConversionError is 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)
Vorherrschende Sprache
Ruby
Sterne
477
Forks
165
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ruby/irb

Alle Issues in ruby/irb

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.