Skip to content

Commit 54ef182

Browse files
justin808claude
andcommitted
Fix server bundle path duplication by handling nested absolute paths
- Handle case where both source_path and source_entry_path are absolute - Extract relative entry path when source_entry_path contains source_path - Remove debug logging since issue is resolved - Add test case for nested absolute path scenario This fixes the issue where paths like: /app/client/app + /app/client/app/packs → client/app/packs (correct) instead of: client/app/app/client/app/packs (incorrect) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bea93f1 commit 54ef182

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

lib/react_on_rails/doctor.rb

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,32 +312,26 @@ def determine_server_bundle_path
312312
source_path = Shakapacker.config.source_path.to_s
313313
source_entry_path = Shakapacker.config.source_entry_path.to_s
314314
server_bundle_filename = get_server_bundle_filename
315+
rails_root = Dir.pwd
315316

316-
# Debug info - remove after fixing
317-
checker.add_info("🔍 Debug - Raw source_path: #{source_path}")
318-
checker.add_info("🔍 Debug - Raw source_entry_path: #{source_entry_path}")
319-
checker.add_info("🔍 Debug - Rails root (Dir.pwd): #{Dir.pwd}")
320-
321-
# If source_path is absolute, make it relative to current directory
322-
if source_path.start_with?("/")
323-
# Convert absolute path to relative by removing the Rails root
324-
rails_root = Dir.pwd
325-
if source_path.start_with?(rails_root)
326-
source_path = source_path.sub("#{rails_root}/", "")
327-
checker.add_info("🔍 Debug - Converted to relative: #{source_path}")
328-
else
329-
# If it's not under Rails root, just use the basename
330-
source_path = File.basename(source_path)
331-
checker.add_info("🔍 Debug - Using basename: #{source_path}")
332-
end
317+
# Convert absolute paths to relative paths
318+
if source_path.start_with?("/") && source_path.start_with?(rails_root)
319+
source_path = source_path.sub("#{rails_root}/", "")
333320
end
334321

335-
final_path = File.join(source_path, source_entry_path, server_bundle_filename)
336-
checker.add_info("🔍 Debug - Final path: #{final_path}")
337-
final_path
338-
rescue LoadError, NameError, StandardError => e
322+
if source_entry_path.start_with?("/") && source_entry_path.start_with?(rails_root)
323+
source_entry_path = source_entry_path.sub("#{rails_root}/", "")
324+
end
325+
326+
# If source_entry_path is already within source_path, just use the relative part
327+
if source_entry_path.start_with?(source_path)
328+
# Extract just the entry path part (e.g., "packs" from "client/app/packs")
329+
source_entry_path = source_entry_path.sub("#{source_path}/", "")
330+
end
331+
332+
File.join(source_path, source_entry_path, server_bundle_filename)
333+
rescue LoadError, NameError, StandardError
339334
# Fallback to default paths if Shakapacker is not available or configured
340-
checker.add_info("🔍 Debug - Shakapacker error: #{e.message}")
341335
server_bundle_filename = get_server_bundle_filename
342336
"app/javascript/packs/#{server_bundle_filename}"
343337
end

spec/lib/react_on_rails/doctor_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@
118118
end
119119
end
120120

121+
context "when Shakapacker gem returns nested absolute paths" do
122+
let(:rails_root) { "/Users/test/myapp" }
123+
let(:shakapacker_config) { double(source_path: "#{rails_root}/client/app", source_entry_path: "#{rails_root}/client/app/packs") }
124+
125+
before do
126+
shakapacker_module = double("Shakapacker", config: shakapacker_config)
127+
stub_const("Shakapacker", shakapacker_module)
128+
allow(doctor).to receive(:require).with("shakapacker").and_return(true)
129+
allow(doctor).to receive(:get_server_bundle_filename).and_return("server-bundle.js")
130+
allow(Dir).to receive(:pwd).and_return(rails_root)
131+
end
132+
133+
it "handles nested absolute paths correctly" do
134+
path = doctor.send(:determine_server_bundle_path)
135+
expect(path).to eq("client/app/packs/server-bundle.js")
136+
end
137+
end
138+
121139
context "when Shakapacker gem is not available" do
122140
before do
123141
allow(doctor).to receive(:require).with("shakapacker").and_raise(LoadError)

0 commit comments

Comments
 (0)