Skip to content

Commit 0993b48

Browse files
justin808claude
andcommitted
Fix precompile hook to properly locate Rails root
Add find_rails_root helper that walks upward from current directory looking for config/environment.rb to locate the Rails root, instead of assuming the current working directory is the Rails root. Changes: - Add find_rails_root() method that searches parent directories - Returns nil if Rails root cannot be found (reaches filesystem root) - Update generate_packs_if_needed to use Rails root for all paths - Build absolute path to config/initializers/react_on_rails.rb Benefits: - Works regardless of current working directory - More robust when hook is called from subdirectories - Gracefully handles non-Rails environments 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7f902cf commit 0993b48

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

spec/dummy/bin/shakapacker-precompile-hook

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
require "fileutils"
99

10+
# Find Rails root by walking upward looking for config/environment.rb
11+
def find_rails_root
12+
dir = Dir.pwd
13+
loop do
14+
return dir if File.exist?(File.join(dir, "config", "environment.rb"))
15+
16+
parent = File.dirname(dir)
17+
return nil if parent == dir # Reached filesystem root
18+
19+
dir = parent
20+
end
21+
end
22+
1023
# Build ReScript if needed
1124
def build_rescript_if_needed
1225
# Check for both old (bsconfig.json) and new (rescript.json) config files
@@ -36,11 +49,18 @@ def build_rescript_if_needed
3649
end
3750

3851
# Generate React on Rails packs if needed
52+
# rubocop:disable Metrics/CyclomaticComplexity
3953
def generate_packs_if_needed
40-
return unless File.exist?("config/initializers/react_on_rails.rb")
54+
# Find Rails root directory
55+
rails_root = find_rails_root
56+
return unless rails_root
57+
58+
# Check if React on Rails initializer exists
59+
initializer_path = File.join(rails_root, "config", "initializers", "react_on_rails.rb")
60+
return unless File.exist?(initializer_path)
4161

4262
# Check if auto-pack generation is configured (match actual config assignments, not comments)
43-
config_file = File.read("config/initializers/react_on_rails.rb")
63+
config_file = File.read(initializer_path)
4464
has_auto_load = config_file =~ /^\s*config\.auto_load_bundle\s*=/
4565
has_components_subdir = config_file =~ /^\s*config\.components_subdirectory\s*=/
4666
return unless has_auto_load || has_components_subdir
@@ -65,6 +85,7 @@ def generate_packs_if_needed
6585
exit 1
6686
end
6787
end
88+
# rubocop:enable Metrics/CyclomaticComplexity
6889

6990
# Main execution
7091
begin

0 commit comments

Comments
 (0)