Skip to content

TypeError on require 'digest/sha2' #2511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ojab opened this issue Oct 20, 2021 · 13 comments
Closed

TypeError on require 'digest/sha2' #2511

ojab opened this issue Oct 20, 2021 · 13 comments
Assignees
Milestone

Comments

@ojab
Copy link

ojab commented Oct 20, 2021

$ ruby -v
truffleruby 21.3.0, like ruby 2.7.4, GraalVM CE Native [x86_64-linux]
$ bundle exec irb
irb(main):001:0> require 'digest/sha2'

From: /home/ojab/.rbenv/versions/truffleruby+graalvm-21.3.0/graalvm/languages/ruby/lib/truffle/truffle/cext.rb @ line 979 :

    974:       const = Object.const_get(path, false)
    975:     rescue NameError
    976:       raise ArgumentError, "undefined class/module #{path}"
    977:     end
    978:     unless const.is_a?(Class)
 => 979:       binding.irb
    980:       raise TypeError
    981:     end
    982:     const
    983:   end
    984: 

irb(Truffle::CExt):001:0> path
=> "Digest"
irb(Truffle::CExt):002:0> const.is_a?(Class)
=> false
irb(Truffle::CExt):003:0> const.is_a?(Module)
=> true
irb(Truffle::CExt):004:0> 
Traceback (most recent call last):
/home/ojab/.rbenv/versions/truffleruby+graalvm-21.3.0/graalvm/languages/ruby/lib/truffle/truffle/cext.rb:980:in `rb_path_to_class': TypeError (TypeError)
@ojab ojab changed the title TypeError on `require 'digest/sha2' TypeError on require 'digest/sha2' Oct 20, 2021
@eregon
Copy link
Member

eregon commented Oct 20, 2021

I cannot reproduce, could you share your Gemfile or parts of it?

$ ruby -v
truffleruby 21.3.0, like ruby 2.7.4, GraalVM CE Native [x86_64-linux]
$ irb
irb(main):001:0> require 'digest/sha2'
=> true

@eregon
Copy link
Member

eregon commented Oct 20, 2021

The full backtrace you get without the binding.irb would also be helpful.

@ojab
Copy link
Author

ojab commented Oct 20, 2021

hmm, right, it happens only inside bundle. Let me debug it some more and I will get back with reproducer.

@ojab
Copy link
Author

ojab commented Oct 20, 2021

ah, quite easy, Gemfile.lock has digest-3.0.0, so

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'digest', '3.0.0'
end

require 'digest/sha2'

@ojab
Copy link
Author

ojab commented Oct 20, 2021

I guess could be #1390 duplicate?

@eregon
Copy link
Member

eregon commented Nov 12, 2021

Sorry, I missed your reply somehow.

That repro works correctly now (no output and no error), because TruffleRuby master already ships with digest 3.0.0 as a default gem.

gem i digest fails though:

$ gem i digest
Fetching digest-3.0.0.gem
Building native extensions. This could take a while...
ERROR:  Error installing digest:
	ERROR: Failed to build gem native extension.

    current directory: /home/eregon/.rubies/truffleruby-dev/lib/gems/gems/digest-3.0.0/ext/digest
/home/eregon/.rubies/truffleruby-dev/bin/truffleruby -I /home/eregon/.rubies/truffleruby-dev/lib/mri -r ./siteconf20211112-17074-qqi4qj.rb extconf.rb
creating Makefile

current directory: /home/eregon/.rubies/truffleruby-dev/lib/gems/gems/digest-3.0.0/ext/digest
make DESTDIR\= clean

current directory: /home/eregon/.rubies/truffleruby-dev/lib/gems/gems/digest-3.0.0/ext/digest
make DESTDIR\=
compiling digest.c
linking shared-object digest.so

current directory: /home/eregon/.rubies/truffleruby-dev/lib/gems/gems/digest-3.0.0/ext/digest
make DESTDIR\= install
/usr/bin/install -c -m 0755 digest.so ./.gem.20211112-17074-pymstw
/usr/bin/install: missing destination file operand after './digest.h'
Try '/usr/bin/install --help' for more information.
make: *** [Makefile:177: /digest.h] Error 1
make: *** Waiting for unfinished jobs....
installing digest libraries

make install failed, exit code 2

Gem files will remain installed in /home/eregon/.rubies/truffleruby-dev/lib/gems/gems/digest-3.0.0 for inspection.
Results logged to /home/eregon/.rubies/truffleruby-dev/lib/gems/extensions/x86_64-linux/3.0.2.4/digest-3.0.0/gem_make.out

In general it's probably better to avoid an explicit dependency on the digest gem, but I guess maybe it's through a transitive dependency or so?

We'll likely need fixes in https://github.com/ruby/digest to make this work (either always use the builtin version, or make sure the C extension works and add truffleruby in CI there).

@eregon eregon self-assigned this Nov 12, 2021
@eregon eregon added this to the 22.0.0 milestone Nov 12, 2021
@eregon
Copy link
Member

eregon commented Nov 12, 2021

After setting sitehdrdir in RbConfig and fixing the check for rb_path2class() it works fine:

$ gem i digest
...
$ ruby -v -rdigest/sha2 -e 'p Digest::SHA2.hexdigest "abc"'
truffleruby 22.0.0-dev-ffc70bd5, like ruby 3.0.2, Interpreted JVM [x86_64-linux]
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"

@ojab
Copy link
Author

ojab commented Nov 12, 2021

Yep, it's transitive via updated net-smtp that we need for ruby/net-smtp#26.

So I guess it's a minor incompatibility issue with particular cext at the end of the day?
Bailing out in truffleruby-dev looks fine, but building ext and bailing out on require in 21.3 looks like a truffleruby bug for me.

If it would work — even better, anyway.

@eregon
Copy link
Member

eregon commented Nov 12, 2021

looks like a truffleruby bug for me.

Yeah, that's a fair assessment, it was a bug of rb_path2class() to only accept classes and not modules.

Regarding the issue label/changelog, compatibility is typically used for "didn't work yet" and bug for incorrect behavior which can cause incorrect result.

@eregon
Copy link
Member

eregon commented Nov 12, 2021

To clarify, it'll be fixed soon and it will be in the next release.

graalvmbot pushed a commit that referenced this issue Nov 12, 2021
* Needed by the digest gem:
  #2511 (comment)
graalvmbot pushed a commit that referenced this issue Nov 12, 2021
PullRequest: truffleruby/3050
@postmodern
Copy link

Also ran into this issue after I had to add net-smtp to the Gemfile, since Ruby 3.1.0 made net-smtp a "bundled gem". I tried restricting the bundled net-* gems using platforms: :ruby so those gems would not pull in C extensions under JRuby.

Should I wait for the next TruffleRuby release or is there a workaround to ignore the digest gem under TruffleRuby?
Thanks.

@eregon
Copy link
Member

eregon commented Jan 10, 2022

truffleruby-dev has this rb_path2class issue already fixed, and the next release 22.0 on January 18 (in a week) will have it as well. So probably simplest to use one of those.

My understanding is those gems should be under if RUBY_VERSION >= "3.1", but if they noop on previous versions probably they work fine on TruffleRuby as well.

FWIW, there is also platforms: :mri if you want to include a gem only on CRuby.

@eregon
Copy link
Member

eregon commented Jan 24, 2022

The digest gem shouldn't cause any problems anymore on TruffleRuby with the fix for #2577: c1655b3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants