...
1# Integration test for golang.org/issue/33848: automatically check and use vendored packages.
2
3env GO111MODULE=on
4
5[short] skip
6
7cd $WORK/auto
8cp go.mod go.mod.orig
9cp $WORK/modules-1.13.txt $WORK/auto/modules.txt
10
11# An explicit -mod=vendor should force use of the vendor directory.
12env GOFLAGS=-mod=vendor
13
14# Pass -e to permit an error: tools.go imports a main package
15# "example.com/printversion".
16# TODO(#59186): investigate why it didn't fail without -e.
17go list -f {{.Dir}} -tags tools -e all
18stdout '^'$WORK'[/\\]auto$'
19stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
20stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
21
22! go list -m all
23stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
24
25! go list -m -f '{{.Dir}}' all
26stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
27
28# An explicit -mod=mod should force the vendor directory to be ignored.
29env GOFLAGS=-mod=mod
30
31go list -f {{.Dir}} -tags tools -e all
32stdout '^'$WORK'[/\\]auto$'
33stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
34stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
35
36go list -m all
37stdout '^example.com/auto$'
38stdout 'example.com/printversion v1.0.0'
39stdout 'example.com/version v1.0.0'
40
41go list -m -f '{{.Dir}}' all
42stdout '^'$WORK'[/\\]auto$'
43stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
44stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
45
46# If the main module's "go" directive says 1.13, we should default to -mod=mod.
47env GOFLAGS=
48go mod edit -go=1.13
49
50go list -f {{.Dir}} -tags tools -e all
51stdout '^'$WORK'[/\\]auto$'
52stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
53stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
54
55go list -m -f '{{.Dir}}' all
56stdout '^'$WORK'[/\\]auto$'
57stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
58stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
59
60# A 'go 1.14' directive in the main module's go.mod file should enable
61# -mod=vendor by default, along with stronger checks for consistency
62# between the go.mod file and vendor/modules.txt.
63# A 'go 1.13' vendor/modules.txt file is not usually sufficient
64# to pass those checks.
65go mod edit -go=1.14
66
67! go list -f {{.Dir}} -tags tools all
68stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
69stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
70stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
71stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
72stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
73
74# Module-specific subcommands should continue to load the full module graph.
75go mod graph
76stdout '^example.com/printversion@v1.0.0 example.com/version@v1.0.0$'
77
78# An explicit -mod=mod should still force the vendor directory to be ignored.
79env GOFLAGS=-mod=mod
80
81go list -f {{.Dir}} -tags tools -e all
82stdout '^'$WORK'[/\\]auto$'
83stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
84stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
85
86go list -m all
87stdout '^example.com/auto$'
88stdout 'example.com/printversion v1.0.0'
89stdout 'example.com/version v1.0.0'
90
91go list -m -f '{{.Dir}}' all
92stdout '^'$WORK'[/\\]auto$'
93stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
94stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
95
96# 'go mod vendor' should repair vendor/modules.txt so that the implicit
97# -mod=vendor works again.
98env GOFLAGS=
99
100go mod edit -go=1.14
101go mod vendor
102
103go list -f {{.Dir}} -tags tools -e all
104stdout '^'$WORK'[/\\]auto$'
105stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
106stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
107
108# ...but 'go list -m' should continue to fail, this time without
109# referring to a -mod default that the user didn't set.
110! go list -m all
111stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
112
113! go list -m -f '{{.Dir}}' all
114stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
115
116
117# 'go mod init' should work if there is already a GOPATH-mode vendor directory
118# present. If there are no module dependencies, -mod=vendor should be used by
119# default and should not fail the consistency check even though no module
120# information is present.
121# Note: This behavior only applies pre-1.23. Go 1.23 and later require vendored
122# packages to be present in modules.txt to be imported.
123
124rm go.mod
125rm vendor/modules.txt
126
127go mod init example.com/auto
128go mod edit -go=1.22
129go list -f {{.Dir}} -tags tools -e all
130stdout '^'$WORK'[/\\]auto$'
131stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
132stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
133
134# If information about dependencies is added to a 1.14 go.mod file, subsequent
135# list commands should error out if vendor/modules.txt is missing or incomplete.
136
137cp go.mod.orig go.mod
138go mod edit -go=1.14
139! go list -f {{.Dir}} -tags tools -e all
140stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
141stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
142stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
143stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
144stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
145
146# If -mod=vendor is set, limited consistency checks should apply even when
147# the go version is 1.13 or earlier.
148# An incomplete or missing vendor/modules.txt should resolve the vendored packages...
149go mod edit -go=1.13
150go list -mod=vendor -f {{.Dir}} -tags tools -e all
151stdout '^'$WORK'[/\\]auto$'
152stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
153stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
154
155# ...but a version mismatch for an explicit dependency should be noticed.
156cp $WORK/modules-bad-1.13.txt vendor/modules.txt
157! go list -mod=vendor -f {{.Dir}} -tags tools all
158stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
159stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/printversion@v1.1.0$'
160stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
161
162# If the go version is still 1.13, 'go mod vendor' should write a
163# matching vendor/modules.txt containing the corrected 1.13 data.
164go mod vendor
165cmp $WORK/modules-1.13.txt vendor/modules.txt
166
167go list -mod=vendor -f {{.Dir}} -tags tools -e all
168stdout '^'$WORK'[/\\]auto$'
169stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
170stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
171
172# When the version is upgraded to 1.14, 'go mod vendor' should write a
173# vendor/modules.txt with the updated 1.14 annotations.
174go mod edit -go=1.14
175go mod vendor
176cmp $WORK/modules-1.14.txt vendor/modules.txt
177
178# Then, -mod=vendor should kick in automatically and succeed.
179go list -f {{.Dir}} -tags tools -e all
180stdout '^'$WORK'[/\\]auto$'
181stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
182stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
183
184# 'go get' should update from the network or module cache,
185# even if a vendor directory is present.
186go get example.com/version@v1.1.0
187! go list -f {{.Dir}} -tags tools all
188stderr '^go: inconsistent vendoring'
189
190-- $WORK/auto/go.mod --
191module example.com/auto
192
193go 1.13
194
195require example.com/printversion v1.0.0
196
197replace (
198 example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
199 example.com/version v1.0.0 => ./replacement-version
200 example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
201)
202-- $WORK/auto/tools.go --
203// +build tools
204
205package auto
206
207import _ "example.com/printversion"
208-- $WORK/auto/auto.go --
209package auto
210-- $WORK/auto/replacement-version/go.mod --
211module example.com/version
212-- $WORK/auto/replacement-version/version.go --
213package version
214
215const V = "v1.0.0-replaced"
216-- $WORK/modules-1.14.txt --
217# example.com/printversion v1.0.0
218## explicit
219example.com/printversion
220# example.com/version v1.0.0 => ./replacement-version
221example.com/version
222# example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
223# example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
224-- $WORK/modules-1.13.txt --
225# example.com/printversion v1.0.0
226example.com/printversion
227# example.com/version v1.0.0 => ./replacement-version
228example.com/version
229-- $WORK/modules-bad-1.13.txt --
230# example.com/printversion v1.1.0
231example.com/printversion
232# example.com/version v1.1.0
233example.com/version
234-- $WORK/auto/vendor/example.com/printversion/go.mod --
235module example.com/printversion
236
237require example.com/version v1.0.0
238replace example.com/version v1.0.0 => ../oops v0.0.0
239exclude example.com/version v1.0.1
240-- $WORK/auto/vendor/example.com/printversion/printversion.go --
241package main
242
243import (
244 "fmt"
245 "os"
246 "runtime/debug"
247
248 _ "example.com/version"
249)
250
251func main() {
252 info, _ := debug.ReadBuildInfo()
253 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
254 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
255 for _, m := range info.Deps {
256 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
257 }
258}
259-- $WORK/auto/vendor/example.com/version/version.go --
260package version
261
262const V = "v1.0.0-replaced"
View as plain text