Skip to content

Commit e6504ce

Browse files
committed
cmd/go: add compatibility tests for go mod tidy -diff
For #27005 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Change-Id: I90ab8c21222ac2189abb40e8c8e7549e2d940dd2 Reviewed-on: https://go-review.googlesource.com/c/go/+/587941 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 81d424e commit e6504ce

13 files changed

+204
-121
lines changed

src/cmd/go/alldocs.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ to standard error.
3535
The -e flag causes tidy to attempt to proceed despite errors
3636
encountered while loading packages.
3737
38-
The -diff flag causes tidy not to modify the files but instead print the
39-
necessary changes as a unified diff. It exits with a non-zero code
40-
if updates are needed.
38+
The -diff flag causes tidy not to modify go.mod or go.sum but
39+
instead print the necessary changes as a unified diff. It exits
40+
with a non-zero code if the diff is not empty.
4141
4242
The -go flag causes tidy to update the 'go' directive in the go.mod
4343
file to the given version, which may change which module dependencies

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ func TidyGoSum(keep map[module.Version]bool) (before, after []byte) {
898898
return before, after
899899
}
900900

901-
// tidyGoSum will return a tidy version of the go.sum file.
901+
// tidyGoSum returns a tidy version of the go.sum file.
902902
// The goSum lock must be held.
903903
func tidyGoSum(data []byte, keep map[module.Version]bool) []byte {
904904
if !goSum.overwrite {

src/cmd/go/internal/modload/load.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ type PackageOpts struct {
155155
// packages.
156156
Tidy bool
157157

158-
// TidyDiff, if true, analyzes the necessary changes to go.mod and go.sum
159-
// to make them tidy. It does not modify these files, but exits with
160-
// a non-zero code if updates are needed.
158+
// TidyDiff, if true, causes tidy not to modify go.mod or go.sum but
159+
// instead print the necessary changes as a unified diff. It exits
160+
// with a non-zero code if the diff is not empty.
161161
TidyDiff bool
162162

163163
// TidyCompatibleVersion is the oldest Go version that must be able to
@@ -445,7 +445,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
445445
if err != nil {
446446
base.Fatal(err)
447447
}
448-
goModDiff := diff.Diff("current go.mod", currentGoMod, "tidy go.mod", updatedGoMod)
448+
goModDiff := diff.Diff("current/go.mod", currentGoMod, "tidy/go.mod", updatedGoMod)
449449

450450
modfetch.TrimGoSum(keep)
451451
// Dropping compatibility for 1.16 may result in a strictly smaller go.sum.
@@ -454,7 +454,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
454454
keep = keepSums(ctx, loaded, requirements, addBuildListZipSums)
455455
}
456456
currentGoSum, tidyGoSum := modfetch.TidyGoSum(keep)
457-
goSumDiff := diff.Diff("current go.sum", currentGoSum, "tidy go.sum", tidyGoSum)
457+
goSumDiff := diff.Diff("current/go.sum", currentGoSum, "tidy/go.sum", tidyGoSum)
458458

459459
if len(goModDiff) > 0 {
460460
fmt.Println(string(goModDiff))

src/cmd/go/testdata/script/mod_tidy_compat.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ cp go.mod go.mod.orig
3030
go mod tidy
3131
cmp go.mod go.mod.orig
3232

33+
# Make sure that -diff behaves the same as tidy.
34+
[exec:patch] mv go.mod go.mod.tidyResult
35+
[exec:patch] mv go.sum go.sum.tidyResult
36+
[exec:patch] cp go.mod.orig go.mod
37+
[exec:patch] ! go mod tidy -diff
38+
[exec:patch] cp stdout diff.patch
39+
[exec:patch] exec patch -p1 -i diff.patch
40+
[exec:patch] go mod tidy -diff
41+
[exec:patch] cmp go.mod go.mod.tidyResult
42+
[exec:patch] cmp go.sum go.sum.tidyResult
43+
3344
go list -m all
3445
cmp stdout m_all.txt
3546

@@ -45,6 +56,17 @@ cp go.mod.orig go.mod
4556
go mod tidy -compat=1.17
4657
cmp go.mod go.mod.orig
4758

59+
# Make sure that -diff behaves the same as tidy.
60+
[exec:patch] mv go.mod go.mod.tidyResult
61+
[exec:patch] mv go.sum go.sum.tidyResult
62+
[exec:patch] cp go.mod.orig go.mod
63+
[exec:patch] ! go mod tidy -compat=1.17 -diff
64+
[exec:patch] cp stdout diff.patch
65+
[exec:patch] exec patch -p1 -i diff.patch
66+
[exec:patch] go mod tidy -compat=1.17 -diff
67+
[exec:patch] cmp go.mod go.mod.tidyResult
68+
[exec:patch] cmp go.sum go.sum.tidyResult
69+
4870
go list -m all
4971
cmp stdout m_all.txt
5072

src/cmd/go/testdata/script/mod_tidy_compat_added.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ stderr '^go: example\.com/m imports\n\texample\.net/added: module example\.net/a
2121

2222
cmp go.mod go.mod.orig
2323

24+
# Make sure that -diff behaves the same as tidy.
25+
[exec:patch] cp go.mod.orig go.mod
26+
[exec:patch] ! exists go.sum
27+
[exec:patch] ! go mod tidy -diff
28+
[exec:patch] ! stdout .
29+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/added: module example\.net/added@latest found \(v0\.3\.0, replaced by \./a1\), but does not contain package example\.net/added$'
2430

2531
# When we run 'go mod tidy -e', we should proceed past the first error and follow
2632
# it with a second error describing the version discrepancy.
@@ -37,6 +43,20 @@ stderr '^go: example\.com/m imports\n\texample\.net/added: module example\.net/a
3743

3844
cmp go.mod go.mod.tidy
3945

46+
# Make sure that -diff behaves the same as tidy.
47+
[exec:patch] cp go.mod go.mod.tidyResult
48+
[exec:patch] ! exists go.sum
49+
[exec:patch] cp go.mod.orig go.mod
50+
[exec:patch] ! go mod tidy -e -diff
51+
[exec:patch] stdout 'diff current/go.mod tidy/go.mod'
52+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/added: module example\.net/added@latest found \(v0\.3\.0, replaced by \./a1\), but does not contain package example\.net/added\ngo: example\.net/added failed to load from any module,\n\tbut go 1\.16 would load it from example\.net/added@v0\.2\.0$'
53+
[exec:patch] ! stderr '\n\tgo mod tidy'
54+
[exec:patch] cp stdout diff.patch
55+
[exec:patch] exec patch -p1 -i diff.patch
56+
[exec:patch] go mod tidy -e -diff
57+
[exec:patch] ! stdout .
58+
[exec:patch] cmp go.mod go.mod.tidyResult
59+
[exec:patch] ! exists go.sum
4060

4161
-- go.mod --
4262
module example.com/m

src/cmd/go/testdata/script/mod_tidy_compat_ambiguous.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ stderr '\n\nTo proceed despite packages unresolved in go 1\.16:\n\tgo mod tidy -
2727

2828
cmp go.mod go.mod.orig
2929

30+
# Make sure that -diff behaves the same as tidy.
31+
[exec:patch] cp go.mod.orig go.mod
32+
[exec:patch] ! exists go.sum
33+
[exec:patch] ! go mod tidy -diff
34+
[exec:patch] ! stdout .
35+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/indirect imports\n\texample\.net/ambiguous/nested/pkg loaded from example\.net/ambiguous/nested@v0\.1\.0,\n\tbut go 1.16 would fail to locate it:\n\tambiguous import: found package example\.net/ambiguous/nested/pkg in multiple modules:\n\texample\.net/ambiguous v0.1.0 \(.*\)\n\texample\.net/ambiguous/nested v0.1.0 \(.*\)\n\n'
36+
[exec:patch] stderr '\n\nTo proceed despite packages unresolved in go 1\.16:\n\tgo mod tidy -e\nIf reproducibility with go 1.16 is not needed:\n\tgo mod tidy -compat=1\.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
37+
3038

3139
# If we run 'go mod tidy -e', we should still save enough checksums to run
3240
# 'go list -m all' reproducibly with go 1.16, even though we can't list
@@ -36,6 +44,19 @@ go mod tidy -e
3644
! stderr '\n\tgo mod tidy'
3745
cmp go.mod go.mod.orig
3846

47+
# Make sure that -diff behaves the same as tidy.
48+
[exec:patch] mv go.mod go.mod.tidyResult
49+
[exec:patch] mv go.sum go.sum.tidyResult
50+
[exec:patch] cp go.mod.orig go.mod
51+
[exec:patch] ! go mod tidy -e -diff
52+
[exec:patch] ! stderr '\n\tgo mod tidy'
53+
[exec:patch] cp stdout diff.patch
54+
[exec:patch] exec patch -p1 -i diff.patch
55+
[exec:patch] go mod tidy -e -diff
56+
[exec:patch] ! stdout .
57+
[exec:patch] cmp go.mod go.mod.tidyResult
58+
[exec:patch] cmp go.sum go.sum.tidyResult
59+
3960
go list -m all
4061
cmp stdout all-m.txt
4162

@@ -60,6 +81,19 @@ go mod tidy -compat=1.17
6081
go list -m all
6182
cmp stdout all-m.txt
6283

84+
# Make sure that -diff behaves the same as tidy.
85+
[exec:patch] mv go.mod go.mod.tidyResult
86+
[exec:patch] mv go.sum go.sum.tidyResult
87+
[exec:patch] cp go.mod.orig go.mod
88+
[exec:patch] ! go mod tidy -compat=1.17 -diff
89+
[exec:patch] ! stderr .
90+
[exec:patch] cp stdout diff.patch
91+
[exec:patch] exec patch -p1 -i diff.patch
92+
[exec:patch] go mod tidy -compat=1.17 -diff
93+
[exec:patch] ! stdout .
94+
[exec:patch] cmp go.mod go.mod.tidyResult
95+
[exec:patch] cmp go.sum go.sum.tidyResult
96+
6397
go mod edit -go=1.16
6498
! go list -m all
6599
stderr '^go: example\.net/indirect@v0\.1\.0 requires\n\texample\.net/ambiguous@v0\.1\.0: missing go\.sum entry for go\.mod file; to add it:\n\tgo mod download example\.net/ambiguous\n'

src/cmd/go/testdata/script/mod_tidy_compat_deleted.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,30 @@ stderr '^go: example\.com/m imports\n\texample\.net/deleted loaded from example\
2121

2222
stderr '\n\nTo upgrade to the versions selected by go 1.16, leaving some packages unresolved:\n\tgo mod tidy -e -go=1\.16 && go mod tidy -e -go=1\.17\nIf reproducibility with go 1.16 is not needed:\n\tgo mod tidy -compat=1\.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
2323

24+
# Make sure that -diff behaves the same as tidy.
25+
[exec:patch] cp go.mod.orig go.mod
26+
[exec:patch] ! exists go.sum
27+
[exec:patch] ! go mod tidy -diff
28+
[exec:patch] ! stdout .
29+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/deleted loaded from example\.net/deleted@v0\.1\.0,\n\tbut go 1\.16 would fail to locate it in example\.net/deleted@v0\.2\.0\n\n'
30+
[exec:patch] stderr '\n\nTo upgrade to the versions selected by go 1.16, leaving some packages unresolved:\n\tgo mod tidy -e -go=1\.16 && go mod tidy -e -go=1\.17\nIf reproducibility with go 1.16 is not needed:\n\tgo mod tidy -compat=1\.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
2431

2532
# The suggested 'go mod tidy -e' command should proceed anyway.
2633

2734
go mod tidy -e
2835
cmp go.mod go.mod.tidy
2936

37+
# Make sure that -diff behaves the same as tidy.
38+
[exec:patch] cp go.mod go.mod.tidyResult
39+
[exec:patch] ! exists go.sum
40+
[exec:patch] cp go.mod.orig go.mod
41+
[exec:patch] ! go mod tidy -e -diff
42+
[exec:patch] cp stdout diff.patch
43+
[exec:patch] exec patch -p1 -i diff.patch
44+
[exec:patch] go mod tidy -e -diff
45+
[exec:patch] ! stdout .
46+
[exec:patch] cmp go.mod go.mod.tidyResult
47+
[exec:patch] ! exists go.sum
3048

3149
# In 'go 1.16' mode we should error out in the way we claimed.
3250

@@ -39,9 +57,16 @@ go mod edit -go=1.16
3957
! go list -deps -f $MODFMT example.com/m
4058
stderr '^go: updates to go\.mod needed; to update it:\n\tgo mod tidy$'
4159

60+
[exec:patch] cp go.mod go.mod.orig
4261
! go mod tidy
4362
stderr '^go: example\.com/m imports\n\texample\.net/deleted: module example\.net/deleted@latest found \(v0\.2\.0, replaced by \./d2\), but does not contain package example\.net/deleted$'
4463

64+
# Make sure that -diff behaves the same as tidy.
65+
[exec:patch] cp go.mod.orig go.mod
66+
[exec:patch] ! exists go.sum
67+
[exec:patch] ! go mod tidy -diff
68+
[exec:patch] ! stdout .
69+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/deleted: module example\.net/deleted@latest found \(v0\.2\.0, replaced by \./d2\), but does not contain package example\.net/deleted$'
4570

4671
-- go.mod --
4772
module example.com/m

src/cmd/go/testdata/script/mod_tidy_compat_implicit.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=
3737

3838
cmp go.mod go.mod.orig
3939

40+
# Make sure that -diff behaves the same as tidy.
41+
[exec:patch] cp go.mod.orig go.mod
42+
[exec:patch] ! exists go.sum
43+
[exec:patch] ! go mod tidy -diff
44+
[exec:patch] ! stdout .
45+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/lazy tested by\n\texample\.net/lazy.test imports\n\texample\.com/retract/incompatible loaded from example\.com/retract/incompatible@v1\.0\.0,\n\tbut go 1\.16 would select v2\.0\.0\+incompatible\n\n'
46+
[exec:patch] stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=1\.16 && go mod tidy -go=1\.17\nIf reproducibility with go 1.16 is not needed:\n\tgo mod tidy -compat=1.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
47+
4048
# The suggested '-compat' flag to ignore differences should silence the error
4149
# and leave go.mod unchanged, resulting in checksum errors when Go 1.16 tries
4250
# to load a module pruned out by Go 1.17.
@@ -45,6 +53,18 @@ go mod tidy -compat=1.17
4553
! stderr .
4654
cmp go.mod go.mod.orig
4755

56+
# Make sure that -diff behaves the same as tidy.
57+
[exec:patch] mv go.mod go.mod.tidyResult
58+
[exec:patch] mv go.sum go.sum.tidyResult
59+
[exec:patch] cp go.mod.orig go.mod
60+
[exec:patch] ! go mod tidy -compat=1.17 -diff
61+
[exec:patch] cp stdout diff.patch
62+
[exec:patch] exec patch -p1 -i diff.patch
63+
[exec:patch] go mod tidy -compat=1.17 -diff
64+
[exec:patch] ! stdout .
65+
[exec:patch] cmp go.mod go.mod.tidyResult
66+
[exec:patch] cmp go.sum go.sum.tidyResult
67+
4868
go list -deps -test -f $MODFMT ./...
4969
stdout '^example.net/lazy v0.1.0$'
5070

src/cmd/go/testdata/script/mod_tidy_compat_incompatible.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=
3737

3838
cmp go.mod go.mod.orig
3939

40+
# Make sure that -diff behaves the same as tidy.
41+
[exec:patch] cp go.mod.orig go.mod
42+
[exec:patch] ! exists go.sum
43+
[exec:patch] ! go mod tidy -diff
44+
[exec:patch] ! stdout .
45+
[exec:patch] stderr '^go: example\.com/m imports\n\texample\.net/lazy imports\n\texample\.com/retract/incompatible loaded from example\.com/retract/incompatible@v1\.0\.0,\n\tbut go 1\.16 would select v2\.0\.0\+incompatible\n\n'
46+
[exec:patch] stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=1\.16 && go mod tidy -go=1\.17\nIf reproducibility with go 1\.16 is not needed:\n\tgo mod tidy -compat=1.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
4047

4148
# The suggested '-compat' flag to ignore differences should silence the error
4249
# and leave go.mod unchanged, resulting in checksum errors when Go 1.16 tries
@@ -46,6 +53,18 @@ go mod tidy -compat=1.17
4653
! stderr .
4754
cmp go.mod go.mod.orig
4855

56+
# Make sure that -diff behaves the same as tidy.
57+
[exec:patch] mv go.mod go.mod.tidyResult
58+
[exec:patch] mv go.sum go.sum.tidyResult
59+
[exec:patch] cp go.mod.orig go.mod
60+
[exec:patch] ! go mod tidy -compat=1.17 -diff
61+
[exec:patch] cp stdout diff.patch
62+
[exec:patch] exec patch -p1 -i diff.patch
63+
[exec:patch] go mod tidy -compat=1.17 -diff
64+
[exec:patch] ! stdout .
65+
[exec:patch] cmp go.mod go.mod.tidyResult
66+
[exec:patch] cmp go.sum go.sum.tidyResult
67+
4968
go mod edit -go=1.16
5069
! go list -f $MODFMT -deps ./...
5170
stderr -count=1 '^go: example\.net/lazy@v0\.1\.0 requires\n\texample\.net/requireincompatible@v0\.1\.0 requires\n\texample\.com/retract/incompatible@v2\.0\.0\+incompatible: missing go.sum entry for go.mod file; to add it:\n\tgo mod download example.com/retract/incompatible$'

src/cmd/go/testdata/script/mod_tidy_compat_irrelevant.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ cp go.mod go.mod.orig
2727
go mod tidy
2828
cmp go.mod go.mod.orig
2929

30+
# Make sure that -diff behaves the same as tidy.
31+
[exec:patch] mv go.mod go.mod.tidyResult
32+
[exec:patch] mv go.sum go.sum.tidyResult
33+
[exec:patch] cp go.mod.orig go.mod
34+
[exec:patch] ! go mod tidy -diff
35+
[exec:patch] cp stdout diff.patch
36+
[exec:patch] exec patch -p1 -i diff.patch
37+
[exec:patch] go mod tidy -diff
38+
[exec:patch] ! stdout .
39+
[exec:patch] cmp go.mod go.mod.tidyResult
40+
[exec:patch] cmp go.sum go.sum.tidyResult
41+
3042
go list -deps -test -f $MODFMT all
3143
cp stdout out-117.txt
3244

@@ -42,6 +54,12 @@ cp go.mod.orig go.mod
4254
go mod tidy -compat=1.17
4355
cmp go.mod go.mod.orig
4456

57+
# Make sure that -diff behaves the same as tidy.
58+
[exec:patch] cp go.mod.orig go.mod
59+
[exec:patch] rm go.sum
60+
[exec:patch] go mod tidy -compat=1.17 -diff
61+
[exec:patch] ! stdout .
62+
4563
go list -deps -test -f $MODFMT all
4664
cmp stdout out-117.txt
4765

0 commit comments

Comments
 (0)