Skip to content

Commit ae5425b

Browse files
justin808claude
andauthored
Fix generator validation by using environment variable (#1923)
* Fix generator validation by using environment variable 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 <noreply@anthropic.com> * Add workflow_dispatch trigger to enable manual workflow runs --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent a1c71ea commit ae5425b

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class InstallGenerator < Rails::Generators::Base
3737
# Removed: --skip-shakapacker-install (Shakapacker is now a required dependency)
3838

3939
def run_generators
40+
# Set environment variable to skip validation during generator run
41+
# This is inherited by all invoked generators and persists through Rails initialization
42+
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
43+
4044
if installation_prerequisites_met? || options.ignore_warnings?
4145
invoke_generators
4246
add_bin_scripts
@@ -55,6 +59,7 @@ def run_generators
5559
GeneratorMessages.add_error(error)
5660
end
5761
ensure
62+
ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION")
5863
print_generator_messages
5964
end
6065

lib/react_on_rails/engine.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Engine < ::Rails::Engine
2020
# Determine if version validation should be skipped
2121
# @return [Boolean] true if validation should be skipped
2222
def self.skip_version_validation?
23+
# Skip if explicitly disabled via environment variable (set by generators)
24+
if ENV["REACT_ON_RAILS_SKIP_VALIDATION"] == "true"
25+
Rails.logger.debug "[React on Rails] Skipping validation - disabled via environment variable"
26+
return true
27+
end
28+
2329
# Check package.json first as it's cheaper and handles more cases
2430
if package_json_missing?
2531
Rails.logger.debug "[React on Rails] Skipping validation - package.json not found"

spec/react_on_rails/engine_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ 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+
end
35+
1636
context "when package.json doesn't exist" do
1737
before do
1838
allow(File).to receive(:exist?).with(package_json_path).and_return(false)

0 commit comments

Comments
 (0)