@@ -250,6 +250,14 @@ def construct(builder)
250250
251251 spec . extend ( GemSpecQueryable )
252252
253+ verbose "\t gem_dir: #{ spec . gem_dir } "
254+ verbose "\t gem_dir exists: #{ File . directory? ( spec . gem_dir ) } "
255+ loaded_matches = include . include? ( :loaded ) ? features . select { |f | f . subpath? ( spec . gem_dir ) } : [ ]
256+ verbose "\t :loaded candidates in features: #{ loaded_matches . size } "
257+ loaded_matches . each { |f | verbose "\t loaded: #{ f } " }
258+ resource_count = include . include? ( :files ) && File . directory? ( spec . gem_dir ) ? spec . resource_files . size : 0
259+ verbose "\t :files (resource_files) count: #{ resource_count } "
260+
253261 actual_files = spec . find_gem_files ( include , features )
254262 say "\t #{ actual_files . size } files, #{ actual_files . sum ( 0 , &:size ) } bytes"
255263
@@ -328,23 +336,28 @@ def construct(builder)
328336 features . each do |feature |
329337 load_path = @post_env . find_load_path ( feature )
330338 if load_path . nil?
339+ verbose "\t libfile: #{ feature } -> src (no load path)"
331340 source_files << feature
332341 next
333342 end
334343 abs_load_path = Pathname ( @post_env . expand_path ( load_path ) )
335344 if abs_load_path == pre_working_directory
345+ verbose "\t libfile: #{ feature } -> src (pre-working-dir load path)"
336346 source_files << feature
337347 elsif feature . subpath? ( exec_prefix )
338348 # Features found in the Ruby installation are put in the
339349 # temporary Ruby installation.
350+ verbose "\t libfile: #{ feature } -> exec_prefix"
340351 builder . duplicate_to_exec_prefix ( feature )
341352 elsif ( gem_path = GemSpecQueryable . find_gem_path ( feature ) )
342353 # Features found in any other Gem path (e.g. ~/.gems) is put
343354 # in a special 'gems' folder.
355+ verbose "\t libfile: #{ feature } -> gem_home"
344356 builder . duplicate_to_gem_home ( feature , gem_path )
345357 elsif feature . subpath? ( src_prefix ) || abs_load_path == working_directory
346358 # Any feature found inside the src_prefix automatically gets
347359 # added as a source file (to go in 'src').
360+ verbose "\t libfile: #{ feature } -> src (src_prefix/working_dir)"
348361 source_files << feature
349362 # Add the load path unless it was added by the script while
350363 # running (or we assume that the script can also set it up
@@ -354,6 +367,7 @@ def construct(builder)
354367 # Any feature that exist in a load path added by the script
355368 # itself is added as a file to go into the 'src' (src_prefix
356369 # will be adjusted below to point to the common parent).
370+ verbose "\t libfile: #{ feature } -> src (script-added load path)"
357371 source_files << feature
358372 else
359373 # All other feature that can not be resolved go in the the
@@ -439,9 +453,37 @@ def construct(builder)
439453 # Add the load path that are required with the correct path after
440454 # src_prefix was adjusted.
441455 load_path = src_load_path . map { |path | SRCDIR / path . relative_path_from ( inst_src_prefix ) } . uniq
456+
457+ # On POSIX systems, also add the packed Ruby standard library directories
458+ # to RUBYLIB. The Ruby binary has a compiled-in prefix pointing to the build
459+ # host, which doesn't exist on other systems (e.g., Docker with no Ruby).
460+ # By adding the extract-dir equivalents of rubylibdir, sitelibdir, etc. to
461+ # RUBYLIB, Ruby can find rubygems and the standard library in the packed tree.
462+ unless Gem . win_platform?
463+ core_lib_paths = all_core_dir
464+ . select { |dir | dir . subpath? ( exec_prefix ) }
465+ . map { |dir | dir . relative_path_from ( exec_prefix ) }
466+ archdir = Pathname ( RbConfig ::CONFIG [ "archdir" ] )
467+ if archdir . subpath? ( exec_prefix )
468+ core_lib_paths << archdir . relative_path_from ( exec_prefix )
469+ end
470+ load_path = core_lib_paths + load_path
471+ end
472+
442473 builder . set_env_path ( "RUBYLIB" , *load_path )
443474 builder . set_env_path ( "GEM_HOME" , GEMDIR )
444- builder . set_env_path ( "GEM_PATH" , GEMDIR )
475+
476+ gem_paths = [ GEMDIR ]
477+ # On POSIX, default gems (e.g. error_highlight) are stored under the Ruby
478+ # installation's gem dir (Gem.default_dir), not in GEMDIR. Include it in
479+ # GEM_PATH so RubyGems can find and activate them in the extracted tree.
480+ unless Gem . win_platform?
481+ default_gem_dir = Pathname ( Gem . default_dir )
482+ if default_gem_dir . subpath? ( exec_prefix )
483+ gem_paths << default_gem_dir . relative_path_from ( exec_prefix )
484+ end
485+ end
486+ builder . set_env_path ( "GEM_PATH" , *gem_paths )
445487
446488 # Add the opcode to launch the script
447489 installed_ruby_exe = BINDIR / ruby_executable
0 commit comments