Skip to content

Commit 52289f1

Browse files
Agent-Hellboymatloob
authored andcommitted
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: 1477d7c GitHub-Pull-Request: #22 Reviewed-on: https://go-review.googlesource.com/c/mod/+/634875 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent dec0365 commit 52289f1

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

modfile/read.go

+5
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ func (in *input) parseLineBlock(start Position, token []string, lparen token) *L
877877
in.Error(fmt.Sprintf("syntax error (unterminated block started at %s:%d:%d)", in.filename, x.Start.Line, x.Start.LineRune))
878878
case ')':
879879
rparen := in.lex()
880+
// Don't preserve blank lines (denoted by a single empty comment, added above)
881+
// at the end of the block.
882+
if len(comments) == 1 && comments[0] == (Comment{}) {
883+
comments = nil
884+
}
880885
x.RParen.Before = comments
881886
x.RParen.Pos = rparen.pos
882887
if !in.peek().isEOL() {

modfile/testdata/issue70632.golden

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module tidy
2+
3+
go 1.23.0
4+
5+
require (
6+
golang.org/x/time v0.8.0
7+
)

modfile/testdata/issue70632.in

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module tidy
2+
3+
go 1.23.0
4+
5+
require (
6+
7+
"golang.org/x/time" v0.8.0
8+
9+
10+
11+
12+
)

0 commit comments

Comments
 (0)