2525 rails_version = latest_version
2626end
2727
28+
2829# Get currently installed Rails versions
2930rails_gems = `gem list rails -l`
3031installed_versions = rails_gems . scan ( /rails \( ([^)]+)\) / ) . flatten . first &.split ( ", " ) || [ ]
3132
33+
3234# Check if we need to install this version
3335if !installed_versions . include? ( rails_version )
3436 puts "Rails #{ rails_version } not found. Installing..."
3739 puts "Rails #{ rails_version } already installed"
3840end
3941
40- require "bundler/setup"
42+
43+ # Don't require bundler/setup - we want to avoid loading our project's Gemfile
4144
4245require "fileutils"
43- require "rails/version"
4446require "erb"
4547require "sorbet-runtime"
4648
49+ # Make the rails_version available as an instance variable for the ERB templates
50+ @rails_version = rails_version
51+
4752# Path constants
4853ROOT_DIR = File . expand_path ( ".." , __dir__ )
4954TEMPLATE_DIR = File . expand_path ( "templates" , __dir__ )
5459 "PATH" => ENV [ "PATH" ] ,
5560 "HOME" => ENV [ "HOME" ] ,
5661 "RAILS_ENV" => "development" ,
62+ "RAILS_VERSION" => nil ,
63+ "RUBY_VERSION" => nil ,
5764 "BUNDLE_GEMFILE" => nil ,
5865 "GEM_HOME" => nil ,
5966 "GEM_PATH" => nil ,
@@ -114,6 +121,8 @@ def copy_template(file, target_path = nil)
114121if !skip_app_creation
115122 # Use rails new to create a new application
116123 puts "Creating new Rails application with version #{ rails_version } ..."
124+
125+ # Try using gem's exec command with proper version specification
117126 rails_new_command = "rails _#{ rails_version } _ new #{ RAILS_APP_DIR } --skip-git --skip-keeps --skip-action-cable " \
118127 "--skip-sprockets --skip-javascript --skip-hotwire --skip-jbuilder --skip-asset-pipeline " \
119128 "--skip-bootsnap --api -T"
@@ -124,6 +133,30 @@ def copy_template(file, target_path = nil)
124133 puts "=> Deleting default .rubocop.yml"
125134 FileUtils . rm_f ( File . join ( RAILS_APP_DIR , ".rubocop.yml" ) )
126135
136+ # Verify we're using the correct Rails version defaults
137+ if rails_version . start_with? ( "8.0" )
138+ defaults_dir = File . join ( RAILS_APP_DIR , "config/initializers" )
139+ rails72_defaults = File . join ( defaults_dir , "new_framework_defaults_7_2.rb" )
140+ rails80_defaults = File . join ( defaults_dir , "new_framework_defaults_8_0.rb" )
141+
142+ # Check if we have the wrong defaults file
143+ if File . exist? ( rails72_defaults ) && !File . exist? ( rails80_defaults )
144+ abort ( <<~ERROR )
145+ ERROR: Expected Rails 8.0 but found Rails 7.2 defaults file!
146+
147+ This indicates a serious problem with how Rails is being executed.
148+ You have Rails 8.0.1 installed, but the generator is using Rails 7.2 behavior.
149+
150+ Possible solutions to try:
151+ 1. Make sure your Ruby environment is set up correctly
152+ 2. Try using a direct path to the rails executable
153+ 3. Try running the command manually outside of this script
154+
155+ Command attempted: #{ rails_new_command }
156+ ERROR
157+ end
158+ end
159+
127160 # Update Gemfile to include the local logstruct gem and test gems
128161 gemfile_path = File . join ( RAILS_APP_DIR , "Gemfile" )
129162 gemfile_content = File . read ( gemfile_path )
@@ -179,11 +212,6 @@ def copy_template(file, target_path = nil)
179212 copy_template ( relative_path )
180213end
181214
182- # # Run bundle install again to ensure all dependencies are correctly resolved
183- # puts "Running final bundle install..."
184- # Dir.chdir(RAILS_APP_DIR) do
185- # system(clean_env, "bundle install") || abort("Bundle install failed")
186- # end
187215
188216# Set up the database
189217puts "Setting up Rails application in #{ RAILS_APP_DIR } ..."
0 commit comments