Skip to content

Commit be4ff99

Browse files
author
Martin Sander
committed
Read package.json from Rails.root, not cwd
Fixes #519.
1 parent 9544d55 commit be4ff99

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Changes since the last non-beta release.
1313
### Changed
1414
- Changed internal `require`s to `require_relative` to make code less dependent on the load path. [PR 516](https://github.com/shakacode/shakapacker/pull/516) by [tagliala](https://github.com/tagliala).
1515

16+
### Fixed
17+
- Fix error when rails environment is required from outside the rails root directory [PR 520](https://github.com/shakacode/shakapacker/pull/520)
18+
1619
## [v8.0.2] - August 28, 2024
1720

1821
### Fixed

lib/shakapacker/utils/manager.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Error < StandardError; end
1616

1717
# Emits a warning if it's not obvious what package manager to use
1818
def self.error_unless_package_manager_is_obvious!
19-
return unless PackageJson.read.fetch("packageManager", nil).nil?
19+
return unless PackageJson.read(rails_root).fetch("packageManager", nil).nil?
2020

2121
guessed_binary = guess_binary
2222

@@ -35,7 +35,7 @@ def self.error_unless_package_manager_is_obvious!
3535
#
3636
# @return [String]
3737
def self.guess_binary
38-
MANAGER_LOCKS.find { |_, lock| File.exist?(lock) }&.first || "npm"
38+
MANAGER_LOCKS.find { |_, lock| File.exist?(rails_root.join(lock)) }&.first || "npm"
3939
end
4040

4141
# Guesses the version of the package manager to use by calling `<manager> --version`
@@ -53,6 +53,17 @@ def self.guess_version
5353

5454
stdout.chomp
5555
end
56+
57+
private
58+
def self.rails_root
59+
if defined?(APP_ROOT)
60+
Pathname.new(APP_ROOT)
61+
elsif defined?(Rails)
62+
Rails.root
63+
else
64+
raise "can only be called from a rails environment or with APP_ROOT defined"
65+
end
66+
end
5667
end
5768
end
5869
end

spec/shakapacker/utils_manager_spec.rb

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def exitstatus
4848
context "when there is a #{lock}" do
4949
before do
5050
allow(Open3).to receive(:capture3).and_return(["1.2.3\n", "", Struct::Status.new(0)])
51+
allow(Rails).to receive(:root).and_return(Pathname.new("."))
5152
end
5253

5354
it "raises an error about setting 'packageManager' for #{manager}" do
@@ -63,10 +64,35 @@ def exitstatus
6364
MSG
6465
end
6566
end
67+
68+
context "when lockfile is in Rails.root, but pwd is different" do
69+
before do
70+
allow(Open3).to receive(:capture3).and_return(["1.2.3\n", "", Struct::Status.new(0)])
71+
end
72+
73+
it "raises an error about setting 'packageManager' for #{manager}" do
74+
rails_root = Pathname.new("rails_root_#{lock}")
75+
FileUtils.mkdir_p(rails_root)
76+
allow(Rails).to receive(:root).and_return(rails_root)
77+
78+
File.write(rails_root.join("package.json"), {}.to_json)
79+
FileUtils.touch(rails_root.join(lock))
80+
81+
expect { Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious! }.to raise_error(Shakapacker::Utils::Manager::Error, <<~MSG)
82+
You don't have "packageManager" set in your package.json
83+
meaning that Shakapacker will use npm but you've got a #{lock}
84+
file meaning you probably want to be using #{manager} instead.
85+
86+
To make this happen, set "packageManager" in your package.json to #{manager}@1.2.3
87+
MSG
88+
end
89+
end
6690
end
6791
end
6892

6993
describe "~guess_binary" do
94+
before { allow(Rails).to receive(:root).and_return(Pathname.new(".")) }
95+
7096
Shakapacker::Utils::Manager::MANAGER_LOCKS.each do |manager, lock|
7197
context "when a #{lock} exists" do
7298
before { FileUtils.touch(lock) }
@@ -75,6 +101,19 @@ def exitstatus
75101
expect(Shakapacker::Utils::Manager.guess_binary).to eq manager
76102
end
77103
end
104+
105+
context "when lockfile is in Rails.root, but pwd is different" do
106+
before do
107+
rails_root = Pathname.new("rails_root_#{lock}")
108+
FileUtils.mkdir_p(rails_root)
109+
FileUtils.touch(rails_root.join(lock))
110+
allow(Rails).to receive(:root).and_return(rails_root)
111+
end
112+
113+
it "guesses #{manager}" do
114+
expect(Shakapacker::Utils::Manager.guess_binary).to eq manager
115+
end
116+
end
78117
end
79118

80119
context "when there is no lockfile" do
@@ -91,7 +130,25 @@ def exitstatus
91130

92131
Shakapacker::Utils::Manager::MANAGER_LOCKS.each do |manager, lock|
93132
context "when a #{lock} exists" do
94-
before { FileUtils.touch(lock) }
133+
before do
134+
FileUtils.touch(lock)
135+
allow(Rails).to receive(:root).and_return(Pathname.new("."))
136+
end
137+
138+
it "calls #{manager} with --version" do
139+
Shakapacker::Utils::Manager.guess_version
140+
141+
expect(Open3).to have_received(:capture3).with("#{manager} --version")
142+
end
143+
end
144+
145+
context "when lockfile is in Rails.root, but pwd is different" do
146+
before do
147+
rails_root = Pathname.new("rails_root_#{lock}")
148+
FileUtils.mkdir_p(rails_root)
149+
FileUtils.touch(rails_root.join(lock))
150+
allow(Rails).to receive(:root).and_return(rails_root)
151+
end
95152

96153
it "calls #{manager} with --version" do
97154
Shakapacker::Utils::Manager.guess_version
@@ -110,6 +167,7 @@ def exitstatus
110167
context "when the command errors" do
111168
before do
112169
allow(Open3).to receive(:capture3).and_return(["", "oh noes!", Struct::Status.new(1)])
170+
allow(Rails).to receive(:root).and_return(Pathname.new("."))
113171
end
114172

115173
it "raises an error" do

0 commit comments

Comments
 (0)