Skip to content

Commit d531d5a

Browse files
committed
Make load path cache work on truffleruby
1 parent 640c126 commit d531d5a

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
fail-fast: false
4444
matrix:
4545
os: [ubuntu]
46-
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2', 'ruby-head', 'debug', 'truffleruby']
46+
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2', 'ruby-head', 'debug', 'truffleruby', 'truffleruby-head']
4747
runs-on: ${{ matrix.os }}-latest
4848
steps:
4949
- uses: actions/checkout@v2

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
* Support YAML and JSON CompileCache on TruffleRuby.
4+
* Support LoadPathCache on TruffleRuby.
45

56
# 1.16.0
67

lib/bootsnap/load_path_cache.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@ def unload!
5454
end
5555

5656
def supported?
57-
RUBY_ENGINE == "ruby" &&
58-
RUBY_PLATFORM =~ /darwin|linux|bsd|mswin|mingw|cygwin/
57+
case RUBY_ENGINE
58+
when "truffleruby"
59+
# https://github.com/oracle/truffleruby/issues/3131
60+
return false if RUBY_ENGINE_VERSION < "23.1.0"
61+
when "ruby"
62+
# fall through
63+
else
64+
return false
65+
end
66+
67+
RUBY_PLATFORM.match?(/darwin|linux|bsd|mswin|mingw|cygwin/)
5968
end
6069
end
6170
end

lib/bootsnap/load_path_cache/cache.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ def load_dir(dir)
2424
@mutex.synchronize { @dirs[dir] }
2525
end
2626

27+
TRUFFLERUBY_LIB_DIR_PREFIX = if RUBY_ENGINE == "truffleruby"
28+
"#{File.join(RbConfig::CONFIG['libdir'], 'truffle')}#{File::SEPARATOR}"
29+
end
30+
2731
# { 'enumerator' => nil, 'enumerator.so' => nil, ... }
2832
BUILTIN_FEATURES = $LOADED_FEATURES.each_with_object({}) do |feat, features|
33+
if TRUFFLERUBY_LIB_DIR_PREFIX && feat.start_with?(TRUFFLERUBY_LIB_DIR_PREFIX)
34+
feat = feat.byteslice(TRUFFLERUBY_LIB_DIR_PREFIX.bytesize..-1)
35+
end
36+
2937
# Builtin features are of the form 'enumerator.so'.
3038
# All others include paths.
3139
next unless feat.size < 20 && !feat.include?("/")

test/integration/kernel_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class KernelTest < Minitest::Test
88
include TmpdirHelper
99

1010
def test_require_symlinked_file_twice
11+
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?
12+
1113
setup_symlinked_files
1214
if RUBY_VERSION >= "3.1"
1315
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
@@ -41,6 +43,8 @@ def test_require_symlinked_file_twice_aliased
4143
end
4244

4345
def test_require_relative_symlinked_file_twice
46+
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?
47+
4448
setup_symlinked_files
4549
if RUBY_VERSION >= "3.1"
4650
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
@@ -83,6 +87,8 @@ def test_require_relative_and_then_require_symlinked_file
8387
end
8488

8589
def test_require_deep_symlinked_file_twice
90+
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?
91+
8692
setup_symlinked_files
8793
if RUBY_VERSION >= "3.1"
8894
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
@@ -116,6 +122,8 @@ def test_require_deep_symlinked_file_twice_aliased
116122
end
117123

118124
def test_require_relative_deep_symlinked_file_twice
125+
skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?
126+
119127
setup_symlinked_files
120128
if RUBY_VERSION >= "3.1"
121129
# Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
@@ -211,5 +219,9 @@ def setup_symlinked_files
211219
RUBY
212220
File.symlink("real", "symlink")
213221
end
222+
223+
def truffleruby?
224+
RUBY_ENGINE == "truffleruby"
225+
end
214226
end
215227
end

test/load_path_cache/cache_test.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ def test_builtin_features
3535
assert_equal false, cache.find("thread")
3636
assert_equal false, cache.find("thread.rb")
3737
assert_equal false, cache.find("enumerator")
38-
assert_equal false, cache.find("enumerator.so")
3938

40-
if RUBY_PLATFORM =~ /darwin/
41-
assert_equal false, cache.find("enumerator.bundle")
39+
if truffleruby?
40+
assert_equal false, cache.find("enumerator.rb")
4241
else
43-
assert_same FALLBACK_SCAN, cache.find("enumerator.bundle")
42+
assert_equal false, cache.find("enumerator.so")
43+
if RUBY_PLATFORM =~ /darwin/
44+
assert_equal false, cache.find("enumerator.bundle")
45+
else
46+
assert_same FALLBACK_SCAN, cache.find("enumerator.bundle")
47+
end
4448
end
4549

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

52+
# These are not present in TruffleRuby but that means they will still return falsey.
4853
refute(cache.find("thread.#{bundle}"))
4954
refute(cache.find("enumerator.rb"))
5055
refute(cache.find("encdb.#{bundle}"))
@@ -165,9 +170,15 @@ def test_path_encoding
165170
require path
166171
internal_path = $LOADED_FEATURES.last
167172
assert_equal(OS_ASCII_PATH_ENCODING, internal_path.encoding)
168-
assert_equal(OS_ASCII_PATH_ENCODING, path.encoding)
173+
# TruffleRuby object is a copy and the encoding resets to utf-8.
174+
assert_equal(OS_ASCII_PATH_ENCODING, path.encoding) unless truffleruby?
169175
File.write(path, "")
170-
assert_same path, internal_path
176+
177+
if truffleruby?
178+
assert_equal path, internal_path
179+
else
180+
assert_same path, internal_path
181+
end
171182

172183
utf8_path = cache.find("béé")
173184
assert_equal("#{@dir1}/béé.rb", utf8_path)
@@ -176,9 +187,20 @@ def test_path_encoding
176187
assert_equal(Encoding::UTF_8, internal_utf8_path.encoding)
177188
assert_equal(Encoding::UTF_8, utf8_path.encoding)
178189
File.write(utf8_path, "")
179-
assert_same utf8_path, internal_utf8_path
190+
191+
if truffleruby?
192+
assert_equal utf8_path, internal_utf8_path
193+
else
194+
assert_same utf8_path, internal_utf8_path
195+
end
180196
end
181197
end
198+
199+
private
200+
201+
def truffleruby?
202+
RUBY_ENGINE == "truffleruby"
203+
end
182204
end
183205
end
184206
end

0 commit comments

Comments
 (0)