diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb index cb644a7f69..a738992068 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -86,7 +86,7 @@ module Bundler segments = version.segments seg_end_index = version >= Gem::Version.new("1.0") ? 1 : 2 - prerelease_suffix = version.to_s.gsub(version.release.to_s, "") if version.prerelease? + prerelease_suffix = version.to_s.delete_prefix(version.release.to_s) if version.prerelease? "#{version_prefix}#{segments[0..seg_end_index].join(".")}#{prerelease_suffix}" end diff --git a/spec/bundler/commands/add_spec.rb b/spec/bundler/commands/add_spec.rb index 1eaff913c3..9398bfb3d1 100644 --- a/spec/bundler/commands/add_spec.rb +++ b/spec/bundler/commands/add_spec.rb @@ -9,6 +9,7 @@ RSpec.describe "bundle add" do build_gem "bar", "0.12.3" build_gem "cat", "0.12.3.pre" build_gem "dog", "1.1.3.pre" + build_gem "lemur", "3.1.1.pre.2023.1.1" end build_git "foo", "2.0" @@ -51,6 +52,13 @@ RSpec.describe "bundle add" do expect(bundled_app_gemfile.read).to match(/gem "dog", "~> 1.1.pre"/) expect(the_bundle).to include_gems "dog 1.1.3.pre" end + + it "version requirement becomes ~> major.minor.pre.tail when resolved version has a very long tail pre version" do + bundle "add 'lemur'" + # the trailing pre purposely matches the release version to ensure that subbing the release doesn't change the pre.version" + expect(bundled_app_gemfile.read).to match(/gem "lemur", "~> 3.1.pre.2023.1.1"/) + expect(the_bundle).to include_gems "lemur 3.1.1.pre.2023.1.1" + end end describe "with --version" do