rbs icon indicating copy to clipboard operation
rbs copied to clipboard

[prototype runtime] fails due to `cannot load such file -- sorted_set` with Ruby 3.0

Open ybiquitous opened this issue 4 years ago • 2 comments

Hello, I find a bug that the rbs prototype runtime Object command fails on Ruby 3.0 (it succeeds on Ruby 2.7). Please see the reproduction below for details.

Reproduction

Note: I'm using Docker to reduce any environment-specific problems.

Ruby 3.0

rbs prototype runtime Object fails with LoadError:

$ docker run --rm -it rubylang/ruby:3.0.0-focal bash
# gem install rbs:1.0.4
Fetching rbs-1.0.4.gem
Successfully installed rbs-1.0.4
1 gem installed

# rbs prototype runtime Object
/usr/local/lib/ruby/3.0.0/set/sorted_set.rb:4:in `rescue in <top (required)>': The `SortedSet` class has been extracted from the `set` library.You must use the `sorted_set` gem or other alternatives. (RuntimeError)
	from /usr/local/lib/ruby/3.0.0/set/sorted_set.rb:1:in `<top (required)>'
	from <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:305:in `const_get'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:305:in `block in generate_constants'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:304:in `each'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:304:in `generate_constants'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:385:in `generate_class'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:45:in `block in decls'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:42:in `each'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:42:in `decls'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/cli.rb:597:in `run_prototype'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/cli.rb:113:in `run'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/exe/rbs:7:in `<top (required)>'
	from /usr/local/bin/rbs:23:in `load'
	from /usr/local/bin/rbs:23:in `<main>'
<internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require': cannot load such file -- sorted_set (LoadError)
	from <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from /usr/local/lib/ruby/3.0.0/set/sorted_set.rb:2:in `<top (required)>'
	from <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:305:in `const_get'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:305:in `block in generate_constants'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:304:in `each'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:304:in `generate_constants'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:385:in `generate_class'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:45:in `block in decls'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:42:in `each'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/prototype/runtime.rb:42:in `decls'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/cli.rb:597:in `run_prototype'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/lib/rbs/cli.rb:113:in `run'
	from /usr/local/lib/ruby/gems/3.0.0/gems/rbs-1.0.4/exe/rbs:7:in `<top (required)>'
	from /usr/local/bin/rbs:23:in `load'
	from /usr/local/bin/rbs:23:in `<main>'

Ruby 2.7

rbs prototype runtime Object has no problems:

$ docker run --rm -it rubylang/ruby:2.7.2-bionic bash
# gem install rbs:1.0.4
Fetching rbs-1.0.4.gem
Successfully installed rbs-1.0.4
1 gem installed

# rbs prototype runtime Object
class Object < BasicObject
  include PP::ObjectMixin

  include JSON::Ext::Generator::GeneratorMethods::Object

  include Kernel
end

Object::ARGF: ARGF.class

Object::ARGV: Array[untyped]

Object::CROSS_COMPILING: untyped?

Object::ENV: Object

Object::FALSE: bool

Object::NIL: untyped?

Object::RUBY_COPYRIGHT: String

Object::RUBY_DESCRIPTION: String

Object::RUBY_ENGINE: String

Object::RUBY_ENGINE_VERSION: String

Object::RUBY_PATCHLEVEL: Integer

Object::RUBY_PLATFORM: String

Object::RUBY_RELEASE_DATE: String

Object::RUBY_REVISION: String

Object::RUBY_VERSION: String

Object::STDERR: IO

Object::STDIN: IO

Object::STDOUT: IO

Object::TOPLEVEL_BINDING: Binding

Object::TRUE: bool

Environment

  • Ruby: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
  • RBS: rbs 1.0.4

ybiquitous avatar Feb 09 '21 01:02 ybiquitous

SortedSet has been removed from set library since Ruby 3.0.0. But the SortedSet constant is still available as an autoload. https://github.com/ruby/ruby/blob/v3_0_0/lib/set.rb#L692

rbs prototype runtime accesses all constants, so it causes an error.

I think we can suppress the error on const_get. https://github.com/ruby/rbs/blob/f965869c4b4224a796ebc9f34cfc4b07bde82cfc/lib/rbs/prototype/runtime.rb#L305

I got similar errors when I used prototype runtime for Rails gems, because they use autoload. If the error is suppressed, the errors for Rails gems will be also resolved.

pocke avatar Feb 11 '21 08:02 pocke

@pocke Thank you for sharing it! I understand the cause.

It seems a bit weird to me that Object.constants includes SortedSet despite the sorted_set gem is not loaded... 🤔 But I cannot get any good idea to resolve this error. 😓

ybiquitous avatar Feb 11 '21 12:02 ybiquitous