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 # TODO(golang.org/issue/32027): currently, it succeeds.
177 cd needmod
178 go doc
179 cd ..
180
181 # 'go doc' of a non-module directory should also succeed.
182 go doc ./needmod
183
184 # 'go doc' should succeed for standard-library packages.
185 go doc fmt
186
187 # 'go doc' should fail for a package path outside a module.
188 ! go doc example.com/version
189 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''$'
190
191 # 'go install' with a version should succeed if all constraints are met.
192 # See mod_install_pkg_version.
193 rm $GOPATH/bin
194 go install example.com/printversion@v0.1.0
195 exists $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
199 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$'
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
204 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''$'
205
206 # 'go install' should succeed with a package in GOROOT.
207 go install cmd/addr2line
208 ! stderr .
209
210 # 'go run' should fail if a package argument must be resolved to a module.
211 ! go run example.com/printversion
212 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''$'
213
214 # 'go run' should fail if a source file imports a package that must be
215 # resolved to a module.
216 ! go run ./needmod/needmod.go
217 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''$'
218
219
220 # 'go fmt' should be able to format files outside of a module.
221 go fmt needmod/needmod.go
222
223
224 # The remainder of the test checks dependencies by linking and running binaries.
225
226 # 'go run' should work with file arguments if they don't import anything
227 # outside std.
228 go run ./stdonly/stdonly.go
229 stdout 'path is command-line-arguments$'
230 stdout 'main is $'
231
232 # 'go generate' should work with file arguments.
233 [exec:touch] go generate ./needmod/needmod.go
234 [exec:touch] exists ./needmod/gen.txt
235
236 # 'go install' should work with file arguments.
237 go install ./stdonly/stdonly.go
238
239 # 'go test' should work with file arguments.
240 go test -v ./stdonly/stdonly_test.go
241 stdout 'stdonly was tested'
242
243 # 'go vet' should work with file arguments.
244 go vet ./stdonly/stdonly.go
245
246
247 -- README.txt --
248 There is no go.mod file in the working directory.
249
250 -- needmod/needmod.go --
251 //go:generate touch gen.txt
252
253 package main
254
255 import (
256 "fmt"
257 "os"
258 "runtime/debug"
259
260 _ "example.com/version"
261 )
262
263 func main() {
264 info, ok := debug.ReadBuildInfo()
265 if !ok {
266 panic("missing build info")
267 }
268 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
269 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
270 for _, m := range info.Deps {
271 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
272 }
273 }
274
275 -- stdonly/stdonly.go --
276 package main
277
278 import (
279 "fmt"
280 "os"
281 "runtime/debug"
282 )
283
284 func main() {
285 info, ok := debug.ReadBuildInfo()
286 if !ok {
287 panic("missing build info")
288 }
289 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
290 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
291 for _, m := range info.Deps {
292 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
293 }
294 }
295
296 -- stdonly/stdonly_test.go --
297 package main
298
299 import (
300 "fmt"
301 "testing"
302 )
303
304 func Test(t *testing.T) {
305 fmt.Println("stdonly was tested")
306 }
307
308 -- newgo/newgo.go --
309 // Package newgo requires Go 1.14 or newer.
310 package newgo
311
312 import "io"
313
314 const C = 299_792_458
315
316 type ReadWriteCloser interface {
317 io.ReadCloser
318 io.WriteCloser
319 }
320
View as plain text