From aa51b25a4485b19ca64f578bad6fa40229e75984 Mon Sep 17 00:00:00 2001 From: Quan Tong Date: Mon, 13 Nov 2023 12:14:57 +0700 Subject: [PATCH] modfile: do not collapse if there are unattached comments within blocks The existing implementation drops unattached comments when collapsing block into single line. Fixes #33280 Change-Id: I388e1468657d461cba771e90d5e328746d927ae2 Reviewed-on: https://go-review.googlesource.com/c/mod/+/541815 Reviewed-by: Cherry Mui Reviewed-by: Bryan Mills Auto-Submit: Bryan Mills LUCI-TryBot-Result: Go LUCI --- modfile/read.go | 2 +- modfile/rule_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/modfile/read.go b/modfile/read.go index 5b5bb5e..2205682 100644 --- a/modfile/read.go +++ b/modfile/read.go @@ -225,7 +225,7 @@ func (x *FileSyntax) Cleanup() { if ww == 0 { continue } - if ww == 1 { + if ww == 1 && len(stmt.RParen.Comments.Before) == 0 { // Collapse block into single line. line := &Line{ Comments: Comments{ diff --git a/modfile/rule_test.go b/modfile/rule_test.go index 96e0bfe..ca11d17 100644 --- a/modfile/rule_test.go +++ b/modfile/rule_test.go @@ -90,6 +90,24 @@ var addRequireTests = []struct { ) `, }, + { + `unattached_comments`, + ` + module m + require ( + foo v0.0.0-00010101000000-000000000000 + // bar v0.0.0-00010101000000-000000000000 + ) + `, + "foo", "v0.0.0-00010101000000-000000000000", + ` + module m + require ( + foo v0.0.0-00010101000000-000000000000 + // bar v0.0.0-00010101000000-000000000000 + ) + `, + }, } type require struct {