modfile: fix trailing empty lines in require blocks
This change ensures that trailing empty lines in `require` blocks
are ignored during parsing itself. Specifically:
- Modified the `parseLineBlock` function to detect and discard blank lines
(represented by a single empty comment) at the end of a block.
- Blank lines within a block are preserved as expected, but trailing
blank lines immediately before the closing parenthesis are now skipped.
For golang/go#70632
Change-Id: Ica76b3edb3bf7fdc327c7cdc9e401dcf19c523b0
GitHub-Last-Rev: 1477d7ce8b79b953be1bf5d7a20d4f9917347299
GitHub-Pull-Request: golang/mod#22
Reviewed-on: https://go-review.googlesource.com/c/mod/+/634875
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
diff --git a/modfile/read.go b/modfile/read.go
index de1b982..2d74868 100644
--- a/modfile/read.go
+++ b/modfile/read.go
@@ -877,6 +877,11 @@
in.Error(fmt.Sprintf("syntax error (unterminated block started at %s:%d:%d)", in.filename, x.Start.Line, x.Start.LineRune))
case ')':
rparen := in.lex()
+ // Don't preserve blank lines (denoted by a single empty comment, added above)
+ // at the end of the block.
+ if len(comments) == 1 && comments[0] == (Comment{}) {
+ comments = nil
+ }
x.RParen.Before = comments
x.RParen.Pos = rparen.pos
if !in.peek().isEOL() {
diff --git a/modfile/testdata/issue70632.golden b/modfile/testdata/issue70632.golden
new file mode 100644
index 0000000..f559879
--- /dev/null
+++ b/modfile/testdata/issue70632.golden
@@ -0,0 +1,7 @@
+module tidy
+
+go 1.23.0
+
+require (
+ golang.org/x/time v0.8.0
+)
diff --git a/modfile/testdata/issue70632.in b/modfile/testdata/issue70632.in
new file mode 100644
index 0000000..e6e6b93
--- /dev/null
+++ b/modfile/testdata/issue70632.in
@@ -0,0 +1,12 @@
+module tidy
+
+go 1.23.0
+
+require (
+
+ "golang.org/x/time" v0.8.0
+
+
+
+
+)