Skip to content
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

Set RUBY_ENGINE to truffleruby and fix related libs #46

Merged
merged 8 commits into from
Feb 20, 2017
Merged

Conversation

eregon
Copy link
Member

@eregon eregon commented Feb 1, 2017

No description provided.

@@ -51,8 +51,7 @@
public static final String RUBY_VERSION = "2.3.1";
public static final int RUBY_REVISION = 0;
public static final String COMPILE_DATE = "2017";
// TODO (pitr-ch 11-Jan-2017): use our value when we are recognized by rubygems
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific workarounds are better than waiting for a new RubyGems version (but we should fix upstream as well obviously).
We can also patch our own Rubygems of course.

@bjfish
Copy link
Contributor

bjfish commented Feb 1, 2017

There may be some failures due to updating this. We'll probably need to shim/update in some places. This was one usage that came to mind:
https://github.com/bundler/bundler/blob/b3cb9516aaf83cf865c9ed76c52ec4d0d182015f/spec/support/platforms.rb#L66

@eregon
Copy link
Member Author

eregon commented Feb 1, 2017

@bjfish This is in Bundler specs, right?
It seems fine to define BUNDLER_SPEC_RUBY_ENGINE_VERSION=1.0 or so for running Bundler specs (and fix upstream with Truffle::VERSION).

@bjfish
Copy link
Contributor

bjfish commented Feb 1, 2017

I think there's similar usage in the lib somewhere. Maybe here: https://github.com/bundler/bundler/blob/121ada2a1842ebe9989f35567e2dd829ec06b7a1/lib/bundler/ruby_version.rb#L108

@pitr-ch
Copy link
Contributor

pitr-ch commented Feb 1, 2017

@bjfish please have a look at my commit introducing the change form jruby, it'll help to track down what has to be fixed

@eregon
Copy link
Member Author

eregon commented Feb 2, 2017

@pitr-ch Which commit it that?

@pitr-ch
Copy link
Contributor

pitr-ch commented Feb 2, 2017

@eregon git blame of line #46 (diff)

@eregon
Copy link
Member Author

eregon commented Feb 2, 2017

83c0f24 is the commit

@eregon
Copy link
Member Author

eregon commented Feb 7, 2017

@pitr-ch @bjfish I fixed a few failures but the remaining ones seem related to RubyGems/Bundler.
I did not find an easy solution.
Could you take a look and continue this PR as you are more knowledgeable in this area?
The last commit (4a715d7) was just an experiment, feel free to get rid of it.

@pitr-ch
Copy link
Contributor

pitr-ch commented Feb 7, 2017

It looks like bundler is not even resolving the tested gem as a dependency, therefore it is not put in $LOAD_PATH. This is why I went for the "ruby" originally to avoid untangling bundler complexities ;) until it's fixed properly without hacks. I rather not spend time on this. I think we all agree that RUBY_ENGINE should be 'truffleruby' but lets wait until bundler recognises truffleruby.

@eregon
Copy link
Member Author

eregon commented Feb 7, 2017

until bundler recognises truffleruby

What does it mean? We will need to find out how to make it work anyway and make a PR to Bundler. Maybe we can ask some help from the Bundler team?

@pitr-ch
Copy link
Contributor

pitr-ch commented Feb 8, 2017

@eregon exactly, I'll update bundler and submit a PR. I would like to avoid hacking it in the meantime, especially if RUBY_ENGINE='ruby' works fine.

@pitr-ch
Copy link
Contributor

pitr-ch commented Feb 18, 2017

fix #46

@pitr-ch pitr-ch changed the title Set RUBY_ENGINE to truffleruby Set RUBY_ENGINE to truffleruby and fix related libs Feb 18, 2017
@pitr-ch pitr-ch force-pushed the fix_ruby_engine branch 3 times, most recently from 0f7c6d1 to ead57a4 Compare February 18, 2017 22:42
@@ -38,6 +38,8 @@
end
end

require 'truffle/patching'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it too late? What if we want to patch something in rubygems?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the rubygem files is loaded lazily, so it works. If we need to patch any of the eagerly loaded files it can be done manually here. I rather expect we'll be removing the patches rather than adding new ones, it should not be a problem.

