@@ -178,10 +178,10 @@ def setup_work_directory
178
178
179
179
def ruby_platform
180
180
@ruby_platform ||= begin
181
- platform_cmd = "#{ RAILS_GEN_ROOT } /ruby/bin/ruby -e 'puts RUBY_DESCRIPTION.split.last '"
181
+ platform_cmd = "#{ RAILS_GEN_ROOT } /ruby/bin/ruby -e 'puts RUBY_PLATFORM '"
182
182
platform = `#{ platform_cmd } ` . strip
183
183
184
- if platform . empty ?
184
+ if platform . blank ?
185
185
# Fallback in case the command fails
186
186
case RbConfig ::CONFIG [ "host_os" ]
187
187
when /darwin/
@@ -195,10 +195,14 @@ def ruby_platform
195
195
end
196
196
end
197
197
198
+ def platform_path
199
+ @platform_path ||= "#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0/#{ ruby_platform } "
200
+ end
201
+
198
202
def ruby_lib_paths
199
203
[
200
204
"#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0" ,
201
- " #{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0/ #{ ruby_platform } "
205
+ platform_path
202
206
] . join ( ":" )
203
207
end
204
208
@@ -221,25 +225,19 @@ def run_isolated_process
221
225
# Ruby
222
226
"GEM_HOME" => "#{ RAILS_GEN_ROOT } /gems" ,
223
227
"GEM_PATH" => "#{ RAILS_GEN_ROOT } /gems" ,
224
- "RUBYOPT" => nil ,
225
- "RUBYLIB" => nil ,
226
- "RUBY_ROOT" => ruby_dir ,
228
+ "RUBYOPT" => "--disable-gems" ,
229
+ "RUBYLIB" => ruby_lib_paths ,
230
+ "RUBY_ROOT" => " #{ RAILS_GEN_ROOT } /ruby" ,
227
231
"RUBY_ENGINE" => "ruby" ,
228
232
"RUBY_VERSION" => RUBY_VERSION ,
229
233
"RUBY_PATCHLEVEL" => nil ,
230
- # asdf (extra thorough)
234
+ # asdf
231
235
"ASDF_DIR" => nil ,
232
236
"ASDF_DATA_DIR" => nil ,
233
237
"ASDF_CONFIG_FILE" => nil ,
234
238
"ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" => nil ,
235
239
"ASDF_RUBY_VERSION" => nil ,
236
240
"ASDF_GEM_HOME" => nil ,
237
- "ASDF_RUBY_PATH" => nil ,
238
- "ASDF_RUBY_HOME" => nil ,
239
- "ASDF_RUBY_ROOT" => nil ,
240
- "ASDF_RUBY_GEMS_PATH" => nil ,
241
- "ASDF_RUBY_GEMS_HOME" => nil ,
242
- "ASDF_RUBY_GEMS_ROOT" => nil ,
243
241
# RVM
244
242
"rvm_bin_path" => nil ,
245
243
"rvm_path" => nil ,
@@ -251,13 +249,10 @@ def run_isolated_process
251
249
"RBENV_ROOT" => nil ,
252
250
# chruby
253
251
"RUBY_AUTO_VERSION" => nil ,
254
- # Minimal PATH with only what we need, ensuring our Ruby is first
252
+ # Minimal PATH with only what we need
255
253
"PATH" => "#{ ruby_dir } /bin:#{ RAILS_GEN_ROOT } /gems/bin:/usr/local/bin:/usr/bin:/bin" ,
256
254
# Extra insurance
257
255
"SHELL" => "/bin/bash" ,
258
- # Force Ruby to ignore other gem paths
259
- "RUBYOPT" => "--disable-gems" , # Start clean
260
- "RUBYLIB" => ruby_lib_paths , # Only our paths
261
256
# Prevent Ruby from looking in user directories
262
257
"HOME" => "/var/lib/rails-new-io/home" ,
263
258
# Prevent loading of any user config
@@ -266,76 +261,77 @@ def run_isolated_process
266
261
"XDG_DATA_HOME" => "/var/lib/rails-new-io/data"
267
262
}
268
263
264
+ @logger . debug ( "Environment before command execution" , {
265
+ gem_home : ENV [ "GEM_HOME" ] ,
266
+ gem_path : ENV [ "GEM_PATH" ] ,
267
+ bundle_path : ENV [ "BUNDLE_PATH" ] ,
268
+ bundle_gemfile : ENV [ "BUNDLE_GEMFILE" ] ,
269
+ rubylib : ENV [ "RUBYLIB" ] ,
270
+ load_path : $LOAD_PATH
271
+ } )
272
+
269
273
# Create isolation directories
270
274
FileUtils . mkdir_p ( "/var/lib/rails-new-io/home" )
271
275
FileUtils . mkdir_p ( "/var/lib/rails-new-io/config" )
272
276
FileUtils . mkdir_p ( "/var/lib/rails-new-io/cache" )
273
277
FileUtils . mkdir_p ( "/var/lib/rails-new-io/data" )
274
278
275
279
env = if @command . start_with? ( "rails new" )
276
- base_env
277
- elsif @command . start_with? ( "bundle" )
278
- # For bundle commands, be extra explicit about the environment
279
280
base_env . merge (
280
- "BUNDLE_GEMFILE" => File . join ( @work_dir , "Gemfile" ) ,
281
- "BUNDLE_APP_CONFIG" => File . join ( @work_dir , ".bundle" ) ,
282
- "BUNDLE_USER_HOME" => "#{ RAILS_GEN_ROOT } /bundle" ,
283
- "BUNDLE_DISABLE_SHARED_GEMS" => "true" ,
284
281
"GEM_HOME" => "#{ RAILS_GEN_ROOT } /gems" ,
285
282
"GEM_PATH" => "#{ RAILS_GEN_ROOT } /gems" ,
286
- "RUBYOPT" => "--disable-gems" , # Start clean
283
+ "BUNDLE_USER_HOME" => "#{ RAILS_GEN_ROOT } /bundle" ,
284
+ "BUNDLE_GEMFILE" => "" ,
285
+ "BUNDLE_BIN" => "" ,
286
+ "BUNDLE_PATH" => "" ,
287
+ "BUNDLE_APP_CONFIG" => "" ,
288
+ "RUBYOPT" => "" ,
287
289
"RUBYLIB" => ruby_lib_paths ,
288
- "PATH" => "#{ ruby_dir } /bin:#{ RAILS_GEN_ROOT } /gems/bin:/usr/local/bin:/usr/bin:/bin" ,
289
- # Extra insurance against asdf
290
- "ASDF_RUBY_VERSION" => nil ,
291
- "ASDF_GEM_HOME" => nil ,
292
- "ASDF_RUBY_PATH" => nil ,
293
- "ASDF_RUBY_HOME" => nil ,
294
- "ASDF_RUBY_ROOT" => nil ,
295
- "ASDF_RUBY_GEMS_PATH" => nil ,
296
- "ASDF_RUBY_GEMS_HOME" => nil ,
297
- "ASDF_RUBY_GEMS_ROOT" => nil
290
+ "RUBY_ROOT" => "#{ RAILS_GEN_ROOT } /ruby" ,
291
+ "RUBY_VERSION" => RUBY_VERSION ,
292
+ "PATH" => "#{ RAILS_GEN_ROOT } /ruby/bin:#{ RAILS_GEN_ROOT } /gems/bin:/usr/local/bin:/usr/bin:/bin"
298
293
)
299
- elsif @command . start_with? ( "rails app:template" )
294
+ elsif @command . start_with? ( "bundle" )
295
+ # For bundle commands, be extra explicit about the environment
300
296
base_env . merge (
301
- "BUNDLE_GEMFILE" => File . join ( @work_dir , "Gemfile" ) ,
302
- "BUNDLE_APP_CONFIG" => File . join ( @work_dir , ".bundle" ) ,
303
- "PATH" => "#{ ruby_dir } /bin:#{ RAILS_GEN_ROOT } /gems/bin:#{ File . join ( @work_dir , 'bin' ) } :/usr/local/bin:/usr/bin:/bin"
297
+ "GEM_HOME" => "#{ RAILS_GEN_ROOT } /gems" ,
298
+ "GEM_PATH" => "#{ RAILS_GEN_ROOT } /gems" ,
299
+ "BUNDLE_USER_HOME" => "#{ RAILS_GEN_ROOT } /bundle" ,
300
+ "BUNDLE_GEMFILE" => "" ,
301
+ "BUNDLE_BIN" => "" ,
302
+ "BUNDLE_PATH" => "" ,
303
+ "BUNDLE_APP_CONFIG" => "" ,
304
+ "RUBYOPT" => "" ,
305
+ "RUBYLIB" => ruby_lib_paths ,
306
+ "RUBY_ROOT" => "#{ RAILS_GEN_ROOT } /ruby" ,
307
+ "RUBY_VERSION" => RUBY_VERSION ,
308
+ "PATH" => "#{ RAILS_GEN_ROOT } /ruby/bin:#{ RAILS_GEN_ROOT } /gems/bin:/usr/local/bin:/usr/bin:/bin"
304
309
)
305
310
else
306
311
base_env . merge (
307
- "BUNDLE_GEMFILE" => File . join ( @work_dir , "Gemfile" ) ,
308
- "PATH" => "#{ ruby_dir } /bin:#{ RAILS_GEN_ROOT } /gems/bin:#{ File . join ( @work_dir , 'bin' ) } :/usr/local/bin:/usr/bin:/bin"
312
+ "GEM_HOME" => "#{ RAILS_GEN_ROOT } /gems" ,
313
+ "GEM_PATH" => "#{ RAILS_GEN_ROOT } /gems" ,
314
+ "BUNDLE_USER_HOME" => "#{ RAILS_GEN_ROOT } /bundle" ,
315
+ "BUNDLE_GEMFILE" => "" ,
316
+ "BUNDLE_BIN" => "" ,
317
+ "BUNDLE_PATH" => "" ,
318
+ "BUNDLE_APP_CONFIG" => "" ,
319
+ "RUBYOPT" => "" ,
320
+ "RUBYLIB" => ruby_lib_paths ,
321
+ "RUBY_ROOT" => "#{ RAILS_GEN_ROOT } /ruby" ,
322
+ "RUBY_VERSION" => RUBY_VERSION ,
323
+ "PATH" => "#{ RAILS_GEN_ROOT } /ruby/bin:#{ RAILS_GEN_ROOT } /gems/bin:/usr/local/bin:/usr/bin:/bin"
309
324
)
310
325
end
311
326
312
- bundle_command = if @command . start_with? ( "rails new" )
313
- @command
314
- elsif @command . start_with? ( "bundle" )
315
- # For bundle commands, bypass the bundle executable entirely and run the code directly
316
- command_parts = @command . split
317
- bundle_args = command_parts [ 1 ..-1 ] . join ( " " )
318
- [
319
- "#{ RAILS_GEN_ROOT } /ruby/bin/ruby" ,
320
- "-I#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0" ,
321
- "-I#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0/#{ ruby_platform } " ,
322
- "-e" ,
323
- "'require \" bundler\" ; Bundler::CLI.start([#{ bundle_args . split . map { |arg | %Q(\" #{ arg } \" ) } . join ( ", " ) } ])'"
324
- ] . join ( " " )
325
- else
326
- # Instead of using ./bin/rails, explicitly use our Ruby and load bundler from Ruby's installation
327
- command_parts = @command . split
328
- command_parts [ 0 ] = [
329
- "#{ RAILS_GEN_ROOT } /ruby/bin/ruby" ,
330
- "-I#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0" ,
331
- "-I#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0/#{ ruby_platform } " ,
332
- "-I#{ RAILS_GEN_ROOT } /gems/gems" ,
333
- "-r bundler/setup" ,
334
- "-e" ,
335
- "'load \" #{ RAILS_GEN_ROOT } /gems/bin/rails\" '"
336
- ] . join ( " " )
337
- command_parts . join ( " " )
338
- end
327
+ @logger . debug ( "Environment after command execution setup" , {
328
+ gem_home : env [ "GEM_HOME" ] ,
329
+ gem_path : env [ "GEM_PATH" ] ,
330
+ bundle_path : env [ "BUNDLE_PATH" ] ,
331
+ bundle_gemfile : env [ "BUNDLE_GEMFILE" ] ,
332
+ rubylib : env [ "RUBYLIB" ] ,
333
+ command : bundle_command
334
+ } )
339
335
340
336
# For non-rails-new commands, we need to run bundle install first
341
337
if !@command . start_with? ( "rails new" ) && !@command . start_with? ( "bundle" )
@@ -379,6 +375,7 @@ def run_isolated_process
379
375
stderr_thread = Thread . new do
380
376
stderr . each_line do |line |
381
377
error_buffer << line . strip
378
+ @logger . debug ( "Command stderr: #{ line . strip } " )
382
379
end
383
380
end
384
381
@@ -393,7 +390,16 @@ def run_isolated_process
393
390
@logger . error ( "Command failed" , {
394
391
status : exit_status ,
395
392
output : output ,
396
- error_buffer : error_buffer . join ( "<br>" )
393
+ error_buffer : error_buffer . join ( "<br>" ) ,
394
+ command : bundle_command ,
395
+ directory : @work_dir ,
396
+ load_paths : [
397
+ "#{ RAILS_GEN_ROOT } /ruby/lib/ruby/3.4.0" ,
398
+ platform_path ,
399
+ "#{ RAILS_GEN_ROOT } /gems/gems" ,
400
+ "#{ RAILS_GEN_ROOT } /gems/gems/rails-#{ RAILS_VERSION } /lib" ,
401
+ "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /lib"
402
+ ]
397
403
} )
398
404
raise "Command failed with status: #{ exit_status } "
399
405
end
@@ -475,4 +481,51 @@ def jemalloc_lib_path
475
481
"/usr/lib/x86_64-linux-gnu/libjemalloc.so.2"
476
482
end
477
483
end
484
+
485
+ def bundle_command
486
+ @logger . debug ( "Starting bundle_command construction" )
487
+ @logger . debug ( "Command is: #{ @command } " )
488
+ @logger . debug ( "Platform path is: #{ platform_path } " )
489
+
490
+ # Debug: Check what's in the gems directory
491
+ @logger . debug ( "Checking Rails installation" , {
492
+ rails_cli_path : "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /lib/rails/cli.rb" ,
493
+ rails_cli_exists : File . exist? ( "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /lib/rails/cli.rb" ) ,
494
+ rails_lib_path : "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /lib" ,
495
+ rails_lib_exists : File . exist? ( "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /lib" ) ,
496
+ rails_gem_path : "#{ RAILS_GEN_ROOT } /gems/gems/rails-#{ RAILS_VERSION } " ,
497
+ rails_gem_exists : File . exist? ( "#{ RAILS_GEN_ROOT } /gems/gems/rails-#{ RAILS_VERSION } " ) ,
498
+ gem_home_contents : Dir . glob ( "#{ ENV [ 'GEM_HOME' ] } /*" ) . join ( ", " ) ,
499
+ gem_home_gems : Dir . glob ( "#{ ENV [ 'GEM_HOME' ] } /gems/*" ) . join ( ", " ) ,
500
+ rails_gem_contents : Dir . glob ( "#{ RAILS_GEN_ROOT } /gems/gems/rails-#{ RAILS_VERSION } /*" ) . join ( ", " ) ,
501
+ railties_gem_contents : Dir . glob ( "#{ RAILS_GEN_ROOT } /gems/gems/railties-#{ RAILS_VERSION } /*" ) . join ( ", " )
502
+ } )
503
+
504
+ # Debug: Check load paths
505
+ @logger . debug ( "Ruby load paths" , {
506
+ load_path : $LOAD_PATH,
507
+ gem_path : Gem . path ,
508
+ gem_paths_exist : Gem . path . map { |p | [ p , Dir . exist? ( p ) ] } . to_h
509
+ } )
510
+
511
+ command = if @command . start_with? ( "rails new" )
512
+ # For rails new, use the rails executable
513
+ command_parts = @command . split
514
+ command_parts [ 0 ] = "#{ RAILS_GEN_ROOT } /gems/bin/rails"
515
+ command_parts . join ( " " ) . tap { |cmd | @logger . debug ( "Constructed rails new command: #{ cmd } " ) }
516
+ elsif @command . start_with? ( "bundle" )
517
+ # For bundle commands, use the bundle executable
518
+ command_parts = @command . split
519
+ command_parts [ 0 ] = "#{ RAILS_GEN_ROOT } /gems/bin/bundle"
520
+ command_parts . join ( " " ) . tap { |cmd | @logger . debug ( "Constructed bundle command: #{ cmd } " ) }
521
+ else
522
+ # For other rails commands, use the rails executable
523
+ command_parts = @command . split
524
+ command_parts [ 0 ] = "#{ RAILS_GEN_ROOT } /gems/bin/rails"
525
+ command_parts . join ( " " ) . tap { |cmd | @logger . debug ( "Constructed other command: #{ cmd } " ) }
526
+ end
527
+
528
+ @logger . debug ( "Final command: #{ command } " )
529
+ command
530
+ end
478
531
end
0 commit comments