aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/mod_outside.txt
blob: e398f7bc40b0b4df528b8dbf21ee091ad3b2050c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
env GO111MODULE=on
[short] skip

# This script tests commands in module mode outside of any module.
#
# First, ensure that we really are in module mode, and that we really don't have
# a go.mod file.
go env GOMOD
stdout 'NUL|/dev/null'


# 'go list' without arguments implicitly operates on the current directory,
# which is not in a module.
! go list
stderr 'cannot find main module'
go list -m
stdout '^command-line-arguments$'
# 'go list' in the working directory should fail even if there is a a 'package
# main' present: without a main module, we do not know its package path.
! go list ./needmod
stderr 'cannot find main module'

# 'go list all' lists the transitive import graph of the main module,
# which is empty if there is no main module.
go list all
! stdout .
stderr 'warning: "all" matched no packages'

# 'go list' on standard-library packages should work, since they do not depend
# on the contents of any module.
go list -deps cmd
stdout '^fmt$'
stdout '^cmd/go$'

go list $GOROOT/src/fmt
stdout '^fmt$'

# 'go list' should work with file arguments.
go list ./needmod/needmod.go
stdout 'command-line-arguments'

# 'go list -m' with an explicit version should resolve that version.
go list -m example.com/version@latest
stdout 'example.com/version v1.1.0'

# 'go list -m -versions' should succeed even without an explicit version.
go list -m -versions example.com/version
stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'

# 'go list -m all' should fail. "all" is not meaningful outside of a module.
! go list -m all
stderr 'go: cannot match "all": working directory is not part of a module'

# 'go list -m <mods> all' should also fail.
! go list -m example.com/printversion@v1.0.0 all
stderr 'go: cannot match "all": working directory is not part of a module'
! stdout 'example.com/version'

# 'go list -m' with wildcards should fail. Wildcards match modules in the
# build list, so they aren't meaningful outside a module.
! go list -m ...
stderr 'go: cannot match "...": working directory is not part of a module'
! go list -m rsc.io/quote/...
stderr 'go: cannot match "rsc.io/quote/...": working directory is not part of a module'


# 'go clean' should skip the current directory if it isn't in a module.
go clean -n
! stdout .
! stderr .

# 'go mod graph' should fail, since there's no module graph.
! go mod graph
stderr 'cannot find main module'

# 'go mod why' should fail, since there is no main module to depend on anything.
! go mod why -m example.com/version
stderr 'cannot find main module'

# 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
# there is no go.mod file to edit.
! go mod tidy
stderr 'cannot find main module'
! go mod edit -fmt
stderr 'cannot find main module'
! go mod edit -require example.com/version@v1.0.0
stderr 'cannot find main module'


# 'go mod download' without arguments should report an error.
! go mod download
stderr 'no modules specified'

# 'go mod download' should download exactly the requested module without dependencies.
rm -r $GOPATH/pkg/mod/cache/download/example.com
go mod download example.com/printversion@v1.0.0
exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip

# 'go mod download all' should fail. "all" is not meaningful outside of a module.
! go mod download all
stderr 'go: cannot match "all": working directory is not part of a module'


# 'go mod vendor' should fail: it starts by clearing the existing vendor
# directory, and we don't know where that is.
! go mod vendor
stderr 'cannot find main module'


# 'go mod verify' should fail: we have no modules to verify.
! go mod verify
stderr 'cannot find main module'


# 'go get' without arguments implicitly operates on the main module, and thus
# should fail.
! go get
stderr 'cannot find main module'
! go get -u
stderr 'cannot find main module'
! go get -u ./needmod
stderr 'cannot find main module'

# 'go get -u all' upgrades the transitive import graph of the main module,
# which is empty.
! go get -u all
stderr 'go get all: cannot match "all": working directory is not part of a module'

# 'go get' should check the proposed module graph for consistency,
# even though we won't write it anywhere.
! go get -d example.com/printversion@v1.0.0 example.com/version@none
stderr 'inconsistent versions'

# 'go get -d' should download and extract the source code needed to build the requested version.
rm -r $GOPATH/pkg/mod/example.com
go get -d example.com/printversion@v1.0.0
exists $GOPATH/pkg/mod/example.com/printversion@v1.0.0
exists $GOPATH/pkg/mod/example.com/version@v1.0.0


# 'go build' without arguments implicitly operates on the current directory, and should fail.
cd needmod
! go build
stderr 'cannot find main module'
cd ..

# 'go build' of a non-module directory should fail too.
! go build ./needmod
stderr 'cannot find main module'

