Skip to content

Commit 4a90b8f

Browse files
justin808claude
andcommitted
Fix RuboCop directives and improve path normalization robustness
- Fix doctor.rb RuboCop directive consistency: disable and enable now match - Improve normalize_to_relative_path to prevent false matches with substring paths - Normalize Rails.root by removing trailing slash for consistent comparisons - Add Metrics/CyclomaticComplexity disable for normalize_to_relative_path 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2c08975 commit 4a90b8f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/react_on_rails/doctor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ def analyze_server_rendering_config(content)
716716

717717
checker.add_info(" raise_on_prerender_error: #{raise_on_error_match[1]}")
718718
end
719-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
719+
# rubocop:enable Metrics/CyclomaticComplexity
720720

721-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
721+
# rubocop:disable Metrics/CyclomaticComplexity
722722
def analyze_performance_config(content)
723723
checker.add_info("\n⚡ Performance & Loading:")
724724

lib/react_on_rails/utils.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,20 +467,25 @@ def self.package_manager_remove_command(package_name)
467467
#
468468
# @example Absolute paths outside Rails.root (edge case)
469469
# normalize_to_relative_path("/other/path/bundles") # => "/other/path/bundles"
470+
# rubocop:disable Metrics/CyclomaticComplexity
470471
def self.normalize_to_relative_path(path)
471472
return nil if path.nil?
472473

473474
path_str = path.to_s
474-
rails_root_str = Rails.root.to_s
475+
rails_root_str = Rails.root.to_s.chomp("/")
475476

476-
# If path starts with Rails.root, remove that prefix
477-
if path_str.start_with?(rails_root_str)
477+
# Treat as "inside Rails.root" only for exact match or a subdirectory
478+
inside_rails_root = rails_root_str.present? &&
479+
(path_str == rails_root_str || path_str.start_with?("#{rails_root_str}/"))
480+
481+
# If path is within Rails.root, remove that prefix
482+
if inside_rails_root
478483
# Remove Rails.root and any leading slash
479484
path_str.sub(%r{^#{Regexp.escape(rails_root_str)}/?}, "")
480485
else
481-
# Path is already relative or doesn't contain Rails.root
486+
# Path is already relative or outside Rails.root
482487
# Warn if it's an absolute path outside Rails.root (edge case)
483-
if path_str.start_with?("/") && !path_str.start_with?(rails_root_str)
488+
if path_str.start_with?("/") && !inside_rails_root
484489
Rails.logger&.warn(
485490
"ReactOnRails: Detected absolute path outside Rails.root: '#{path_str}'. " \
486491
"Server bundles are typically stored within Rails.root. " \
@@ -490,6 +495,7 @@ def self.normalize_to_relative_path(path)
490495
path_str
491496
end
492497
end
498+
# rubocop:enable Metrics/CyclomaticComplexity
493499

494500
def self.default_troubleshooting_section
495501
<<~DEFAULT

0 commit comments

Comments
 (0)