[ruby-core:98205] [Ruby master Feature#16832] Use #name rather than #inspect to build "uninitialized constant" error messages
From:
eregontp@...
Date:
2020-05-07 19:54:38 UTC
List:
ruby-core #98205
Issue #16832 has been updated by Eregon (Benoit Daloze).
Right, `name_err_mesg_to_str` does this.
And TruffleRuby actually replicated that logic but I didn't think about it for this issue:
https://github.com/oracle/truffleruby/blob/e8270af20bc4fae74be7f78d3d474e271768b943/src/main/ruby/truffleruby/core/truffle/exception_operations.rb#L18
In the case we exceed the limit, I think we could call `rb_class_name()` for Module/Class, instead of `rb_any_to_s()`.
Such an arbitrary limit seems weird, maybe we should extend it to at least 100?
Or just not limit at all and if people write a too large `inspect` then they'll just see the issue (my preference).
I guess maybe it's done that way so it avoids the printed error message to be longer than 80 chars in the terminal?
A more intuitive way to achieve that could be to print something like `"#{obj.inspect[0, 65]} ..."`
----------------------------------------
Feature #16832: Use #name rather than #inspect to build "uninitialized constant" error messages
https://bugs.ruby-lang.org/issues/16832#change-85442
* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
While debugging a bug in Rails (https://github.com/rails/rails/pull/37632#issuecomment-623387954) I noticed `NameError` calls `inspect` on the `const_get` receiver to build its error message.
The problem is that some libraries such as Active Record have been redefining `inspect` for years to provide human readable information, e.g.:
```
>> Shipit::Stack.inspect
=> "Shipit::Stack (call 'Shipit::Stack.connection' to establish a connection)"
>> Shipit::Stack.connection; nil
>> Shipit::Stack.inspect
=> "Shipit::Stack(id: integer, environment: string, ...)"
```
Which makes for fairly unhelpful error messages:
```
>> Shipit::Stack.const_get(:Foo)
Traceback (most recent call last):
2: from (irb):4
1: from (irb):4:in `const_get'
NameError (uninitialized constant #<Class:0x00007fc8cadf2dd0>::Foo)
```
So perhaps it's Active Record that is at fault here, but from my understanding since the goal is to display the constant path that was requested, `name` is much more likely to return a relevant constant name.
Proposed patch: https://github.com/ruby/ruby/pull/3080
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>