Skip to content

Commit

Permalink
Read package.json from Rails.root, not cwd
Browse files Browse the repository at this point in the history
Fixes #519.
  • Loading branch information
Martin Sander committed Oct 9, 2024
1 parent 9544d55 commit 4d276ca
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Changes since the last non-beta release.
### Changed
- 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).

### Fixed
- Fix error when rails environment is required from outside the rails root directory [PR 520](https://github.com/shakacode/shakapacker/pull/520)

## [v8.0.2] - August 28, 2024

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions lib/shakapacker/utils/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Error < StandardError; end

# Emits a warning if it's not obvious what package manager to use
def self.error_unless_package_manager_is_obvious!
return unless PackageJson.read.fetch("packageManager", nil).nil?
return unless PackageJson.read(Rails.root).fetch("packageManager", nil).nil?

guessed_binary = guess_binary

Expand All @@ -35,7 +35,7 @@ def self.error_unless_package_manager_is_obvious!
#
# @return [String]
def self.guess_binary
MANAGER_LOCKS.find { |_, lock| File.exist?(lock) }&.first || "npm"
MANAGER_LOCKS.find { |_, lock| File.exist?(Rails.root.join(lock)) }&.first || "npm"
end

# Guesses the version of the package manager to use by calling `<manager> --version`
Expand Down
60 changes: 59 additions & 1 deletion spec/shakapacker/utils_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def exitstatus
context "when there is a #{lock}" do
before do
allow(Open3).to receive(:capture3).and_return(["1.2.3\n", "", Struct::Status.new(0)])
allow(Rails).to receive(:root).and_return(Pathname.new("."))
end

it "raises an error about setting 'packageManager' for #{manager}" do
Expand All @@ -63,10 +64,35 @@ def exitstatus
MSG
end
end

context "when lockfile is in Rails.root, but pwd is different" do
before do
allow(Open3).to receive(:capture3).and_return(["1.2.3\n", "", Struct::Status.new(0)])
end

it "raises an error about setting 'packageManager' for #{manager}" do
rails_root = Pathname.new("rails_root_#{lock}")
FileUtils.mkdir_p(rails_root)
allow(Rails).to receive(:root).and_return(rails_root)

File.write(rails_root.join("package.json"), {}.to_json)
FileUtils.touch(rails_root.join(lock))

expect { Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious! }.to raise_error(Shakapacker::Utils::Manager::Error, <<~MSG)
You don't have "packageManager" set in your package.json
meaning that Shakapacker will use npm but you've got a #{lock}
file meaning you probably want to be using #{manager} instead.
To make this happen, set "packageManager" in your package.json to #{manager}@1.2.3
MSG
end
end
end
end

describe "~guess_binary" do
before { allow(Rails).to receive(:root).and_return(Pathname.new(".")) }

Shakapacker::Utils::Manager::MANAGER_LOCKS.each do |manager, lock|
context "when a #{lock} exists" do
before { FileUtils.touch(lock) }
Expand All @@ -75,6 +101,19 @@ def exitstatus
expect(Shakapacker::Utils::Manager.guess_binary).to eq manager
end
end

context "when lockfile is in Rails.root, but pwd is different" do
before do
rails_root = Pathname.new("rails_root_#{lock}")
FileUtils.mkdir_p(rails_root)
FileUtils.touch(rails_root.join(lock))
allow(Rails).to receive(:root).and_return(rails_root)
end

it "guesses #{manager}" do
expect(Shakapacker::Utils::Manager.guess_binary).to eq manager
end
end
end

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

Shakapacker::Utils::Manager::MANAGER_LOCKS.each do |manager, lock|
context "when a #{lock} exists" do
before { FileUtils.touch(lock) }
before do
FileUtils.touch(lock)
allow(Rails).to receive(:root).and_return(Pathname.new("."))
end

it "calls #{manager} with --version" do
Shakapacker::Utils::Manager.guess_version

expect(Open3).to have_received(:capture3).with("#{manager} --version")
end
end

context "when lockfile is in Rails.root, but pwd is different" do
before do
rails_root = Pathname.new("rails_root_#{lock}")
FileUtils.mkdir_p(rails_root)
FileUtils.touch(rails_root.join(lock))
allow(Rails).to receive(:root).and_return(rails_root)
end

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

it "raises an error" do
Expand Down

0 comments on commit 4d276ca

Please sign in to comment.