Skip to content

Commit db85e69

Browse files
committed
Merge pull request #9669 from ruby/claude/recursing-shamir-018b5d
Preserve CRLF lockfile line endings on Windows (cherry picked from commit 0d35496)
1 parent a52ed2c commit db85e69

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

bundler/lib/bundler/definition.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,6 @@ def write_lock(file, preserve_unknown_sections)
397397

398398
contents = to_lock
399399

400-
# Convert to \r\n if the existing lock has them
401-
# i.e., Windows with `git config core.autocrlf=true`
402-
contents.gsub!(/\n/, "\r\n") if @lockfile_contents.match?("\r\n")
403-
404400
if @locked_bundler_version
405401
locked_major = @locked_bundler_version.segments.first
406402
current_major = bundler_version_to_lock.segments.first
@@ -421,6 +417,14 @@ def write_lock(file, preserve_unknown_sections)
421417
return
422418
end
423419

420+
# Convert to \r\n if the existing lock has them, i.e., Windows with
421+
# `git config core.autocrlf=true`. Detect from the bytes on disk because
422+
# reading in text mode strips carriage returns on Windows, which would
423+
# otherwise defeat this check and rewrite a `\r\n` lockfile with `\n`.
424+
if File.exist?(file) && SharedHelpers.filesystem_access(file, :read) {|p| File.binread(p).include?("\r\n") }
425+
contents.gsub!(/\n/, "\r\n")
426+
end
427+
424428
begin
425429
SharedHelpers.filesystem_access(file) do |p|
426430
File.open(p, "wb") {|f| f.puts(contents) }

bundler/spec/lock/lockfile_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,9 +2332,7 @@ def set_lockfile_mtime_to_known_value
23322332
expect(the_bundle).to include_gems "myrack 1.2"
23332333
end
23342334

2335-
it "preserves Gemfile.lock \\n\\r line endings" do
2336-
skip "needs to be adapted" if Gem.win_platform?
2337-
2335+
it "preserves Gemfile.lock \\r\\n line endings" do
23382336
update_repo2 do
23392337
build_gem "myrack", "1.2" do |s|
23402338
s.executables = "myrackup"
@@ -2346,7 +2344,7 @@ def set_lockfile_mtime_to_known_value
23462344
set_lockfile_mtime_to_known_value
23472345

23482346
expect { bundle "update", all: true }.to change { File.mtime(bundled_app_lock) }
2349-
expect(File.read(bundled_app_lock)).to match("\r\n")
2347+
expect(File.binread(bundled_app_lock)).to match("\r\n")
23502348

23512349
expect(the_bundle).to include_gems "myrack 1.2"
23522350
end
@@ -2362,7 +2360,7 @@ def set_lockfile_mtime_to_known_value
23622360
end.not_to change { File.mtime(bundled_app_lock) }
23632361
end
23642362

2365-
it "preserves Gemfile.lock \\n\\r line endings" do
2363+
it "preserves Gemfile.lock \\r\\n line endings" do
23662364
win_lock = File.read(bundled_app_lock).gsub(/\n/, "\r\n")
23672365
File.open(bundled_app_lock, "wb") {|f| f.puts(win_lock) }
23682366
set_lockfile_mtime_to_known_value

0 commit comments

Comments
 (0)