modfile: remove end-of-line comments in removeLine I added this test case while updating documentation for golang/go#45965, and it failed. This CL fixes the behavior, and the next CL in the stack documents it. For golang/go#45965 Change-Id: Ia68dbd33530eec138745c6e291b096a9fa1e1d58 Reviewed-on: https://go-review.googlesource.com/c/mod/+/323170 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
diff --git a/modfile/read.go b/modfile/read.go index 2a961ca..81e7609 100644 --- a/modfile/read.go +++ b/modfile/read.go
@@ -196,6 +196,7 @@ func (x *FileSyntax) removeLine(line *Line) { line.Token = nil + line.Comments.Suffix = nil } // Cleanup cleans up the file syntax x after any edit operations.
diff --git a/modfile/rule_test.go b/modfile/rule_test.go index d581ba2..2ec24ea 100644 --- a/modfile/rule_test.go +++ b/modfile/rule_test.go
@@ -33,6 +33,32 @@ `, }, { + `existing2`, + ` + module m + require ( + x.y/z v1.2.3 // first + x.z/a v0.1.0 // first-a + ) + require x.y/z v1.4.5 // second + require ( + x.y/z v1.6.7 // third + x.z/a v0.2.0 // third-a + ) + `, + "x.y/z", "v1.8.9", + ` + module m + + require ( + x.y/z v1.8.9 // first + x.z/a v0.1.0 // first-a + ) + + require x.z/a v0.2.0 // third-a + `, + }, + { `new`, ` module m @@ -892,7 +918,9 @@ for _, tt := range addRequireTests { t.Run(tt.desc, func(t *testing.T) { testEdit(t, tt.in, tt.out, true, func(f *File) error { - return f.AddRequire(tt.path, tt.vers) + err := f.AddRequire(tt.path, tt.vers) + f.Cleanup() + return err }) }) }