From 7e612b7414280c49ccc633cdf392f61e7acf2970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 8 Jul 2024 18:02:03 +0200 Subject: [PATCH] [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 --- lib/bundler/definition.rb | 4 ++++ lib/bundler/injector.rb | 5 +---- lib/bundler/installer.rb | 4 +--- lib/bundler/runtime.rb | 2 +- spec/bundler/commands/add_spec.rb | 9 +++++++++ 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index da8d374627..63033b9065 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -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 = [] diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb index 879b481339..c7e93c9ee0 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -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 + Bundler.definition.ensure_equivalent_gemfile_and_lockfile(true) # temporarily unfreeze Bundler.settings.temporary(deployment: false, frozen: false) do diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb index 256f0be348..485782d1b4 100644 --- a/lib/bundler/installer.rb +++ b/lib/bundler/installer.rb @@ -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 + @definition.ensure_equivalent_gemfile_and_lockfile(options[:deployment]) if @definition.dependencies.empty? Bundler.ui.warn "The Gemfile specifies no dependencies" diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb index 54aa30ce0b..4b2c54d0b6 100644 --- a/lib/bundler/runtime.rb +++ b/lib/bundler/runtime.rb @@ -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 diff --git a/spec/bundler/commands/add_spec.rb b/spec/bundler/commands/add_spec.rb index f549e4f8bc..9eb9c876ca 100644 --- a/spec/bundler/commands/add_spec.rb +++ b/spec/bundler/commands/add_spec.rb @@ -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'"