Skip to content

Commit 5186da7

Browse files
justin808claude
andauthored
Fix bin/dev pack generation in Bundler context (#1907)
* Fix bin/dev pack generation failure when run from Bundler context When bin/dev runs in a Bundler context, calling `bundle exec rake` can fail or cause issues. This change detects when we're already inside a Bundler context and runs the Rake task directly instead of shelling out. Key improvements: - Detects Bundler context and runs tasks directly - Falls back to bundle exec when not in Bundler context - Properly handles silent mode for both execution paths - Loads Rails environment when needed This fixes pack generation failures when using bin/dev. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Add rails_available? check to prevent nil Rails.application The previous implementation didn't check if Rails.application was available before calling load_tasks, which caused test failures. This adds the rails_available? method to check if Rails is properly initialized. Also updates run_rake_task_directly to use task.reenable for better re-execution support. Fixes RSpec test failures in pack_generator_spec.rb * Improve pack generator robustness and error handling Based on code review feedback, this commit addresses several critical issues: 1. **Better Bundler context detection**: Now checks ENV['BUNDLE_GEMFILE'] for more robust detection of meaningful Bundler contexts 2. **Enhanced Rails availability checking**: Safely verifies Rails.application can actually load tasks, with proper error handling 3. **Consistent error handling**: Errors are always written to stderr, even in silent mode. Backtrace included when DEBUG env var is set 4. **Reduced code complexity**: Refactored run_rake_task_directly into smaller, focused methods (load_rake_tasks, prepare_rake_task, capture_output, handle_rake_error) to satisfy RuboCop complexity checks 5. **Maintained test compatibility**: Tests continue to pass because the code properly falls back to bundle exec in test environment where BUNDLE_GEMFILE is not set These changes ensure: - Tests work correctly (they mock system calls which are used in test env) - Production bin/dev usage is optimized (direct Rake execution when safe) - Error messages are never silently swallowed - More robust detection of execution context --------- Co-authored-by: Claude <[email protected]>
1 parent 3182910 commit 5186da7

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

lib/react_on_rails/dev/pack_generator.rb

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class << self
99
def generate(verbose: false)
1010
if verbose
1111
puts "📦 Generating React on Rails packs..."
12-
success = system "bundle exec rake react_on_rails:generate_packs"
12+
success = run_pack_generation
1313
else
1414
print "📦 Generating packs... "
15-
success = system "bundle exec rake react_on_rails:generate_packs > /dev/null 2>&1"
15+
success = run_pack_generation(silent: true)
1616
puts success ? "✅" : "❌"
1717
end
1818

@@ -21,6 +21,100 @@ def generate(verbose: false)
2121
puts "❌ Pack generation failed"
2222
exit 1
2323
end
24+
25+
private
26+
27+
def run_pack_generation(silent: false)
28+
# If we're already inside a Bundler context AND Rails is available (e.g., called from bin/dev),
29+
# we can directly require and run the task. Otherwise, use bundle exec.
30+
if should_run_directly?
31+
run_rake_task_directly(silent: silent)
32+
else
33+
run_via_bundle_exec(silent: silent)
34+
end
35+
end
36+
37+
def should_run_directly?
38+
# Check if we're in a meaningful Bundler context with BUNDLE_GEMFILE
39+
return false unless defined?(Bundler)
40+
return false unless ENV["BUNDLE_GEMFILE"]
41+
return false unless rails_available?
42+
43+
true
44+
end
45+
46+
def rails_available?
47+
return false unless defined?(Rails)
48+
return false unless Rails.respond_to?(:application)
49+
return false if Rails.application.nil?
50+
51+
# Verify Rails app can actually load tasks
52+
begin
53+
Rails.application.respond_to?(:load_tasks)
54+
rescue StandardError
55+
false
56+
end
57+
end
58+
59+
def run_rake_task_directly(silent: false)
60+
require "rake"
61+
62+
load_rake_tasks
63+
task = prepare_rake_task
64+
65+
capture_output(silent) do
66+
task.invoke
67+
true
68+
end
69+
rescue StandardError => e
70+
handle_rake_error(e, silent)
71+
false
72+
end
73+
74+
def load_rake_tasks
75+
return if Rake::Task.task_defined?("react_on_rails:generate_packs")
76+
77+
Rails.application.load_tasks
78+
end
79+
80+
def prepare_rake_task
81+
task = Rake::Task["react_on_rails:generate_packs"]
82+
task.reenable # Allow re-execution if called multiple times
83+
task
84+
end
85+
86+
def capture_output(silent)
87+
return yield unless silent
88+
89+
original_stdout = $stdout
90+
original_stderr = $stderr
91+
output_buffer = StringIO.new
92+
$stdout = output_buffer
93+
$stderr = output_buffer
94+
95+
begin
96+
yield
97+
ensure
98+
$stdout = original_stdout
99+
$stderr = original_stderr
100+
end
101+
end
102+
103+
def handle_rake_error(error, _silent)
104+
error_msg = "Error generating packs: #{error.message}"
105+
error_msg += "\n#{error.backtrace.join("\n")}" if ENV["DEBUG"]
106+
107+
# Always write to stderr, even in silent mode
108+
warn error_msg
109+
end
110+
111+
def run_via_bundle_exec(silent: false)
112+
if silent
113+
system "bundle exec rake react_on_rails:generate_packs > /dev/null 2>&1"
114+
else
115+
system "bundle exec rake react_on_rails:generate_packs"
116+
end
117+
end
24118
end
25119
end
26120
end

0 commit comments

Comments
 (0)