Skip to content

Commit 387239f

Browse files
justin808claude
andcommitted
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 <noreply@anthropic.com>
1 parent 1a84b4a commit 387239f

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

lib/react_on_rails/dev/pack_generator.rb

Lines changed: 52 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,56 @@ 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 (e.g., called from bin/dev),
29+
# we can directly require and run the task. Otherwise, use bundle exec.
30+
if defined?(Bundler)
31+
run_rake_task_directly(silent: silent)
32+
else
33+
run_via_bundle_exec(silent: silent)
34+
end
35+
end
36+
37+
def run_rake_task_directly(silent: false)
38+
require "rake"
39+
40+
# Load Rails environment if not already loaded
41+
require File.expand_path("config/environment", Dir.pwd) unless defined?(Rails)
42+
43+
Rake::Task.clear
44+
Rails.application.load_tasks unless Rake::Task.task_defined?("react_on_rails:generate_packs")
45+
46+
if silent
47+
original_stdout = $stdout
48+
original_stderr = $stderr
49+
$stdout = StringIO.new
50+
$stderr = StringIO.new
51+
end
52+
53+
begin
54+
Rake::Task["react_on_rails:generate_packs"].invoke
55+
true
56+
rescue StandardError => e
57+
warn "Error generating packs: #{e.message}" unless silent
58+
false
59+
ensure
60+
if silent
61+
$stdout = original_stdout
62+
$stderr = original_stderr
63+
end
64+
end
65+
end
66+
67+
def run_via_bundle_exec(silent: false)
68+
if silent
69+
system "bundle exec rake react_on_rails:generate_packs > /dev/null 2>&1"
70+
else
71+
system "bundle exec rake react_on_rails:generate_packs"
72+
end
73+
end
2474
end
2575
end
2676
end

0 commit comments

Comments
 (0)