Skip to content

Commit d1f873e

Browse files
jameshartigmatloob
authored andcommitted
modfile: fix Cleanup clobbering Line reference
Fixes golang/go#45130 Change-Id: I2dccba5e958911177f10a5104a182f86ff8378d9 Reviewed-on: https://go-review.googlesource.com/c/mod/+/303234 Reviewed-by: Michael Matloob <matloob@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
1 parent b56a28f commit d1f873e

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

modfile/read.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,17 @@ func (x *FileSyntax) Cleanup() {
226226
continue
227227
}
228228
if ww == 1 && len(stmt.RParen.Comments.Before) == 0 {
229-
// Collapse block into single line.
230-
line := &Line{
229+
// Collapse block into single line but keep the Line reference used by the
230+
// parsed File structure.
231+
*stmt.Line[0] = Line{
231232
Comments: Comments{
232233
Before: commentsAdd(stmt.Before, stmt.Line[0].Before),
233234
Suffix: commentsAdd(stmt.Line[0].Suffix, stmt.Suffix),
234235
After: commentsAdd(stmt.Line[0].After, stmt.After),
235236
},
236237
Token: stringsAdd(stmt.Token, stmt.Line[0].Token),
237238
}
238-
x.Stmt[w] = line
239+
x.Stmt[w] = stmt.Line[0]
239240
w++
240241
continue
241242
}

modfile/read_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,134 @@ comments before "// k"
603603
})
604604
}
605605
}
606+
607+
func TestCleanup(t *testing.T) {
608+
for _, test := range []struct {
609+
desc string
610+
want string
611+
input []Expr
612+
}{
613+
{
614+
desc: "simple_lines",
615+
want: `line: module a
616+
line: require b v1.0.0
617+
`,
618+
input: []Expr{
619+
&Line{
620+
Token: []string{"module", "a"},
621+
},
622+
&Line{
623+
Token: []string{"require", "b", "v1.0.0"},
624+
},
625+
&Line{
626+
Token: nil,
627+
},
628+
},
629+
}, {
630+
desc: "line_block",
631+
want: `line: module a
632+
block: require
633+
blockline: b v1.0.0
634+
blockline: c v1.0.0
635+
`,
636+
input: []Expr{
637+
&Line{
638+
Token: []string{"module", "a"},
639+
},
640+
&LineBlock{
641+
Token: []string{"require"},
642+
Line: []*Line{
643+
{
644+
Token: []string{"b", "v1.0.0"},
645+
InBlock: true,
646+
},
647+
{
648+
Token: nil,
649+
InBlock: true,
650+
},
651+
{
652+
Token: []string{"c", "v1.0.0"},
653+
InBlock: true,
654+
},
655+
},
656+
},
657+
},
658+
}, {
659+
desc: "collapse",
660+
want: `line: module a
661+
line: require b v1.0.0
662+
`,
663+
input: []Expr{
664+
&Line{
665+
Token: []string{"module", "a"},
666+
},
667+
&LineBlock{
668+
Token: []string{"require"},
669+
Line: []*Line{
670+
{
671+
Token: []string{"b", "v1.0.0"},
672+
InBlock: true,
673+
},
674+
{
675+
Token: nil,
676+
InBlock: true,
677+
},
678+
},
679+
},
680+
},
681+
},
682+
} {
683+
t.Run(test.desc, func(t *testing.T) {
684+
syntax := &FileSyntax{
685+
Stmt: test.input,
686+
}
687+
syntax.Cleanup()
688+
689+
buf := &bytes.Buffer{}
690+
for _, stmt := range syntax.Stmt {
691+
switch stmt := stmt.(type) {
692+
case *Line:
693+
fmt.Fprintf(buf, "line: %v\n", strings.Join(stmt.Token, " "))
694+
case *LineBlock:
695+
fmt.Fprintf(buf, "block: %v\n", strings.Join(stmt.Token, " "))
696+
for _, line := range stmt.Line {
697+
fmt.Fprintf(buf, "blockline: %v\n", strings.Join(line.Token, " "))
698+
}
699+
}
700+
}
701+
702+
got := strings.TrimSpace(buf.String())
703+
want := strings.TrimSpace(test.want)
704+
if got != want {
705+
t.Errorf("got:\n%s\nwant:\n%s", got, want)
706+
}
707+
})
708+
}
709+
}
710+
711+
// Issue 45130: File.Cleanup breaks references so future edits do nothing
712+
func TestCleanupMaintainsRefs(t *testing.T) {
713+
lineB := &Line{
714+
Token: []string{"b", "v1.0.0"},
715+
InBlock: true,
716+
}
717+
syntax := &FileSyntax{
718+
Stmt: []Expr{
719+
&LineBlock{
720+
Token: []string{"require"},
721+
Line: []*Line{
722+
lineB,
723+
{
724+
Token: nil,
725+
InBlock: true,
726+
},
727+
},
728+
},
729+
},
730+
}
731+
syntax.Cleanup()
732+
733+
if syntax.Stmt[0] != lineB {
734+
t.Errorf("got:\n%v\nwant:\n%v", syntax.Stmt[0], lineB)
735+
}
736+
}

0 commit comments

Comments
 (0)