pry icon indicating copy to clipboard operation
pry copied to clipboard

Coderay exception in a hook caused by a probably faulty default assignment

Open joallard opened this issue 5 years ago • 4 comments

I stumbled upon this weird bug where an exception was raised in a hook by Coderay.

before_session hook failed: ArgumentError: Symbol or String expected, but NilClass given.
/Users/jon/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/coderay-1.1.3/lib/coderay/helpers/plugin_host.rb:215:in `validate_id'

Fearing I had messed up something in Pry Rescue, I went digging.

Looks like when @code_type is nil, self.highlight received nil instead of the default :ruby, and passing nil to Coderay downwards. A kind of annoying pattern in Ruby.

I don't exactly know why this was triggered (I was doing some stack exploration manually, ie. bindings[10].pry) so I'm unable to make a test case without some further exploration, but seems like the quick fix would be to set language ||= :ruby instead of the argument line.

From: /Users/jon/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/code.rb:272 Pry::Code#print_to_output:

    269: def print_to_output(output, color = false)
    270:   @lines.each do |loc|
    271:     loc = loc.dup
 => 272:     loc.colorize(@code_type)              if color
    273:     loc.add_line_number(max_lineno_width, color) if @with_line_numbers
    274:     loc.add_marker(@marker_lineno)        if @with_marker
    275:     loc.indent(@indentation_num)          if @with_indentation
    276:     output << loc.line
    277:     output << "\n"
    278:   end
    279:   output
    280: end

From: /Users/jon/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/code/loc.rb:57 Pry::Code::LOC#colorize:

    56: def colorize(code_type)
 => 57:   tuple[0] = SyntaxHighlighter.highlight(line, code_type)
    58: end

From: /Users/jon/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/syntax_highlighter.rb:10 Pry::SyntaxHighlighter.highlight:

     9: def self.highlight(code, language = :ruby)
 => 10:   tokenize(code, language).term
    11: end

https://github.com/pry/pry/blob/9d9ae4a0b0bd487bb41170c834b3fa417e161f23/lib/pry/syntax_highlighter.rb#L9

joallard avatar Jun 06 '20 22:06 joallard

This might be related to #1511? (I'm on MRI though)

joallard avatar Jun 06 '20 22:06 joallard

Same issue:

before_session hook failed: ArgumentError: Symbol or String expected, but NilClass given.
/Users/XXX/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/coderay-1.1.3/lib/coderay/helpers/plugin_host.rb:215:in `validate_id'
(see pry_instance.hooks.errors to debug)

Basically my issue is that I am trying to figure out where "I am" (with cd-cause or an "@" as it usally works with pry). I suspect it has more to do with pry than with pry-rescue (maybe?)

Context:

  1. Happened while using pry-rescue (https://github.com/ConradIrwin/pry-rescue) over rspec
  2. Specifically cd-cause
  3. Then on the "subject" (which triggered the RecordInvalidException) calling subject.errors
  4. Using Ruby 2.7.3
  5. pry (0.14.1) [latest]
  6. pry-rescue (1.5.2) [latest]
  7. coderay (1.1.3) [latest]

rgp avatar Sep 16 '21 16:09 rgp

Followup:

It got partially fixed by upgrading Ruby to 2.7.4 but it still shows up mainly maybe on a "second-layer" exception.

At least it allowed me to continue with what I was doing so I hope this helps someone else if they ever encounter a similar/same issue.

rgp avatar Sep 16 '21 16:09 rgp

Changing the line from above

loc.colorize(@code_type)              if color

to

loc.colorize(@code_type || :ruby)              if color

has worked for me as a workaround.

kisp avatar Nov 29 '21 13:11 kisp