Text file
src/cmd/go/testdata/script/mod_tidy_compat_incompatible.txt
1 # https://golang.org/issue/46141: 'go mod tidy' for a Go 1.17 module should by
2 # default preserve enough checksums for the module to be used by Go 1.16.
3 #
4 # We don't have a copy of Go 1.16 handy, but we can simulate it by editing the
5 # 'go' version in the go.mod file to 1.16, without actually updating the
6 # requirements to match.
7
8 [short] skip
9
10 env MODFMT='{{with .Module}}{{.Path}} {{.Version}}{{end}}'
11
12
13 # For this module, Go 1.17 prunes out a (transitive and otherwise-irrelevant)
14 # requirement on a retracted higher version of a dependency.
15 # However, when Go 1.16 reads the same requirements from the go.mod file,
16 # it does not prune out that requirement, and selects the retracted version.
17 #
18 # The Go 1.16 module graph looks like:
19 #
20 # m ---- lazy v0.1.0 ---- requireincompatible v0.1.0 ---- incompatible v2.0.0+incompatible
21 # | |
22 # + -------+------------- incompatible v1.0.0
23 #
24 # The Go 1.17 module graph is the same except that the dependencies of
25 # requireincompatible are pruned out (because the module that requires
26 # it — lazy v0.1.0 — specifies 'go 1.17', and it is not otherwise relevant to
27 # the main module).
28
29
30 # 'go mod tidy' should by default diagnose the difference in dependencies as an
31 # error, with useful suggestions about how to resolve it.
32
33 cp go.mod go.mod.orig
34 ! go mod tidy
35 stderr '^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'
36 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'
37
38 cmp go.mod go.mod.orig
39
40
41 # The suggested '-compat' flag to ignore differences should silence the error
42 # and leave go.mod unchanged, resulting in checksum errors when Go 1.16 tries
43 # to load a module pruned out by Go 1.17.
44
45 go mod tidy -compat=1.17
46 ! stderr .
47 cmp go.mod go.mod.orig
48
49 go mod edit -go=1.16
50 ! go list -f $MODFMT -deps ./...
51 # TODO(#46160): -count=1 instead of -count=2.
52 stderr -count=2 '^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; to add it:\n\tgo mod download example.com/retract/incompatible$'
53
54
55 # There are two ways for the module author to bring the two into alignment.
56 # One is to *explicitly* 'exclude' the version that is already *implicitly*
57 # pruned out under 1.17.
58
59 go mod edit -exclude=example.com/retract/incompatible@v2.0.0+incompatible
60 go list -f $MODFMT -deps ./...
61 stdout '^example.com/retract/incompatible v1\.0\.0$'
62 ! stdout 'v2\.0\.0'
63
64
65 # The other is to explicitly upgrade the version required under Go 1.17
66 # to match the version selected by Go 1.16. The commands suggested by
67 # 'go mod tidy' should do exactly that.
68
69 cp go.mod.orig go.mod
70
71 go mod tidy -go=1.16
72 go list -f $MODFMT -deps ./...
73 stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
74 ! stdout 'v1\.0\.0'
75
76 go mod tidy -go=1.17
77 go list -f $MODFMT -deps ./...
78 stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
79 ! stdout 'v1\.0\.0'
80
81 go mod edit -go=1.16
82 go list -f $MODFMT -deps ./...
83 stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
84 ! stdout 'v1\.0\.0'
85
86
87 -- go.mod --
88 // Module m indirectly imports a package from
89 // example.com/retract/incompatible. Its selected version of
90 // that module is lower under Go 1.17 semantics than under Go 1.16.
91 module example.com/m
92
93 go 1.17
94
95 replace (
96 example.net/lazy v0.1.0 => ./lazy
97 example.net/requireincompatible v0.1.0 => ./requireincompatible
98 )
99
100 require example.net/lazy v0.1.0
101
102 require example.com/retract/incompatible v1.0.0 // indirect
103 -- incompatible.go --
104 package incompatible
105
106 import _ "example.net/lazy"
107
108 -- lazy/go.mod --
109 // Module lazy requires example.com/retract/incompatible v1.0.0.
110 //
111 // When viewed from the outside it also has a transitive dependency
112 // on v2.0.0+incompatible, but in lazy mode that transitive dependency
113 // is pruned out.
114 module example.net/lazy
115
116 go 1.17
117
118 exclude example.com/retract/incompatible v2.0.0+incompatible
119
120 require (
121 example.com/retract/incompatible v1.0.0
122 example.net/requireincompatible v0.1.0
123 )
124 -- lazy/lazy.go --
125 package lazy
126
127 import _ "example.com/retract/incompatible"
128
129 -- requireincompatible/go.mod --
130 module example.net/requireincompatible
131
132 go 1.15
133
134 require example.com/retract/incompatible v2.0.0+incompatible
135
View as plain text