Preserve trailing newline when updating package.json

Fixes https://github.com/oven-sh/bun/issues/1375
This commit is contained in:
Jarred Sumner
2022-10-23 21:53:54 -07:00
parent 360a007f16
commit ec9787770e
2 changed files with 14 additions and 0 deletions

View File

@@ -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, &current_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(

View File

@@ -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();