Skip to content

Commit

Permalink
Make load path cache work on truffleruby
Browse files Browse the repository at this point in the history
  • Loading branch information
rwstauner committed Jul 20, 2023
1 parent 640c126 commit 64a4fda
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

* Support YAML and JSON CompileCache on TruffleRuby.
* Support LoadPathCache on TruffleRuby.

# 1.16.0

Expand Down
2 changes: 1 addition & 1 deletion lib/bootsnap/load_path_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def unload!
end

def supported?
RUBY_ENGINE == "ruby" &&
%w[ruby truffleruby].include?(RUBY_ENGINE) &&
RUBY_PLATFORM =~ /darwin|linux|bsd|mswin|mingw|cygwin/
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/bootsnap/load_path_cache/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ def load_dir(dir)
@mutex.synchronize { @dirs[dir] }
end

TRUFFLERUBY_LIB_DIR_PREFIX = if RUBY_ENGINE == "truffleruby"
"#{File.join(RbConfig::CONFIG['libdir'], 'truffle')}#{File::SEPARATOR}"
end

# { 'enumerator' => nil, 'enumerator.so' => nil, ... }
BUILTIN_FEATURES = $LOADED_FEATURES.each_with_object({}) do |feat, features|
if RUBY_ENGINE == "truffleruby" && feat.start_with?(TRUFFLERUBY_LIB_DIR_PREFIX)
feat = feat.slice(TRUFFLERUBY_LIB_DIR_PREFIX.length..-1)
end

# Builtin features are of the form 'enumerator.so'.
# All others include paths.
next unless feat.size < 20 && !feat.include?("/")
Expand Down
12 changes: 12 additions & 0 deletions test/integration/kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class KernelTest < Minitest::Test
include TmpdirHelper

def test_require_symlinked_file_twice
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

setup_symlinked_files
if RUBY_VERSION >= "3.1"
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
Expand Down Expand Up @@ -41,6 +43,8 @@ def test_require_symlinked_file_twice_aliased
end

def test_require_relative_symlinked_file_twice
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

setup_symlinked_files
if RUBY_VERSION >= "3.1"
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
Expand Down Expand Up @@ -83,6 +87,8 @@ def test_require_relative_and_then_require_symlinked_file
end

def test_require_deep_symlinked_file_twice
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

setup_symlinked_files
if RUBY_VERSION >= "3.1"
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
Expand Down Expand Up @@ -116,6 +122,8 @@ def test_require_deep_symlinked_file_twice_aliased
end

def test_require_relative_deep_symlinked_file_twice
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

setup_symlinked_files
if RUBY_VERSION >= "3.1"
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
Expand Down Expand Up @@ -211,5 +219,9 @@ def setup_symlinked_files
RUBY
File.symlink("real", "symlink")
end

def truffleruby?
RUBY_ENGINE == "truffleruby"
end
end
end
36 changes: 29 additions & 7 deletions test/load_path_cache/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ def test_builtin_features
assert_equal false, cache.find("thread")
assert_equal false, cache.find("thread.rb")
assert_equal false, cache.find("enumerator")
assert_equal false, cache.find("enumerator.so")

if RUBY_PLATFORM =~ /darwin/
assert_equal false, cache.find("enumerator.bundle")
if truffleruby?
assert_equal false, cache.find("enumerator.rb")
else
assert_same FALLBACK_SCAN, cache.find("enumerator.bundle")
assert_equal false, cache.find("enumerator.so")
if RUBY_PLATFORM =~ /darwin/
assert_equal false, cache.find("enumerator.bundle")
else
assert_same FALLBACK_SCAN, cache.find("enumerator.bundle")
end
end

bundle = RUBY_PLATFORM =~ /darwin/ ? "bundle" : "so"

# These are not present in TruffleRuby but that means they will still return falsey.
refute(cache.find("thread.#{bundle}"))
refute(cache.find("enumerator.rb"))
refute(cache.find("encdb.#{bundle}"))
Expand Down Expand Up @@ -165,9 +170,15 @@ def test_path_encoding
require path
internal_path = $LOADED_FEATURES.last
assert_equal(OS_ASCII_PATH_ENCODING, internal_path.encoding)
assert_equal(OS_ASCII_PATH_ENCODING, path.encoding)
# TruffleRuby object is a copy and the encoding resets to utf-8.
assert_equal(OS_ASCII_PATH_ENCODING, path.encoding) unless truffleruby?
File.write(path, "")
assert_same path, internal_path

if truffleruby?
assert_equal path, internal_path
else
assert_same path, internal_path
end

utf8_path = cache.find("béé")
assert_equal("#{@dir1}/béé.rb", utf8_path)
Expand All @@ -176,9 +187,20 @@ def test_path_encoding
assert_equal(Encoding::UTF_8, internal_utf8_path.encoding)
assert_equal(Encoding::UTF_8, utf8_path.encoding)
File.write(utf8_path, "")
assert_same utf8_path, internal_utf8_path

if truffleruby?
assert_equal utf8_path, internal_utf8_path
else
assert_same utf8_path, internal_utf8_path
end
end
end

private

def truffleruby?
RUBY_ENGINE == "truffleruby"
end
end
end
end

0 comments on commit 64a4fda

Please sign in to comment.