[rubygems/rubygems] Fix strange error when running bundle add with frozen mode set

If Gemfile is empty and there's no lockfile (situation after `bundle init`), and
`frozen` is configured, running `bundle add` will result in an strange
error, like this:

```
$ bundle add rake
, but the lockfile can't be updated because frozen mode is set

You have deleted from the Gemfile:
* rake (~> 13.2)

Run `bundle install` elsewhere and add the updated Gemfile to version control.
```

This commit fixes the problem to instead print

https://github.com/rubygems/rubygems/commit/152331a9dc
This commit is contained in:
David Rodríguez 2024-07-08 18:02:03 +02:00 committed by git
parent 182822683f
commit 7e612b7414
5 changed files with 16 additions and 8 deletions

View File

@ -367,6 +367,10 @@ module Bundler
end
def ensure_equivalent_gemfile_and_lockfile(explicit_flag = false)
return unless Bundler.frozen_bundle?
raise ProductionError, "Frozen mode is set, but there's no lockfile" unless lockfile_exists?
added = []
deleted = []
changed = []

View File

@ -23,10 +23,7 @@ module Bundler
# @param [Pathname] lockfile_path The lockfile in which to inject the new dependency.
# @return [Array]
def inject(gemfile_path, lockfile_path)
if Bundler.frozen_bundle?
# ensure the lock and Gemfile are synced
Bundler.definition.ensure_equivalent_gemfile_and_lockfile(true)
end
# temporarily unfreeze
Bundler.settings.temporary(deployment: false, frozen: false) do

View File

@ -69,9 +69,7 @@ module Bundler
Bundler.create_bundle_path
ProcessLock.lock do
if Bundler.frozen_bundle?
@definition.ensure_equivalent_gemfile_and_lockfile(options[:deployment])
end
if @definition.dependencies.empty?
Bundler.ui.warn "The Gemfile specifies no dependencies"

View File

@ -10,7 +10,7 @@ module Bundler
end
def setup(*groups)
@definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
@definition.ensure_equivalent_gemfile_and_lockfile
# Has to happen first
clean_load_path

View File

@ -28,6 +28,15 @@ RSpec.describe "bundle add" do
end
end
context "when Gemfile is empty, and frozen mode is set" do
it "shows error" do
gemfile 'source "https://gem.repo2"'
bundle "add bar", raise_on_error: false, env: { "BUNDLE_FROZEN" => "true" }
expect(err).to include("Frozen mode is set, but there's no lockfile")
end
end
describe "without version specified" do
it "version requirement becomes ~> major.minor.patch when resolved version is < 1.0" do
bundle "add 'bar'"