@@ -789,6 +789,7 @@ private void initializeConstants() {
Layouts.MODULE.getFields(rubiniusFFIModule).setConstant(context, node, "TYPE_VARARGS", RubiniusTypes.TYPE_VARARGS);

Layouts.MODULE.getFields(objectClass).setConstant(context, node, "RUBY_VERSION", frozenUSASCIIString(RubyLanguage.RUBY_VERSION));
Layouts.MODULE.getFields(objectClass).setConstant(context, node, "TRUFFLERUBY_VERSION", frozenUSASCIIString(RubyLanguage.getTruffleRubyVersion()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be TRUFFLERUBY_VERSION (like JRUBY_VERSION)
or Truffle::VERSION (like Rubinius::VERSION) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are fine to me, the advantage of TRUFFLERUBY_VERSION is that's easier to check, even if defined? is buggy (as we used to have) and it's not the "Truffle version" but the "TruffleRuby version".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is already RUBY_VERSION, JRUBY_VERSION I would follow this pattern.

@@ -687,7 +686,9 @@ def subcommand_run(rest)
{}).
merge(@options[:run][:offline] ?
{ 'GEM_HOME' => @options[:run][:offline_gem_path].to_s,
'GEM_PATH' => @options[:run][:offline_gem_path].to_s } :
'GEM_PATH' => @options[:run][:offline_gem_path].to_s,
'PATH' => [p(@options[:run][:offline_gem_path].join('bin').to_s), ENV['PATH']].join(':')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug p in here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks

@@ -0,0 +1,38 @@
module Truffle::Patching
TRUFFLE_PATCHES_DIRECTORY = File.join(Truffle::Boot.ruby_home, 'lib', 'patches')
TRUFFLE_PATCHES = Dir.glob(File.join(TRUFFLE_PATCHES_DIRECTORY, '**', '*.rb')).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid File.join, it just adds clutter, and in the case of Dir.glob is even potentially incorrect (if File::SEPARATOR is changed). Filesystem pathnames in Ruby are always represented with /.

TRUFFLE_PATCHES = Dir.glob(File.join(TRUFFLE_PATCHES_DIRECTORY, '**', '*.rb')).
select { |path| File.file? path }.
map { |path| path[(TRUFFLE_PATCHES_DIRECTORY.size+1)..-4] }.
reduce ({}) { |h, v| h.update v => true }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the pure functional approach here more verbose and also much less efficient.
I propose:

TRUFFLE_PATCHES = {}
Dir.glob("#{TRUFFLE_PATCHES_DIRECTORY}/**/*.rb") do |path|
  if File.file? path
    feature = path[(TRUFFLE_PATCHES_DIRECTORY.size+1)...-3]
    TRUFFLE_PATCHES[feature] = true
  end
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's evaluated only once over about a dozen of files, I don't think performance is relevant in this particular case. As for functional vs. imperative style I think this is matter of taste, I find functional clearer, more readable. Since I think it's only a matter of personal taste in this case, I'll leave it as is.

Copy link
Member Author

@eregon eregon Feb 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It matters because this runs during startup, isn't it?
It creates 3 extra Arrays, plus 2*n (number of patch files) Hashes, not exactly optimal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array#to_h might be a good compromise here, with [path[...], true] in the map block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does run on startup, but 3 arrays of about ten elements, that's insignificant compared to everything else. The code will also be eventually removed anyway. It's a single hash though, update does not create new instances or did you mean something else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, Hash#update is Hash#merge! (don't use update, it's an uncommon alias)
v => true creates a Hash per iteration.
Measure it if you want.
But anyway I think reduce/inject + modifying inplace the accumulator is an anti-pattern that just adds complexity with extra variables and caring of the return value, for no gain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_h is best option

else
# not defined in ruby 1.8.7
"ruby"
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very weirdly indented, I would rather keep the same indentation as the source to ease comparison.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(on a side note, I personally find no value to aligning assignments)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, auto-formatted by mistake.

(I got used to them only because I prefer aligned hash's keys and values and there is only one option for both in idea and I won't go against auto-formatter.)

Rubinius::VERSION.dup
when "jruby"
JRUBY_VERSION.dup
when 'truffleruby' # added case branch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to use a common prefix to mean "comment added for TruffleRuby support".
We used to have # Truffle: my comment which I think is easy to spot.
I think we should be consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'll use # TruffleRuby:, and update the comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are quite a few # Truffle:, I think it's best to update them all in a different PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry already pushed here.

# add record for truffleruby
Bundler::Dependency.send :const_set,
:PLATFORM_MAP,
old_value.merge(truffleruby: Gem::Platform::RUBY)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer just:

Bundler::Dependency::PLATFORM_MAP[:truffleruby] = Gem::Platform::RUBY

It is much easier to see what's happening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash is frozen, therefore it has to be replaced.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate, maybe something like this would simplify a little:

class Bundler::Depedency
  const_set PLATFORM_MAP, PLATFORM_MAP.merge(truffleruby: Gem::Platform::RUBY)
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe, it's just a patch though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well make it readable.

end

reverse_platform_map.each { |_, platforms| platforms.freeze }
end.freeze)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please use a clearer imperative approach with less nesting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of original code, keeping it.

@@ -0,0 +1,38 @@
module Truffle::Patching
Copy link
Member Author

@eregon eregon Feb 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is quite different than what I had in mind in #68 (comment). Your approach could also use that mechanism, but it is simpler and sufficient for now, so let's keep it simple 👍
Could you add a lib/patches/README.md to document briefly what it is for and what it does?

@pitr-ch pitr-ch force-pushed the fix_ruby_engine branch 4 times, most recently from 1c74877 to bbbfb5b Compare February 19, 2017 19:34
@pitr-ch pitr-ch mentioned this pull request Feb 20, 2017
7 tasks
eregon and others added 4 commits February 20, 2017 17:02
* introduce patching system, loads a file from patches directory after a file
  with same is required
* replace bundler-workarounds with patches
* make bundler recognize truffleruby
* remove few unnecessary fixes
* C extensions are not built by default, TRUFFLERUBY_CEXT_ENABLED can be
  set to enable
@pitr-ch pitr-ch merged commit de7772e into master Feb 20, 2017
@eregon eregon deleted the fix_ruby_engine branch February 20, 2017 18:28
chrisseaton added a commit that referenced this pull request May 30, 2018
* commit '0f3ffd2ecabba9de24ba8aaae5351755fcb42800':
  Change the imagename in the GraalVM property file.
chrisseaton pushed a commit to Shopify/truffleruby that referenced this pull request Nov 13, 2019
Typo

Merge-Requested-By: XrXr
Merge-Queue-Emergency-Bypass: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants