diff options
author | 2022-10-23 21:53:54 -0700 | |
---|---|---|
committer | 2022-10-23 21:53:54 -0700 | |
commit | ec9787770eec73201fae7ee60982e1df93391525 (patch) | |
tree | bc0b9a2dfea4bfb0074baa05a0d95552c7c74dad | |
parent | 360a007f160b6935140dc75003a503059ff23976 (diff) | |
download | bun-ec9787770eec73201fae7ee60982e1df93391525.tar.gz bun-ec9787770eec73201fae7ee60982e1df93391525.tar.zst bun-ec9787770eec73201fae7ee60982e1df93391525.zip |
Preserve trailing newline when updating package.json
Fixes https://github.com/oven-sh/bun/issues/1375
-rw-r--r-- | src/install/install.zig | 8 | ||||
-rw-r--r-- | src/js_printer.zig | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/install/install.zig b/src/install/install.zig index 7ea14acf6..683261e50 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -4541,6 +4541,12 @@ pub const PackageManager = struct { current_package_json_buf[0..current_package_json_contents_len], ); + // If there originally was a newline at the end of their package.json, preserve it + // so that we don't cause unnecessary diffs in their git history. + // https://github.com/oven-sh/bun/issues/1375 + const preserve_trailing_newline_at_eof_for_package_json = current_package_json_contents_len > 0 and + current_package_json_buf[current_package_json_contents_len - 1] == '\n'; + initializeStore(); var current_package_json = json_parser.ParseJSONUTF8(&package_json_source, ctx.log, manager.allocator) catch |err| { if (Output.enable_ansi_colors) { @@ -4691,6 +4697,8 @@ pub const PackageManager = struct { try PackageJSONEditor.edit(ctx.allocator, updates, ¤t_package_json, dependency_list); var buffer_writer_two = try JSPrinter.BufferWriter.init(ctx.allocator); try buffer_writer_two.buffer.list.ensureTotalCapacity(ctx.allocator, new_package_json_source.len + 1); + buffer_writer_two.append_newline = + preserve_trailing_newline_at_eof_for_package_json; var package_json_writer_two = JSPrinter.BufferPrinter.init(buffer_writer_two); written = JSPrinter.printJSON( diff --git a/src/js_printer.zig b/src/js_printer.zig index 707161ffd..bd5573a17 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -4899,6 +4899,7 @@ pub const BufferWriter = struct { written: []u8 = "", sentinel: [:0]u8 = "", append_null_byte: bool = false, + append_newline: bool = false, approximate_newline_count: usize = 0, pub fn getWritten(this: *BufferWriter) []u8 { @@ -4963,6 +4964,11 @@ pub const BufferWriter = struct { pub fn done( ctx: *BufferWriter, ) anyerror!void { + if (ctx.append_newline) { + ctx.append_newline = false; + try ctx.buffer.appendChar('\n'); + } + if (ctx.append_null_byte) { ctx.sentinel = ctx.buffer.toOwnedSentinelLeaky(); ctx.written = ctx.buffer.toOwnedSliceLeaky(); |