Skip to content

Commit a04a12f

Browse files
justin808claude
andcommitted
Merge origin/master into shakapacker-9.3.0 branch
Resolved conflict in lib/generators/react_on_rails/install_generator.rb by adopting master's improved comments that provide better documentation about: - ENV variable persistence through Rails initialization - Reference to engine.rb for validation skip logic - Cleanup guarantees even on generator failure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
2 parents f222525 + 847edcd commit a04a12f

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

.github/workflows/examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
paths-ignore:
1212
- '**.md'
1313
- 'docs/**'
14+
workflow_dispatch:
1415

1516
jobs:
1617
detect-changes:

.github/workflows/package-js-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515
- 'docs/**'
1616
- 'lib/**'
1717
- 'spec/react_on_rails/**'
18+
workflow_dispatch:
1819

1920
jobs:
2021
detect-changes:

lib/generators/react_on_rails/install_generator.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,23 @@ class InstallGenerator < Rails::Generators::Base
3636

3737
# Removed: --skip-shakapacker-install (Shakapacker is now a required dependency)
3838

39+
# Main generator entry point
40+
#
41+
# Sets up React on Rails in a Rails application by:
42+
# 1. Validating prerequisites
43+
# 2. Installing required packages
44+
# 3. Generating configuration files
45+
# 4. Setting up example components
46+
#
47+
# @note Validation Skipping: Sets ENV["REACT_ON_RAILS_SKIP_VALIDATION"] to prevent
48+
# version validation from running during generator execution. The npm package
49+
# isn't installed until midway through the generator, so validation would fail
50+
# if run during Rails initialization. The ensure block guarantees cleanup even
51+
# if the generator fails.
3952
def run_generators
4053
# Set environment variable to skip validation during generator run
41-
# This is inherited by all invoked generators
54+
# This is inherited by all invoked generators and persists through Rails initialization
55+
# See lib/react_on_rails/engine.rb for the validation skip logic
4256
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
4357

4458
if installation_prerequisites_met? || options.ignore_warnings?
@@ -59,6 +73,7 @@ def run_generators
5973
GeneratorMessages.add_error(error)
6074
end
6175
ensure
76+
# Always clean up ENV variable, even if generator fails
6277
ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION")
6378
print_generator_messages
6479
end

lib/react_on_rails/engine.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,41 @@ class Engine < ::Rails::Engine
1818
end
1919

2020
# Determine if version validation should be skipped
21+
#
22+
# This method checks multiple conditions to determine if package version validation
23+
# should be skipped. Validation is skipped during setup scenarios where the npm
24+
# package isn't installed yet (e.g., during generator execution).
25+
#
2126
# @return [Boolean] true if validation should be skipped
27+
#
28+
# @note Thread Safety: ENV variables are process-global. In practice, Rails generators
29+
# run in a single process, so concurrent execution is not a concern. If running
30+
# generators concurrently (e.g., in parallel tests), ensure tests run in separate
31+
# processes to avoid ENV variable conflicts.
32+
#
33+
# @note Manual ENV Setting: While this ENV variable is designed to be set by generators,
34+
# users can manually set it (e.g., `REACT_ON_RAILS_SKIP_VALIDATION=true rails server`)
35+
# to bypass validation. This should only be done temporarily during debugging or
36+
# setup scenarios. The validation helps catch version mismatches early, so bypassing
37+
# it in production is not recommended.
2238
def self.skip_version_validation?
39+
# Skip if explicitly disabled via environment variable (set by generators)
40+
# Using ENV variable instead of ARGV because Rails can modify/clear ARGV during
41+
# initialization, making ARGV unreliable for detecting generator context. The ENV
42+
# variable persists through the entire Rails initialization process.
43+
if ENV["REACT_ON_RAILS_SKIP_VALIDATION"] == "true"
44+
Rails.logger.debug "[React on Rails] Skipping validation - disabled via environment variable"
45+
return true
46+
end
47+
2348
# Check package.json first as it's cheaper and handles more cases
2449
if package_json_missing?
2550
Rails.logger.debug "[React on Rails] Skipping validation - package.json not found"
2651
return true
2752
end
2853

2954
# Skip during generator runtime since packages are installed during execution
55+
# This is a fallback check in case ENV wasn't set, though ENV is the primary mechanism
3056
if running_generator?
3157
Rails.logger.debug "[React on Rails] Skipping validation during generator runtime"
3258
return true

spec/react_on_rails/engine_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,65 @@ module ReactOnRails
1313
allow(Rails.logger).to receive(:debug)
1414
end
1515

16+
context "when REACT_ON_RAILS_SKIP_VALIDATION is set" do
17+
before do
18+
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
19+
end
20+
21+
after do
22+
ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION")
23+
end
24+
25+
it "returns true" do
26+
expect(described_class.skip_version_validation?).to be true
27+
end
28+
29+
it "logs debug message about environment variable" do
30+
described_class.skip_version_validation?
31+
expect(Rails.logger).to have_received(:debug)
32+
.with("[React on Rails] Skipping validation - disabled via environment variable")
33+
end
34+
35+
context "with other skip conditions also present" do
36+
context "when package.json exists and ARGV indicates generator" do
37+
before do
38+
allow(File).to receive(:exist?).with(package_json_path).and_return(true)
39+
stub_const("ARGV", ["generate", "react_on_rails:install"])
40+
end
41+
42+
it "prioritizes ENV over ARGV check" do
43+
expect(described_class.skip_version_validation?).to be true
44+
end
45+
46+
it "short-circuits before checking ARGV" do
47+
described_class.skip_version_validation?
48+
expect(Rails.logger).to have_received(:debug)
49+
.with("[React on Rails] Skipping validation - disabled via environment variable")
50+
expect(Rails.logger).not_to have_received(:debug)
51+
.with("[React on Rails] Skipping validation during generator runtime")
52+
end
53+
end
54+
55+
context "when package.json is missing" do
56+
before do
57+
allow(File).to receive(:exist?).with(package_json_path).and_return(false)
58+
end
59+
60+
it "prioritizes ENV over package.json check" do
61+
expect(described_class.skip_version_validation?).to be true
62+
end
63+
64+
it "short-circuits before checking package.json" do
65+
described_class.skip_version_validation?
66+
expect(Rails.logger).to have_received(:debug)
67+
.with("[React on Rails] Skipping validation - disabled via environment variable")
68+
expect(Rails.logger).not_to have_received(:debug)
69+
.with("[React on Rails] Skipping validation - package.json not found")
70+
end
71+
end
72+
end
73+
end
74+
1675
context "when package.json doesn't exist" do
1776
before do
1877
allow(File).to receive(:exist?).with(package_json_path).and_return(false)

0 commit comments

Comments
 (0)