Fix load-based precompile hook execution path#2419
Conversation
WalkthroughReplace the conditional inline execution in the shared Shakapacker precompile hook with a public Changes
Sequence Diagram(s)sequenceDiagram
participant HookCaller as Bin script
participant Shared as shakapacker_precompile_hook_shared.rb
participant Rescript as build_rescript_if_needed
participant Packs as generate_packs_if_needed
HookCaller->>Shared: load shared hook
HookCaller->>Shared: run_precompile_tasks()
Shared->>Rescript: build_rescript_if_needed()
Rescript-->>Shared: build complete
Shared->>Packs: generate_packs_if_needed()
Packs-->>Shared: generation complete
Shared-->>HookCaller: return
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryRefactors the shared precompile hook to expose a
Confidence Score: 3/5
Important Files Changed
Flowchartflowchart TD
A["bin/shakapacker-precompile-hook<br/>(OSS or Pro dummy)"] -->|"check ENV guard"| B{SHAKAPACKER_SKIP_PRECOMPILE_HOOK?}
B -->|"true"| C["exit 0"]
B -->|"false"| D["load shakapacker_precompile_hook_shared.rb"]
D --> E["call run_precompile_tasks()"]
E --> F["build_rescript_if_needed()"]
E --> G["generate_packs_if_needed()"]
H["Direct execution<br/>(__FILE__ == $PROGRAM_NAME)"] --> E
style A fill:#4a90d9,color:#fff
style E fill:#2ecc71,color:#fff
style H fill:#e67e22,color:#fff
Last reviewed commit: 7b8eaef |
| before do | ||
| load File.expand_path("../support/shakapacker_precompile_hook_shared.rb", __dir__) | ||
| end | ||
|
|
||
| it "exposes run_precompile_tasks for load-based callers" do | ||
| allow(self).to receive(:build_rescript_if_needed) | ||
| allow(self).to receive(:generate_packs_if_needed) | ||
|
|
||
| run_precompile_tasks | ||
|
|
||
| expect(self).to have_received(:build_rescript_if_needed) | ||
| expect(self).to have_received(:generate_packs_if_needed) | ||
| end |
There was a problem hiding this comment.
Global method pollution from load in before block
Using load in the before block defines run_precompile_tasks, build_rescript_if_needed, generate_packs_if_needed, find_rails_root, and detect_package_manager as private instance methods on Object. These persist for the entire test suite run and could interfere with other specs.
Consider wrapping the load in a clean module to isolate these methods:
| before do | |
| load File.expand_path("../support/shakapacker_precompile_hook_shared.rb", __dir__) | |
| end | |
| it "exposes run_precompile_tasks for load-based callers" do | |
| allow(self).to receive(:build_rescript_if_needed) | |
| allow(self).to receive(:generate_packs_if_needed) | |
| run_precompile_tasks | |
| expect(self).to have_received(:build_rescript_if_needed) | |
| expect(self).to have_received(:generate_packs_if_needed) | |
| end | |
| let(:hook_module) do | |
| Module.new.tap { |mod| mod.module_eval(File.read(File.expand_path("../support/shakapacker_precompile_hook_shared.rb", __dir__))) } | |
| end | |
| it "exposes run_precompile_tasks for load-based callers" do | |
| allow(hook_module).to receive(:build_rescript_if_needed) | |
| allow(hook_module).to receive(:generate_packs_if_needed) | |
| hook_module.run_precompile_tasks | |
| expect(hook_module).to have_received(:build_rescript_if_needed) | |
| expect(hook_module).to have_received(:generate_packs_if_needed) | |
| end |
Alternatively, at minimum, the before block should use load shared_path, true (the second argument wraps the code in an anonymous module) to prevent global leakage:
before do
load File.expand_path("../support/shakapacker_precompile_hook_shared.rb", __dir__), true
endNote: using load path, true would wrap the methods in an anonymous module, making them inaccessible from the test. So the Module.new approach or a different isolation strategy would be needed if this is a concern.
| # frozen_string_literal: true | ||
|
|
||
| require_relative "spec_helper" | ||
|
|
||
| RSpec.describe "Shakapacker precompile hook shared script" do | ||
| before do | ||
| load File.expand_path("../support/shakapacker_precompile_hook_shared.rb", __dir__) | ||
| end | ||
|
|
||
| it "exposes run_precompile_tasks for load-based callers" do | ||
| allow(self).to receive(:build_rescript_if_needed) | ||
| allow(self).to receive(:generate_packs_if_needed) | ||
|
|
||
| run_precompile_tasks | ||
|
|
||
| expect(self).to have_received(:build_rescript_if_needed) | ||
| expect(self).to have_received(:generate_packs_if_needed) | ||
| end | ||
| end |
There was a problem hiding this comment.
Test was not run per PR description
The PR description states this test is "UNTESTED in this environment" due to a Ruby version mismatch. Per AGENTS.md policy: "Never claim a test is 'fixed' without running it locally first." The test should be verified in CI or by another contributor before merging.
Additionally, the test only asserts that run_precompile_tasks delegates to the two sub-methods — something trivially obvious from the 3-line implementation. It doesn't test the actual load-based execution path from the hook wrappers (i.e., that load shared_hook followed by run_precompile_tasks works end-to-end). A more meaningful test would invoke the dummy bin/shakapacker-precompile-hook script in a subprocess and verify the expected output or side effects.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Add entries for user-visible changes since v16.4.0.rc.6: - #2539: env-var-driven ports in Procfile templates - #2417: rspack generator config path fix - #2419: precompile hook load-based execution fix - #2577: create-react-on-rails-app validation improvements - #2416: StreamResponse status fallback fix (Pro) - #2566: empty-string license plan mismatch fix (Pro) - Updated #2561 entry to include #2568 contributor credit Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
## Summary - Add changelog entries for 6 user-visible PRs merged since v16.4.0.rc.6 that were missing from `[Unreleased]` - Update existing #2561 entry to include #2568 contributor credit ### New entries added | Section | PR | Description | |---|---|---| | Added | #2539 | Environment-variable-driven ports in Procfile templates | | Fixed | #2417 | Rspack generator config path fix | | Fixed | #2419 | Precompile hook load-based execution fix | | Fixed | #2577 | `create-react-on-rails-app` validation improvements | | Pro Fixed | #2416 | StreamResponse status fallback fix | | Pro Fixed | #2566 | Empty-string license plan mismatch fix | ### Skipped PRs (not user-visible) Docs (#2406, #2414, #2479, #2494, #2518, #2537, #2544), CI/internal (#2533, #2547, #2555, #2557, #2558, #2564), dependabot (#2387, #2541), dev dependencies (#2559, #2569, #2573). ## Test plan - [ ] Verify changelog formatting matches existing entries - [ ] Verify all user-visible PRs since v16.4.0.rc.6 are covered 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Documentation-only changelog updates with no runtime or build behavior changes. > > **Overview** > Updates `CHANGELOG.md`’s **[Unreleased]** section to include previously missing user-facing entries: Procfile templates now support env-driven ports, several generator/`bin/dev` precompile-hook and rspack-path fixes are documented, and `create-react-on-rails-app` validation improvements are noted. > > Also adds two Pro fix entries (StreamResponse status fallback and license plan empty-string validation) and updates the existing `bin/dev` precompile-hook entry to credit an additional PR/contributor. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit e75d2b5. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Summary
run_precompile_tasksentrypoint inshakapacker_precompile_hook_shared.rbrun_precompile_tasksafterload, instead of relying on__FILE__ == $PROGRAM_NAMECloses #2195
Test plan
bundle exec rspec react_on_rails/spec/react_on_rails/shakapacker_precompile_hook_shared_spec.rb2.6.10) vs project requirement (>= 3.0.0)Summary by CodeRabbit
Tests
Refactor