1 # Integration test for golang.org/issue/33848: automatically check and use vendored packages.
2
3 env GO111MODULE=on
4
5 [short] skip
6
7 cd $WORK/auto
8 cp go.mod go.mod.orig
9 cp $WORK/modules-1.13.txt $WORK/auto/modules.txt
10
11 # An explicit -mod=vendor should force use of the vendor directory.
12 env 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.
17 go list -f {{.Dir}} -tags tools -e all
18 stdout '^'$WORK'[/\\]auto$'
19 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
20 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
21
22 ! go list -m all
23 stderr '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
26 stderr '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.
29 env GOFLAGS=-mod=mod
30
31 go list -f {{.Dir}} -tags tools -e all
32 stdout '^'$WORK'[/\\]auto$'
33 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
34 stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
35
36 go list -m all
37 stdout '^example.com/auto$'
38 stdout 'example.com/printversion v1.0.0'
39 stdout 'example.com/version v1.0.0'
40
41 go list -m -f '{{.Dir}}' all
42 stdout '^'$WORK'[/\\]auto$'
43 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
44 stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
45
46 # If the main module's "go" directive says 1.13, we should default to -mod=mod.
47 env GOFLAGS=
48 go mod edit -go=1.13
49
50 go list -f {{.Dir}} -tags tools -e all
51 stdout '^'$WORK'[/\\]auto$'
52 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
53 stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
54
55 go list -m -f '{{.Dir}}' all
56 stdout '^'$WORK'[/\\]auto$'
57 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
58 stdout '^'$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.
65 go mod edit -go=1.14
66
67 ! go list -f {{.Dir}} -tags tools all
68 stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
69 stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
70 stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
71 stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
72 stderr '^\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.
75 go mod graph
76 stdout '^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.
79 env GOFLAGS=-mod=mod
80
81 go list -f {{.Dir}} -tags tools -e all
82 stdout '^'$WORK'[/\\]auto$'
83 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
84 stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
85
86 go list -m all
87 stdout '^example.com/auto$'
88 stdout 'example.com/printversion v1.0.0'
89 stdout 'example.com/version v1.0.0'
90
91 go list -m -f '{{.Dir}}' all
92 stdout '^'$WORK'[/\\]auto$'
93 stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
94 stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
95
96 # 'go mod vendor' should repair vendor/modules.txt so that the implicit
97 # -mod=vendor works again.
98 env GOFLAGS=
99
100 go mod edit -go=1.14
101 go mod vendor
102
103 go list -f {{.Dir}} -tags tools -e all
104 stdout '^'$WORK'[/\\]auto$'
105 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
106 stdout '^'$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
111 stderr '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
114 stderr '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
124 rm go.mod
125 rm vendor/modules.txt
126
127 go mod init example.com/auto
128 go mod edit -go=1.22
129 go list -f {{.Dir}} -tags tools -e all
130 stdout '^'$WORK'[/\\]auto$'
131 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
132 stdout '^'$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
137 cp go.mod.orig go.mod
138 go mod edit -go=1.14
139 ! go list -f {{.Dir}} -tags tools -e all
140 stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
141 stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
142 stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
143 stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
144 stderr '^\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...
149 go mod edit -go=1.13
150 go list -mod=vendor -f {{.Dir}} -tags tools -e all
151 stdout '^'$WORK'[/\\]auto$'
152 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
153 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
154
155 # ...but a version mismatch for an explicit dependency should be noticed.
156 cp $WORK/modules-bad-1.13.txt vendor/modules.txt
157 ! go list -mod=vendor -f {{.Dir}} -tags tools all
158 stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
159 stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/printversion@v1.1.0$'
160 stderr '^\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.
164 go mod vendor
165 cmp $WORK/modules-1.13.txt vendor/modules.txt
166
167 go list -mod=vendor -f {{.Dir}} -tags tools -e all
168 stdout '^'$WORK'[/\\]auto$'
169 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
170 stdout '^'$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.
174 go mod edit -go=1.14
175 go mod vendor
176 cmp $WORK/modules-1.14.txt vendor/modules.txt
177
178 # Then, -mod=vendor should kick in automatically and succeed.
179 go list -f {{.Dir}} -tags tools -e all
180 stdout '^'$WORK'[/\\]auto$'
181 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
182 stdout '^'$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.
186 go get example.com/version@v1.1.0
187 ! go list -f {{.Dir}} -tags tools all
188 stderr '^go: inconsistent vendoring'
189
190 -- $WORK/auto/go.mod --
191 module example.com/auto
192
193 go 1.13
194
195 require example.com/printversion v1.0.0
196
197 replace (
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
205 package auto
206
207 import _ "example.com/printversion"
208 -- $WORK/auto/auto.go --
209 package auto
210 -- $WORK/auto/replacement-version/go.mod --
211 module example.com/version
212 -- $WORK/auto/replacement-version/version.go --
213 package version
214
215 const V = "v1.0.0-replaced"
216 -- $WORK/modules-1.14.txt --
217 # example.com/printversion v1.0.0
218 ## explicit
219 example.com/printversion
220 # example.com/version v1.0.0 => ./replacement-version
221 example.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
226 example.com/printversion
227 # example.com/version v1.0.0 => ./replacement-version
228 example.com/version
229 -- $WORK/modules-bad-1.13.txt --
230 # example.com/printversion v1.1.0
231 example.com/printversion
232 # example.com/version v1.1.0
233 example.com/version
234 -- $WORK/auto/vendor/example.com/printversion/go.mod --
235 module example.com/printversion
236
237 require example.com/version v1.0.0
238 replace example.com/version v1.0.0 => ../oops v0.0.0
239 exclude example.com/version v1.0.1
240 -- $WORK/auto/vendor/example.com/printversion/printversion.go --
241 package main
242
243 import (
244 "fmt"
245 "os"
246 "runtime/debug"
247
248 _ "example.com/version"
249 )
250
251 func 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 --
260 package version
261
262 const V = "v1.0.0-replaced"
263
View as plain text