1 env GO111MODULE=on
2 [short] skip
3
4 # This script tests commands in module mode outside of any module.
5 #
6 # First, ensure that we really are in module mode, and that we really don't have
7 # a go.mod file.
8 go env GOMOD
9 stdout 'NUL|/dev/null'
10
11
12 # 'go list' without arguments implicitly operates on the current directory,
13 # which is not in a module.
14 ! go list
15 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
16 go list -m
17 stdout '^command-line-arguments$'
18 # 'go list' in the working directory should fail even if there is a a 'package
19 # main' present: without a main module, we do not know its package path.
20 ! go list ./needmod
21 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
22
23 # 'go list all' lists the transitive import graph of the main module,
24 # which is empty if there is no main module.
25 go list all
26 ! stdout .
27 stderr 'warning: "all" matched no packages'
28
29 # 'go list' on standard-library packages should work, since they do not depend
30 # on the contents of any module.
31 go list -deps cmd
32 stdout '^fmt$'
33 stdout '^cmd/go$'
34
35 go list $GOROOT/src/fmt
36 stdout '^fmt$'
37
38 # 'go list' should work with file arguments.
39 go list ./needmod/needmod.go
40 stdout 'command-line-arguments'
41
42 # 'go list' on a package from a module should fail.
43 ! go list example.com/printversion
44 stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
45
46
47 # 'go list -m' with an explicit version should resolve that version.
48 go list -m example.com/version@latest
49 stdout 'example.com/version v1.1.0'
50
51 # 'go list -m -versions' should succeed even without an explicit version.
52 go list -m -versions example.com/version
53 stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'
54
55 # 'go list -m all' should fail. "all" is not meaningful outside of a module.
56 ! go list -m all
57 stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
58
59 # 'go list -m <mods> all' should also fail.
60 ! go list -m example.com/printversion@v1.0.0 all
61 stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
62 ! stdout 'example.com/version'
63
64 # 'go list -m <mods>' should fail if any of the mods lacks an explicit version.
65 ! go list -m example.com/printversion
66 stderr 'go: cannot match "example.com/printversion" without -versions or an explicit version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
67 ! stdout 'example.com/version'
68
69 # 'go list -m' with wildcards should fail. Wildcards match modules in the
70 # build list, so they aren't meaningful outside a module.
71 ! go list -m ...
72 stderr 'go: cannot match "...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
73 ! go list -m rsc.io/quote/...
74 stderr 'go: cannot match "rsc.io/quote/...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
75
76
77 # 'go clean' should skip the current directory if it isn't in a module.
78 go clean -n
79 ! stdout .
80 ! stderr .
81
82 # 'go mod graph' should fail, since there's no module graph.
83 ! go mod graph
84 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
85
86 # 'go mod why' should fail, since there is no main module to depend on anything.
87 ! go mod why -m example.com/version
88 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
89
90 # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
91 # there is no go.mod file to edit.
92 ! go mod tidy
93 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
94 ! go mod edit -fmt
95 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
96 ! go mod edit -require example.com/version@v1.0.0
97 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
98
99
100 # 'go mod download' without arguments should report an error.
101 ! go mod download
102 stderr 'no modules specified'
103
104 # 'go mod download' should download exactly the requested module without dependencies.
105 rm -r $GOPATH/pkg/mod/cache/download/example.com
106 go mod download example.com/printversion@v1.0.0
107 exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
108 ! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip
109
110 # 'go mod download all' should fail. "all" is not meaningful outside of a module.
111 ! go mod download all
112 stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
113
114
115 # 'go mod vendor' should fail: it starts by clearing the existing vendor
116 # directory, and we don't know where that is.
117 ! go mod vendor
118 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
119
120
121 # 'go mod verify' should fail: we have no modules to verify.
122 ! go mod verify
123 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
124
125
126 # 'go get' has no go.mod file to update outside a module and should fail.
127 ! go get
128 stderr '^go: go.mod file not found in current directory or any parent directory.$'
129 stderr '^\t''go get'' is no longer supported outside a module.$'
130 ! go get -u
131 stderr '^go: go.mod file not found in current directory or any parent directory.$'
132 stderr '^\t''go get'' is no longer supported outside a module.$'
133 ! go get -u ./needmod
134 stderr '^go: go.mod file not found in current directory or any parent directory.$'
135 stderr '^\t''go get'' is no longer supported outside a module.$'
136 ! go get -u all
137 stderr '^go: go.mod file not found in current directory or any parent directory.$'
138 stderr '^\t''go get'' is no longer supported outside a module.$'
139 ! go get example.com/printversion@v1.0.0 example.com/version@none
140 stderr '^go: go.mod file not found in current directory or any parent directory.$'
141 stderr '^\t''go get'' is no longer supported outside a module.$'
142
143 # 'go get' should not download anything.
144 go clean -modcache
145 ! go get example.com/printversion@v1.0.0
146 stderr '^go: go.mod file not found in current directory or any parent directory.$'
147 stderr '^\t''go get'' is no longer supported outside a module.$'
148 ! exists $GOPATH/pkg/mod/example.com/printversion@v1.0.0
149 ! exists $GOPATH/pkg/mod/example.com/version@v1.0.0
150
151
152 # 'go build' without arguments implicitly operates on the current directory, and should fail.
153 cd needmod
154 ! go build
155 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
156 cd ..
157
158 # 'go build' of a non-module directory should fail too.
159 ! go build ./needmod
160 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
161
162 # 'go build' of source files should fail if they import anything outside std.
163 ! go build -n ./needmod/needmod.go
164 stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
165
166 # 'go build' of source files should succeed if they do not import anything outside std.
167 go build -n -o ignore ./stdonly/stdonly.go
168
169 # 'go build' should succeed for standard-library packages.
170 go build -n fmt
171
172 # 'go build' should use the latest version of the Go language.
173 go build ./newgo/newgo.go
174
175 # 'go doc' without arguments implicitly operates on the current directory, and should fail.
176 cd needmod
177 ! go doc
178 stderr '^doc: go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
179 cd ..
180
181 # 'go doc' of a non-module directory should also fail.
182 ! go doc ./needmod
183 stderr '^doc: go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
184
185
186 # 'go doc' should succeed for standard-library packages.
187 go doc fmt
188
189 # 'go doc' should fail for a package path outside a module.
190 ! go doc example.com/version
191 stderr 'doc: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
192
193 # 'go install' with a version should succeed if all constraints are met.
194 # See mod_install_pkg_version.
195 rm $GOPATH/bin
196 go install example.com/printversion@v0.1.0
197 exists $GOPATH/bin/printversion$GOEXE
198
199 # 'go install' should fail if a package argument must be resolved to a module.
200 ! go install example.com/printversion
201 stderr '^go: ''go install'' requires a version when current directory is not in a module\n\tTry ''go install example.com/printversion@latest'' to install the latest version$'
202
203 # 'go install' should fail if a source file imports a package that must be
204 # resolved to a module.
205 ! go install ./needmod/needmod.go
206 stderr 'needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
207
208 # 'go run' should fail if a package argument must be resolved to a module.
209 ! go run example.com/printversion
210 stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
211
212 # 'go run' should fail if a source file imports a package that must be
213 # resolved to a module.
214 ! go run ./needmod/needmod.go
215 stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
216
217
218 # 'go fmt' should be able to format files outside of a module.
219 go fmt needmod/needmod.go
220
221
222 # The remainder of the test checks dependencies by linking and running binaries.
223
224 # 'go run' should work with file arguments if they don't import anything
225 # outside std.
226 go run ./stdonly/stdonly.go
227 stdout 'path is command-line-arguments$'
228 stdout 'main is $'
229
230 # 'go generate' should work with file arguments.
231 [exec:touch] go generate ./needmod/needmod.go
232 [exec:touch] exists ./needmod/gen.txt
233
234 # 'go install' should work with file arguments.
235 go install ./stdonly/stdonly.go
236
237 # 'go test' should work with file arguments.
238 go test -v ./stdonly/stdonly_test.go
239 stdout 'stdonly was tested'
240
241 # 'go vet' should work with file arguments.
242 go vet ./stdonly/stdonly.go
243
244
245 -- README.txt --
246 There is no go.mod file in the working directory.
247
248 -- needmod/needmod.go --
249 //go:generate touch gen.txt
250
251 package main
252
253 import (
254 "fmt"
255 "os"
256 "runtime/debug"
257
258 _ "example.com/version"
259 )
260
261 func main() {
262 info, ok := debug.ReadBuildInfo()
263 if !ok {
264 panic("missing build info")
265 }
266 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
267 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
268 for _, m := range info.Deps {
269 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
270 }
271 }
272
273 -- stdonly/stdonly.go --
274 package main
275
276 import (
277 "fmt"
278 "os"
279 "runtime/debug"
280 )
281
282 func main() {
283 info, ok := debug.ReadBuildInfo()
284 if !ok {
285 panic("missing build info")
286 }
287 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
288 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
289 for _, m := range info.Deps {
290 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
291 }
292 }
293
294 -- stdonly/stdonly_test.go --
295 package main
296
297 import (
298 "fmt"
299 "testing"
300 )
301
302 func Test(t *testing.T) {
303 fmt.Println("stdonly was tested")
304 }
305
306 -- newgo/newgo.go --
307 // Package newgo requires Go 1.14 or newer.
308 package newgo
309
310 import "io"
311
312 const C = 299_792_458
313
314 type ReadWriteCloser interface {
315 io.ReadCloser
316 io.WriteCloser
317 }
318
View as plain text