-
-
Notifications
You must be signed in to change notification settings - Fork 638
Fix generator validation by using environment variable #1923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The ARGV-based check for generator runtime was unreliable because Rails can modify or clear ARGV during initialization. This caused version validation to run during generator execution, before the react-on-rails npm package was installed, resulting in validation errors. This fix restores the ENV variable approach: - Generator sets REACT_ON_RAILS_SKIP_VALIDATION=true at start - Engine checks this ENV var first (before ARGV check) - Generator clears ENV var in ensure block after completion The ENV variable persists through Rails initialization, making it more reliable than ARGV for detecting generator context. Fixes failing CI tests in examples (3.2, minimum) and examples (3.4, latest) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
WalkthroughThis pull request adds an environment variable-based mechanism to skip version validation during generator execution. The install generator sets Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
spec/react_on_rails/engine_spec.rb (1)
17-23: Consider capturing the original environment variable value.While the current implementation works, the established pattern in this codebase is to capture and restore the original value of environment variables in tests to ensure complete isolation.
Based on learnings, apply this diff:
context "when REACT_ON_RAILS_SKIP_VALIDATION is set" do + let(:original_value) { ENV["REACT_ON_RAILS_SKIP_VALIDATION"] } + before do ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true" end after do - ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION") + if original_value.nil? + ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION") + else + ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = original_value + end end
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
lib/generators/react_on_rails/install_generator.rb(2 hunks)lib/react_on_rails/engine.rb(1 hunks)spec/react_on_rails/engine_spec.rb(1 hunks)
🧰 Additional context used
🧠 Learnings (11)
📓 Common learnings
Learnt from: justin808
Repo: shakacode/react_on_rails PR: 1622
File: spec/dummy/spec/rake/assets_precompile_rake_spec.rb:12-12
Timestamp: 2024-10-08T20:53:47.076Z
Learning: When stubbing environment variables in RSpec tests, use `before` and `after` hooks to ensure that the original values are restored after the tests, preventing any side effects on other tests. Example provided by justin808:
```ruby
describe "My test" do
before do
original_value = ENV["VARIABLE_NAME"]
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_return("stubbed_value")
end
after do
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_call_original
ENV["VARIABLE_NAME"] = original_value
end
it "tests something" do
# Your test code here
end
end
```
This practice ensures test isolation and reliability.
Learnt from: justin808
Repo: shakacode/react_on_rails PR: 1622
File: spec/dummy/spec/rake/assets_precompile_rake_spec.rb:12-12
Timestamp: 2024-06-27T05:22:46.156Z
Learning: When stubbing environment variables in RSpec tests, use `before` and `after` hooks to ensure that the original values are restored after the tests, preventing any side effects on other tests. Example provided by justin808:
```ruby
describe "My test" do
before do
original_value = ENV["VARIABLE_NAME"]
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_return("stubbed_value")
end
after do
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_call_original
ENV["VARIABLE_NAME"] = original_value
end
it "tests something" do
# Your test code here
end
end
```
This practice ensures test isolation and reliability.
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1644
File: lib/react_on_rails/helper.rb:190-197
Timestamp: 2025-02-18T13:08:01.477Z
Learning: RSC support validation in React on Rails Pro is handled through a chain of validations:
1. Pro version check in `run_stream_inside_fiber`
2. RSC support check during pack generation via `ReactOnRailsPro.configuration.enable_rsc_support`
3. RSC support validation during component registration
This makes additional validation in the helper methods unnecessary.
📚 Learning: 2025-02-12T16:38:06.537Z
Learnt from: Romex91
Repo: shakacode/react_on_rails PR: 1697
File: package-scripts.yml:28-28
Timestamp: 2025-02-12T16:38:06.537Z
Learning: The file `node_package/lib/ReactOnRails.full.js` is autogenerated during the build process and should not be present in the repository.
Applied to files:
lib/generators/react_on_rails/install_generator.rb
📚 Learning: 2025-02-18T13:08:01.477Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1644
File: lib/react_on_rails/helper.rb:190-197
Timestamp: 2025-02-18T13:08:01.477Z
Learning: RSC support validation in React on Rails Pro is handled through a chain of validations:
1. Pro version check in `run_stream_inside_fiber`
2. RSC support check during pack generation via `ReactOnRailsPro.configuration.enable_rsc_support`
3. RSC support validation during component registration
This makes additional validation in the helper methods unnecessary.
Applied to files:
lib/generators/react_on_rails/install_generator.rblib/react_on_rails/engine.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2025-04-26T21:55:55.874Z
Learnt from: alexeyr-ci2
Repo: shakacode/react_on_rails PR: 1732
File: spec/dummy/client/app-react16/startup/ReduxSharedStoreApp.client.jsx:40-44
Timestamp: 2025-04-26T21:55:55.874Z
Learning: In the react_on_rails project, files under `app-react16` directories are copied/moved to corresponding `/app` directories during the conversion process (removing the `-react16` suffix), which affects their relative import paths at runtime.
Applied to files:
lib/generators/react_on_rails/install_generator.rb
📚 Learning: 2025-10-23T17:22:01.074Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1875
File: lib/react_on_rails/utils.rb:112-124
Timestamp: 2025-10-23T17:22:01.074Z
Learning: In React on Rails, when Pro is installed but not licensed, the intended behavior is to raise an error on boot. The `react_on_rails_pro?` method validates licenses and should raise errors early (including during path resolution in methods like `server_bundle?`) to enforce licensing requirements rather than failing later with obscure errors.
Applied to files:
lib/generators/react_on_rails/install_generator.rblib/react_on_rails/engine.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2024-12-12T13:07:09.929Z
Learnt from: alexeyr-ci
Repo: shakacode/react_on_rails PR: 1644
File: node_package/src/ReactOnRailsRSC.ts:87-87
Timestamp: 2024-12-12T13:07:09.929Z
Learning: When handling errors in 'node_package/src/ReactOnRailsRSC.ts', include the error stack in error messages in development and test environments to aid debugging.
Applied to files:
lib/generators/react_on_rails/install_generator.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2025-02-18T13:08:01.477Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1644
File: lib/react_on_rails/helper.rb:190-197
Timestamp: 2025-02-18T13:08:01.477Z
Learning: RSC support validation is handled in deeper level calls of the React on Rails Pro codebase, so it doesn't need to be validated again in the `rsc_payload_react_component` helper method.
Applied to files:
lib/react_on_rails/engine.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2025-02-13T16:50:26.861Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1644
File: node_package/src/turbolinksUtils.ts:34-36
Timestamp: 2025-02-13T16:50:26.861Z
Learning: In React on Rails, when checking for Turbolinks version 5 using `turbolinksVersion5()`, always ensure `Turbolinks` exists first by checking `turbolinksInstalled()` to prevent TypeError when accessing properties.
Applied to files:
lib/react_on_rails/engine.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2025-09-15T21:24:48.207Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1781
File: node_package/src/ClientSideRenderer.ts:82-95
Timestamp: 2025-09-15T21:24:48.207Z
Learning: In React on Rails, the force_load feature includes both explicit `data-force-load="true"` usage and the ability to hydrate components during the page loading state (`document.readyState === 'loading'`). Both capabilities require a Pro license, so the condition `!railsContext.rorPro && (isComponentForceLoaded || document.readyState === 'loading')` correctly gates both scenarios.
Applied to files:
lib/react_on_rails/engine.rbspec/react_on_rails/engine_spec.rb
📚 Learning: 2024-10-08T20:53:47.076Z
Learnt from: justin808
Repo: shakacode/react_on_rails PR: 1622
File: spec/dummy/spec/rake/assets_precompile_rake_spec.rb:12-12
Timestamp: 2024-10-08T20:53:47.076Z
Learning: When stubbing environment variables in RSpec tests, use `before` and `after` hooks to ensure that the original values are restored after the tests, preventing any side effects on other tests. Example provided by justin808:
```ruby
describe "My test" do
before do
original_value = ENV["VARIABLE_NAME"]
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_return("stubbed_value")
end
after do
allow(ENV).to receive(:[]).with("VARIABLE_NAME").and_call_original
ENV["VARIABLE_NAME"] = original_value
end
it "tests something" do
# Your test code here
end
end
```
This practice ensures test isolation and reliability.
Applied to files:
spec/react_on_rails/engine_spec.rb
📚 Learning: 2025-02-13T14:29:49.267Z
Learnt from: AbanoubGhadban
Repo: shakacode/react_on_rails PR: 1644
File: spec/react_on_rails/utils_spec.rb:218-218
Timestamp: 2025-02-13T14:29:49.267Z
Learning: In RSpec tests, prefer using local variables over constants within test blocks to avoid constant redefinition warnings and maintain better test isolation.
Applied to files:
spec/react_on_rails/engine_spec.rb
🧬 Code graph analysis (1)
spec/react_on_rails/engine_spec.rb (1)
lib/react_on_rails/engine.rb (1)
skip_version_validation?(22-42)
🔇 Additional comments (3)
lib/generators/react_on_rails/install_generator.rb (1)
39-63: LGTM! Clean implementation with proper cleanup.The environment variable approach correctly addresses the ARGV-based detection issues. Setting the variable at the start of
run_generatorsand clearing it in theensureblock guarantees cleanup even if exceptions occur during generator execution.spec/react_on_rails/engine_spec.rb (1)
25-33: LGTM! Tests thoroughly verify the new behavior.The tests correctly validate both the return value and the debug logging when the environment variable is set.
lib/react_on_rails/engine.rb (1)
23-27: LGTM! Correct implementation of the environment variable check.The early-exit logic correctly prioritizes the explicit environment variable override before other validation skip conditions. The debug logging provides helpful feedback for troubleshooting.
Code Review - PR #1923: Fix generator validation by using environment variableOverviewThis PR addresses failing CI tests by restoring an environment variable-based approach to skip version validation during generator runtime. The solution is well-motivated and properly implemented. ✅ Strengths
🔍 Code Quality ObservationsExcellent Practices:
Minor Suggestions:
🎯 Specific Code Review
|
Code Review: Fix generator validation by using environment variableSummaryThis PR successfully fixes failing CI tests by restoring a reliable environment variable-based approach for skipping version validation during generator runtime. The solution is well-implemented and addresses the root cause effectively. ✅ Strengths1. Root Cause Analysis
2. Code Quality
3. Test Coverage
4. Workflow Improvements
🔍 Potential Issues & Suggestions1. Thread Safety Concern (Medium Priority)Location: lib/generators/react_on_rails/install_generator.rb:42, 62 Issue: Environment variables are process-global. If multiple generators run concurrently (e.g., in parallel tests), they could interfere with each other. Potential scenario:
Suggestion: Consider using a more unique identifier or document that concurrent generator execution is not supported. However, this is likely a non-issue in practice since Rails generators typically run in a single process. 2. Test Coverage Gap (Low Priority)Location: spec/react_on_rails/engine_spec.rb Missing test: There is no test verifying that the ENV variable takes precedence over other conditions (package.json missing, ARGV detection). While the current tests are good, an integration test would be valuable. 3. Documentation (Low Priority)The PR description is excellent, but consider adding a comment in the code about why this approach was chosen over ARGV. 🛡️ Security Review
⚡ Performance Considerations
🧪 TestingWhat is tested:
What could be tested (optional):
📝 Style & ConventionsAccording to CLAUDE.md:
🎯 RecommendationsBefore Merge:
Post-Merge:
📊 Overall AssessmentRating: ⭐⭐⭐⭐½ (4.5/5) Verdict: APPROVE with minor suggestions This is a well-crafted fix that:
The concerns raised are minor and mostly about edge cases that are unlikely to occur. The core solution is sound and production-ready. Great work! 🎉 Note: Remember to run |
Addresses feedback from PR #1923: 1. Enhanced documentation: - Added comprehensive method-level docs explaining ENV variable approach - Documented why ENV is preferred over ARGV (Rails modifies ARGV during init) - Added thread safety notes explaining process-global ENV scope - Cross-referenced engine.rb and install_generator.rb for clarity 2. Improved test coverage: - Added tests verifying ENV variable takes precedence over other checks - Tests confirm ENV check short-circuits before package.json/ARGV checks - Added 4 new test cases covering precedence scenarios 3. Thread safety documentation: - Documented that generators run in single process (not a concern) - Added note about parallel test scenarios (separate processes) - Clarified ENV cleanup in ensure block Changes: - lib/react_on_rails/engine.rb: Enhanced skip_validation? docs - lib/generators/react_on_rails/install_generator.rb: Added run_generators docs - spec/react_on_rails/engine_spec.rb: Added 4 precedence tests All specs pass (21 examples, 0 failures) RuboCop clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
* Improve validation skip documentation and test coverage Addresses feedback from PR #1923: 1. Enhanced documentation: - Added comprehensive method-level docs explaining ENV variable approach - Documented why ENV is preferred over ARGV (Rails modifies ARGV during init) - Added thread safety notes explaining process-global ENV scope - Cross-referenced engine.rb and install_generator.rb for clarity 2. Improved test coverage: - Added tests verifying ENV variable takes precedence over other checks - Tests confirm ENV check short-circuits before package.json/ARGV checks - Added 4 new test cases covering precedence scenarios 3. Thread safety documentation: - Documented that generators run in single process (not a concern) - Added note about parallel test scenarios (separate processes) - Clarified ENV cleanup in ensure block Changes: - lib/react_on_rails/engine.rb: Enhanced skip_validation? docs - lib/generators/react_on_rails/install_generator.rb: Added run_generators docs - spec/react_on_rails/engine_spec.rb: Added 4 precedence tests All specs pass (21 examples, 0 failures) RuboCop clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Refine documentation and test structure per code review Addresses additional code review suggestions: 1. Edge Case Documentation: - Added @note about manual ENV variable setting - Documented use case for debugging/setup scenarios - Warned against bypassing validation in production 2. Improved Test Structure: - Refactored precedence tests to use nested contexts - Changed structure: "with other skip conditions also present" -> "when package.json exists and ARGV indicates generator" -> "when package.json is missing" - More descriptive test names focusing on behavior - Better organization showing ENV precedence clearly 3. Simplified Comments: - Reduced redundancy in install_generator.rb ensure block - Changed from 2 lines to 1 concise line - Removed redundant statement about concurrent generators Changes maintain all functionality while improving readability and addressing edge cases in documentation. All specs pass (21 examples, 0 failures) RuboCop clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
This commit addresses two critical CI issues:
1. **Fix generator validation failing in CI**:
- The rake task that generates example apps was calling `rails generate`
as a shell command, which spawned a new process without the
REACT_ON_RAILS_SKIP_VALIDATION environment variable set
- Modified shakapacker_examples.rake to prefix generator commands with
the ENV variable so validation is skipped during npm package installation
- This resolves the "No React on Rails npm package is installed" error
that was breaking the examples workflow on master
2. **Add workflow_dispatch to all CI workflows**:
- Added workflow_dispatch trigger to enable manual workflow runs on any
branch via GitHub Actions UI
- This makes CI testing more flexible and allows developers to test
changes on feature branches without needing to open a PR
- Updated workflows: main.yml, lint-js-and-ruby.yml,
rspec-package-specs.yml, package-js-tests.yml, pro-integration-tests.yml,
pro-package-tests.yml, pro-lint.yml
The generator validation fix complements the existing skip logic added in
PR #1923 which only handled direct generator invocations, not shell command
invocations.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
…1928) * Fix CI failures on master and add workflow_dispatch to all workflows This commit addresses two critical CI issues: 1. **Fix generator validation failing in CI**: - The rake task that generates example apps was calling `rails generate` as a shell command, which spawned a new process without the REACT_ON_RAILS_SKIP_VALIDATION environment variable set - Modified shakapacker_examples.rake to prefix generator commands with the ENV variable so validation is skipped during npm package installation - This resolves the "No React on Rails npm package is installed" error that was breaking the examples workflow on master 2. **Add workflow_dispatch to all CI workflows**: - Added workflow_dispatch trigger to enable manual workflow runs on any branch via GitHub Actions UI - This makes CI testing more flexible and allows developers to test changes on feature branches without needing to open a PR - Updated workflows: main.yml, lint-js-and-ruby.yml, rspec-package-specs.yml, package-js-tests.yml, pro-integration-tests.yml, pro-package-tests.yml, pro-lint.yml The generator validation fix complements the existing skip logic added in PR #1923 which only handled direct generator invocations, not shell command invocations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Add documentation clarifying ENV variable scope in rake task Added comprehensive comment explaining why we prefix shell commands with the ENV variable rather than setting it in the Ruby process: 1. Each shell command spawns a new process with its own environment 2. ENV variable is automatically scoped to each command (no cleanup needed) 3. Simpler and safer than the generator approach which requires explicit ENV cleanup This addresses potential confusion about ENV variable scope differences between the rake task approach and the generator approach in lib/generators/react_on_rails/install_generator.rb. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Summary
Fixes failing CI tests by restoring the environment variable-based approach for skipping version validation during generator runtime.
Problem
After commit a1c71ea, CI was failing with version validation errors during generator tests:
examples (3.2, minimum)- failureexamples (3.4, latest)- failureThe error occurred when running
rails generate react_on_rails:install:The ARGV-based detection (
ARGV.first.in?(%w[generate g])) was unreliable because Rails can modify or clear ARGV during the initialization process.Solution
Restored the environment variable approach (previously removed in commit c3c1ad4):
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"at the start ofrun_generatorsskip_version_validation?ensureblock to clean up after executionThe ENV variable persists through Rails initialization, making it more reliable than ARGV.
Changes
lib/generators/react_on_rails/install_generator.rb: Set/clear REACT_ON_RAILS_SKIP_VALIDATIONlib/react_on_rails/engine.rb: Check ENV variable first in skip_version_validation?spec/react_on_rails/engine_spec.rb: Added test coverage for ENV variable checkTest Plan
Related
🤖 Generated with Claude Code
This change is
Summary by CodeRabbit