Skip to content

Commit 9431cdc

Browse files
justin808claude
andcommitted
Fix: Skip validation in precompile hook to prevent generator failures
The shakapacker-precompile-hook was causing generator failures because it loads the Rails environment (which triggers version validation) before the react-on-rails npm package is installed during initial setup. This fix adds ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true" at the start of the hook script to skip version validation, since the hook runs as part of the build process and doesn't need package version validation. Fixes CI failures in generator tests where the hook was executed during `rails generate react_on_rails:install` before packages were installed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 39cdc26 commit 9431cdc

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def copy_base_files
4545
template("#{base_path}/#{file}.tt", file)
4646
end
4747

48-
# Make the hook script executable
49-
File.chmod(0o755, "bin/shakapacker-precompile-hook") if File.exist?("bin/shakapacker-precompile-hook")
48+
# Make the hook script executable (copy_file guarantees it exists)
49+
File.chmod(0o755, File.join(destination_root, "bin/shakapacker-precompile-hook"))
5050
end
5151

5252
def copy_js_bundle_files

lib/generators/react_on_rails/templates/base/base/bin/shakapacker-precompile-hook

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
# for auto-bundled components. It's called automatically by Shakapacker
88
# when configured in config/shakapacker.yml:
99
# precompile_hook: 'bin/shakapacker-precompile-hook'
10+
#
11+
# Emoji Scheme:
12+
# 🔄 = Running/in-progress
13+
# ✅ = Success
14+
# ❌ = Error
15+
16+
# Skip validation during precompile hook execution
17+
# The hook runs early in the build process, potentially before full Rails initialization,
18+
# and doesn't need package version validation since it's part of the build itself
19+
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
1020

1121
require_relative "../config/environment"
1222

spec/react_on_rails/dev/pack_generator_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@
55

66
RSpec.describe ReactOnRails::Dev::PackGenerator do
77
describe ".generate" do
8+
context "when shakapacker precompile hook is configured" do
9+
before do
10+
allow(ReactOnRails::PackerUtils).to receive(:shakapacker_precompile_hook_configured?).and_return(true)
11+
end
12+
13+
it "skips pack generation in verbose mode" do
14+
expect { described_class.generate(verbose: true) }
15+
.to output(/⏭️ Skipping pack generation/).to_stdout_from_any_process
16+
end
17+
18+
it "skips pack generation silently in quiet mode" do
19+
expect { described_class.generate(verbose: false) }
20+
.not_to output.to_stdout_from_any_process
21+
end
22+
23+
it "does not invoke the rake task" do
24+
# Mock the task to ensure it's not called
25+
mock_task = instance_double(Rake::Task)
26+
allow(Rake::Task).to receive(:[]).with("react_on_rails:generate_packs").and_return(mock_task)
27+
allow(mock_task).to receive(:invoke)
28+
29+
described_class.generate(verbose: false)
30+
31+
expect(mock_task).not_to have_received(:invoke)
32+
end
33+
end
34+
835
context "when in Bundler context with Rails available" do
936
let(:mock_task) { instance_double(Rake::Task) }
1037
let(:mock_rails_app) do
@@ -17,6 +44,9 @@
1744
end
1845

1946
before do
47+
# Ensure precompile hook is not configured for these tests
48+
allow(ReactOnRails::PackerUtils).to receive(:shakapacker_precompile_hook_configured?).and_return(false)
49+
2050
# Setup Bundler context
2151
stub_const("Bundler", Module.new)
2252
allow(ENV).to receive(:[]).and_call_original

spec/react_on_rails/packer_utils_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,48 @@ module ReactOnRails
139139
expect(described_class.supports_autobundling?).to be(false)
140140
end
141141
end
142+
143+
describe ".shakapacker_precompile_hook_configured?" do
144+
let(:mock_config) { instance_double("::Shakapacker::Config") } # rubocop:disable RSpec/VerifiedDoubleReference
145+
146+
before do
147+
allow(::Shakapacker).to receive(:config).and_return(mock_config)
148+
end
149+
150+
context "when precompile_hook is configured" do
151+
it "returns true" do
152+
allow(mock_config).to receive(:send).with(:data).and_return({ precompile_hook: "bin/hook" })
153+
expect(described_class.shakapacker_precompile_hook_configured?).to be true
154+
end
155+
end
156+
157+
context "when precompile_hook is not configured" do
158+
it "returns false for nil" do
159+
allow(mock_config).to receive(:send).with(:data).and_return({ precompile_hook: nil })
160+
expect(described_class.shakapacker_precompile_hook_configured?).to be false
161+
end
162+
163+
it "returns false for empty string" do
164+
allow(mock_config).to receive(:send).with(:data).and_return({ precompile_hook: "" })
165+
expect(described_class.shakapacker_precompile_hook_configured?).to be false
166+
end
167+
end
168+
169+
context "when Shakapacker is not available" do
170+
before { hide_const("::Shakapacker") }
171+
172+
it "returns false" do
173+
expect(described_class.shakapacker_precompile_hook_configured?).to be false
174+
end
175+
end
176+
177+
context "when config.send raises an error" do
178+
it "returns false" do
179+
allow(mock_config).to receive(:send).and_raise(NoMethodError)
180+
expect(described_class.shakapacker_precompile_hook_configured?).to be false
181+
end
182+
end
183+
end
142184
end
143185

144186
describe "version constants validation" do

0 commit comments

Comments
 (0)