Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Track Bundler version in lockfile #3485

Closed
wants to merge 13 commits into from
15 changes: 15 additions & 0 deletions lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def initialize(lockfile, dependencies, sources, unlock, ruby_version = nil)
@lockfile_contents = Bundler.read_file(lockfile)
locked = LockfileParser.new(@lockfile_contents)
@platforms = locked.platforms
@locked_bundler_version = locked.bundler_version

if unlock != true
@locked_deps = locked.dependencies
Expand Down Expand Up @@ -256,6 +257,16 @@ def lock(file)
"#{File.expand_path(file)}"
end

# Returns the version of Bundler that is creating or has created
# Gemfile.lock. Used in #to_lock.
def lock_version
if @locked_bundler_version && @locked_bundler_version < Gem::Version.new(Bundler::VERSION)
new_version = Bundler::VERSION
end

new_version || @locked_bundler_version || Bundler::VERSION
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I kept mulling this over, and I think I see some ways to cut down on the amount of code that exists without making it (much?) more obscure:

lock_ver = @lockfile_contents[/^  \[(.*)\]$/, 1] if lockfile

if lock_ver && Gem::Version.new(lock_ver) < Gem::Version.new(Bundler::VERSION)
  new_ver = Bundler::VERSION
end

new_ver || lock_ver || Bundler::VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that part is a bit verbose. Thanks!


def to_lock
out = ""

Expand Down Expand Up @@ -294,6 +305,10 @@ def to_lock
handled << dep.name
end

# Record the version of Bundler that was used to create the lockfile
out << "\nBUNDLED WITH\n"
out << " #{lock_version}\n"

out
end

Expand Down
24 changes: 23 additions & 1 deletion lib/bundler/lockfile_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

module Bundler
class LockfileParser
attr_reader :sources, :dependencies, :specs, :platforms
attr_reader :sources, :dependencies, :specs, :platforms, :bundler_version

BUNDLED = "BUNDLED WITH"
DEPENDENCIES = "DEPENDENCIES"
PLATFORMS = "PLATFORMS"
GIT = "GIT"
Expand Down Expand Up @@ -41,18 +42,32 @@ def initialize(lockfile)
@state = :dependency
elsif line == PLATFORMS
@state = :platform
elsif line == BUNDLED
@state = :bundled_with
else
send("parse_#{@state}", line)
end
end
@sources << @rubygems_aggregate
@specs = @specs.values
warn_for_outdated_bundler_version
rescue ArgumentError => e
Bundler.ui.debug(e)
raise LockfileError, "Your lockfile is unreadable. Run `rm Gemfile.lock` " \
"and then `bundle install` to generate a new lockfile."
end

def warn_for_outdated_bundler_version
return unless bundler_version
prerelease_text = bundler_version.prerelease? ? " --pre" : ""
if Gem::Version.new(Bundler::VERSION) < Gem::Version.new(bundler_version)
Bundler.ui.warn "Warning: the running version of Bundler is older " \
"than the version that created the lockfile. We suggest you " \
"upgrade to the latest version of Bundler by running `gem " \
"install bundler#{prerelease_text}`.\n"
end
end

private

TYPES = {
Expand Down Expand Up @@ -157,5 +172,12 @@ def parse_platform(line)
end
end

def parse_bundled_with(line)
line = line.strip
if Gem::Version.correct?(line)
@bundler_version = Gem::Version.create(line)
end
end

end
end
3 changes: 3 additions & 0 deletions spec/commands/lock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def read_lockfile(file = "Gemfile.lock")
foo
rails
with_license

BUNDLED WITH
#{Bundler::VERSION}
L
end

Expand Down
3 changes: 3 additions & 0 deletions spec/install/gems/flex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@

DEPENDENCIES
rack

BUNDLED WITH
#{Bundler::VERSION}
L
end
end
Expand Down
Loading