...
1env 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.
8go env GOMOD
9stdout '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
15stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
16go list -m
17stdout '^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
21stderr '^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.
25go list all
26! stdout .
27stderr '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.
31go list -deps cmd
32stdout '^fmt$'
33stdout '^cmd/go$'
34
35go list $GOROOT/src/fmt
36stdout '^fmt$'
37
38# 'go list' should work with file arguments.
39go list ./needmod/needmod.go
40stdout 'command-line-arguments'
41
42# 'go list' on a package from a module should fail.
43! go list example.com/printversion
44stderr '^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.
48go list -m example.com/version@latest
49stdout 'example.com/version v1.1.0'
50
51# 'go list -m -versions' should succeed even without an explicit version.
52go list -m -versions example.com/version
53stdout '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
57stderr '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
61stderr '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
66stderr '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 ...
72stderr '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/...
74stderr '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.
78go clean -n
79! stdout .
80! stderr .
81
82# 'go mod graph' should fail, since there's no module graph.
83! go mod graph
84stderr '^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
88stderr '^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
93stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
94! go mod edit -fmt
95stderr '^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
97stderr '^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
102stderr 'no modules specified'
103
104# 'go mod download' should download exactly the requested module without dependencies.
105rm -r $GOPATH/pkg/mod/cache/download/example.com
106go mod download example.com/printversion@v1.0.0
107exists $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
112stderr '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
118stderr '^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
123stderr '^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
128stderr '^go: go.mod file not found in current directory or any parent directory.$'
129stderr '^\t''go get'' is no longer supported outside a module.$'
130! go get -u
131stderr '^go: go.mod file not found in current directory or any parent directory.$'
132stderr '^\t''go get'' is no longer supported outside a module.$'
133! go get -u ./needmod
134stderr '^go: go.mod file not found in current directory or any parent directory.$'
135stderr '^\t''go get'' is no longer supported outside a module.$'
136! go get -u all
137stderr '^go: go.mod file not found in current directory or any parent directory.$'
138stderr '^\t''go get'' is no longer supported outside a module.$'
139! go get example.com/printversion@v1.0.0 example.com/version@none
140stderr '^go: go.mod file not found in current directory or any parent directory.$'
141stderr '^\t''go get'' is no longer supported outside a module.$'
142
143# 'go get' should not download anything.
144go clean -modcache
145! go get example.com/printversion@v1.0.0
146stderr '^go: go.mod file not found in current directory or any parent directory.$'
147stderr '^\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.
153cd needmod
154! go build
155stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
156cd ..
157
158# 'go build' of a non-module directory should fail too.
159! go build ./needmod
160stderr '^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
164stderr '^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.
167go build -n -o ignore ./stdonly/stdonly.go
168
169# 'go build' should succeed for standard-library packages.
170go build -n fmt
171
172# 'go build' should use the latest version of the Go language.
173go build ./newgo/newgo.go
174
175# 'go doc' without arguments implicitly operates on the current directory, and should fail.
176# TODO(golang.org/issue/32027): currently, it succeeds.
177cd needmod
178go doc
179cd ..
180
181# 'go doc' of a non-module directory should also succeed.
182go doc ./needmod
183
184# 'go doc' should succeed for standard-library packages.
185go doc fmt
186
187# 'go doc' should fail for a package path outside a module.
188! go doc example.com/version
189stderr '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''$'
190
191# 'go install' with a version should succeed if all constraints are met.
192# See mod_install_pkg_version.
193rm $GOPATH/bin
194go install example.com/printversion@v0.1.0
195exists $GOPATH/bin/printversion$GOEXE
196
197# 'go install' should fail if a package argument must be resolved to a module.
198! go install example.com/printversion
199stderr '^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$'
200
201# 'go install' should fail if a source file imports a package that must be
202# resolved to a module.
203! go install ./needmod/needmod.go
204stderr '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''$'
205
206# 'go run' should fail if a package argument must be resolved to a module.
207! go run example.com/printversion
208stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
209
210# 'go run' should fail if a source file imports a package that must be
211# resolved to a module.
212! go run ./needmod/needmod.go
213stderr '^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''$'
214
215
216# 'go fmt' should be able to format files outside of a module.
217go fmt needmod/needmod.go
218
219
220# The remainder of the test checks dependencies by linking and running binaries.
221
222# 'go run' should work with file arguments if they don't import anything
223# outside std.
224go run ./stdonly/stdonly.go
225stdout 'path is command-line-arguments$'
226stdout 'main is $'
227
228# 'go generate' should work with file arguments.
229[exec:touch] go generate ./needmod/needmod.go
230[exec:touch] exists ./needmod/gen.txt
231
232# 'go install' should work with file arguments.
233go install ./stdonly/stdonly.go
234
235# 'go test' should work with file arguments.
236go test -v ./stdonly/stdonly_test.go
237stdout 'stdonly was tested'
238
239# 'go vet' should work with file arguments.
240go vet ./stdonly/stdonly.go
241
242
243-- README.txt --
244There is no go.mod file in the working directory.
245
246-- needmod/needmod.go --
247//go:generate touch gen.txt
248
249package main
250
251import (
252 "fmt"
253 "os"
254 "runtime/debug"
255
256 _ "example.com/version"
257)
258
259func main() {
260 info, ok := debug.ReadBuildInfo()
261 if !ok {
262 panic("missing build info")
263 }
264 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
265 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
266 for _, m := range info.Deps {
267 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
268 }
269}
270
271-- stdonly/stdonly.go --
272package main
273
274import (
275 "fmt"
276 "os"
277 "runtime/debug"
278)
279
280func main() {
281 info, ok := debug.ReadBuildInfo()
282 if !ok {
283 panic("missing build info")
284 }
285 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
286 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
287 for _, m := range info.Deps {
288 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
289 }
290}
291
292-- stdonly/stdonly_test.go --
293package main
294
295import (
296 "fmt"
297 "testing"
298)
299
300func Test(t *testing.T) {
301 fmt.Println("stdonly was tested")
302}
303
304-- newgo/newgo.go --
305// Package newgo requires Go 1.14 or newer.
306package newgo
307
308import "io"
309
310const C = 299_792_458
311
312type ReadWriteCloser interface {
313 io.ReadCloser
314 io.WriteCloser
315}
View as plain text