# 'go build' of source files should fail if they import anything outside std.
! go build -n ./needmod/needmod.go
stderr 'needmod[/\\]needmod.go:10:2: cannot find module providing package example.com/version: working directory is not part of a module'

# 'go build' of source files should succeed if they do not import anything outside std.
go build -n -o ignore ./stdonly/stdonly.go

# 'go build' should succeed for standard-library packages.
go build -n fmt


# 'go doc' without arguments implicitly operates on the current directory, and should fail.
# TODO(golang.org/issue/32027): currently, it succeeds.
cd needmod
go doc
cd ..

# 'go doc' of a non-module directory should also succeed.
go doc ./needmod

# 'go doc' should succeed for standard-library packages.
go doc fmt

# 'go doc' should fail for a package path outside a module.
! go doc example.com/version
stderr 'doc: cannot find module providing package example.com/version: working directory is not part of a module'

# 'go install' with a version should succeed if all constraints are met.
# See mod_install_pkg_version.
rm $GOPATH/bin
go install example.com/printversion@v0.1.0
exists $GOPATH/bin/printversion$GOEXE

# 'go install' should fail if a package argument must be resolved to a module.
! go install example.com/printversion
stderr 'cannot find module providing package example.com/printversion: working directory is not part of a module'

# 'go install' should fail if a source file imports a package that must be
# resolved to a module.
! go install ./needmod/needmod.go
stderr 'needmod[/\\]needmod.go:10:2: cannot find module providing package example.com/version: working directory is not part of a module'


# 'go run' with a verison should fail due to syntax.
! go run example.com/printversion@v1.0.0
stderr 'can only use path@version syntax with'

# 'go run' should fail if a package argument must be resolved to a module.
! go run example.com/printversion
stderr 'cannot find module providing package example.com/printversion: working directory is not part of a module'

# 'go run' should fail if a source file imports a package that must be
# resolved to a module.
! go run ./needmod/needmod.go
stderr 'needmod[/\\]needmod.go:10:2: cannot find module providing package example.com/version: working directory is not part of a module'


# 'go fmt' should be able to format files outside of a module.
go fmt needmod/needmod.go


# The remainder of the test checks dependencies by linking and running binaries.

# 'go get' of a binary without a go.mod should install the requested version,
# resolving outside dependencies to the latest available versions.
go get example.com/printversion@v0.1.0
exec ../bin/printversion
stdout 'path is example.com/printversion'
stdout 'main is example.com/printversion v0.1.0'
stdout 'using example.com/version v1.1.0'

# 'go get' of a versioned binary should build and install the latest version
# using its minimal module requirements, ignoring replacements and exclusions.
go get example.com/printversion
exec ../bin/printversion
stdout 'path is example.com/printversion'
stdout 'main is example.com/printversion v1.0.0'
stdout 'using example.com/version v1.0.0'

# 'go get -u=patch' should patch dependencies before installing,
# again ignoring replacements and exclusions.
go get -u=patch example.com/printversion@v1.0.0
exec ../bin/printversion
stdout 'path is example.com/printversion'
stdout 'main is example.com/printversion v1.0.0'
stdout 'using example.com/version v1.0.1'

# 'go run' should work with file arguments if they don't import anything
# outside std.
go run ./stdonly/stdonly.go
stdout 'path is command-line-arguments$'
stdout 'main is command-line-arguments \(devel\)'

# 'go generate' should work with file arguments.
[exec:touch] go generate ./needmod/needmod.go
[exec:touch] exists ./needmod/gen.txt

# 'go install' should work with file arguments.
go install ./stdonly/stdonly.go

# 'go test' should work with file arguments.
go test -v ./stdonly/stdonly_test.go
stdout 'stdonly was tested'

# 'go vet' should work with file arguments.
go vet ./stdonly/stdonly.go


-- README.txt --
There is no go.mod file in the working directory.

-- needmod/needmod.go --
//go:generate touch gen.txt

package main

import (
	"fmt"
	"os"
	"runtime/debug"

	_ "example.com/version"
)

func main() {
	info, ok := debug.ReadBuildInfo()
	if !ok {
		panic("missing build info")
	}
	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
	for _, m := range info.Deps {
		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
	}
}

-- stdonly/stdonly.go --
package main

import (
	"fmt"
	"os"
	"runtime/debug"
)

func main() {
	info, ok := debug.ReadBuildInfo()
	if !ok {
		panic("missing build info")
	}
	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
	for _, m := range info.Deps {
		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
	}
}

-- stdonly/stdonly_test.go --
package main

import (
	"fmt"
	"testing"
)

func Test(t *testing.T) {
	fmt.Println("stdonly was tested")
}