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' without arguments implicitly operates on the main module, and thus
127 # should fail.
128 ! go get
129 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
130 ! go get -u
131 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
132 ! go get -u ./needmod
133 stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
134
135 # 'go get -u all' upgrades the transitive import graph of the main module,
136 # which is empty.
137 ! go get -u all
138 stderr '^go get: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
139
140 # 'go get' should check the proposed module graph for consistency,
141 # even though we won't write it anywhere.
142 ! go get -d example.com/printversion@v1.0.0 example.com/version@none
143 stderr '^go get: example.com/printversion@v1.0.0 requires example.com/version@v1.0.0, not example.com/version@none$'
144
145 # 'go get -d' should download and extract the source code needed to build the requested version.
146 rm -r $GOPATH/pkg/mod/example.com
147 go get -d example.com/printversion@v1.0.0
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 install: version is required 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 get' of a binary without a go.mod should install the requested version,
227 # resolving outside dependencies to the latest available versions.
228 go get example.com/printversion@v0.1.0
229 exec ../bin/printversion
230 stdout 'path is example.com/printversion'
231 stdout 'main is example.com/printversion v0.1.0'
232 stdout 'using example.com/version v1.1.0'
233
234 # 'go get' of a versioned binary should build and install the latest version
235 # using its minimal required modules, ignoring replacements and exclusions.
236 go get example.com/printversion
237 exec ../bin/printversion
238 stdout 'path is example.com/printversion'
239 stdout 'main is example.com/printversion v1.0.0'
240 stdout 'using example.com/version v1.0.0'
241
242 # 'go get -u=patch' should patch dependencies before installing,
243 # again ignoring replacements and exclusions.
244 go get -u=patch example.com/printversion@v1.0.0
245 exec ../bin/printversion
246 stdout 'path is example.com/printversion'
247 stdout 'main is example.com/printversion v1.0.0'
248 stdout 'using example.com/version v1.0.1'
249
250 # 'go run' should work with file arguments if they don't import anything
251 # outside std.
252 go run ./stdonly/stdonly.go
253 stdout 'path is command-line-arguments$'
254 stdout 'main is command-line-arguments \(devel\)'
255
256 # 'go generate' should work with file arguments.
257 [exec:touch] go generate ./needmod/needmod.go
258 [exec:touch] exists ./needmod/gen.txt
259
260 # 'go install' should work with file arguments.
261 go install ./stdonly/stdonly.go
262
263 # 'go test' should work with file arguments.
264 go test -v ./stdonly/stdonly_test.go
265 stdout 'stdonly was tested'
266
267 # 'go vet' should work with file arguments.
268 go vet ./stdonly/stdonly.go
269
270
271 -- README.txt --
272 There is no go.mod file in the working directory.
273
274 -- needmod/needmod.go --
275 //go:generate touch gen.txt
276
277 package main
278
279 import (
280 "fmt"
281 "os"
282 "runtime/debug"
283
284 _ "example.com/version"
285 )
286
287 func main() {
288 info, ok := debug.ReadBuildInfo()
289 if !ok {
290 panic("missing build info")
291 }
292 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
293 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
294 for _, m := range info.Deps {
295 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
296 }
297 }
298
299 -- stdonly/stdonly.go --
300 package main
301
302 import (
303 "fmt"
304 "os"
305 "runtime/debug"
306 )
307
308 func main() {
309 info, ok := debug.ReadBuildInfo()
310 if !ok {
311 panic("missing build info")
312 }
313 fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
314 fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
315 for _, m := range info.Deps {
316 fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
317 }
318 }
319
320 -- stdonly/stdonly_test.go --
321 package main
322
323 import (
324 "fmt"
325 "testing"
326 )
327
328 func Test(t *testing.T) {
329 fmt.Println("stdonly was tested")
330 }
331
332 -- newgo/newgo.go --
333 // Package newgo requires Go 1.14 or newer.
334 package newgo
335
336 import "io"
337
338 const C = 299_792_458
339
340 type ReadWriteCloser interface {
341 io.ReadCloser
342 io.WriteCloser
343 }
344
View as plain text