...
1env GO111MODULE=on
2[short] skip
3
4# -mod=readonly must not resolve missing modules nor update go.mod
5env GOFLAGS=-mod=readonly
6go mod edit -fmt
7cp go.mod go.mod.empty
8! go list all
9stderr '^x.go:2:8: cannot find module providing package rsc\.io/quote: import lookup disabled by -mod=readonly'
10! stderr '\(\)' # If we don't have a reason for -mod=readonly, don't log an empty one.
11cmp go.mod go.mod.empty
12
13# -mod=readonly should be set by default.
14env GOFLAGS=
15! go list all
16stderr '^x.go:2:8: no required module provides package rsc\.io/quote; to add it:\n\tgo get rsc\.io/quote$'
17cmp go.mod go.mod.empty
18
19env GOFLAGS=-mod=readonly
20
21# update go.mod - go get allowed
22go get rsc.io/quote
23grep rsc.io/quote go.mod
24
25# update go.mod - go mod tidy allowed
26cp go.mod.empty go.mod
27go mod tidy
28cp go.mod go.mod.tidy
29
30# -mod=readonly must succeed once go.mod is up-to-date...
31go list all
32
33# ... even if it needs downloads
34go clean -modcache
35go list all
36
37# -mod=readonly must not cause 'go list -m' to fail.
38# (golang.org/issue/36478)
39go list -m all
40! stderr 'cannot query module'
41
42# -mod=readonly should reject inconsistent go.mod files
43# (ones that would be rewritten).
44go get rsc.io/sampler@v1.2.0
45go mod edit -require rsc.io/quote@v1.5.2
46cp go.mod go.mod.inconsistent
47! go list
48stderr 'go: updates to go.mod needed, disabled by -mod=readonly'
49cmp go.mod go.mod.inconsistent
50
51# We get a different message when -mod=readonly is used by default.
52env GOFLAGS=
53! go list
54stderr '^go: updates to go.mod needed; to update it:\n\tgo mod tidy'
55
56# However, it should not reject files missing a 'go' directive,
57# since that was not always required.
58cp go.mod.nogo go.mod
59go list all
60cmp go.mod go.mod.nogo
61
62# Nor should it reject files with redundant (not incorrect)
63# requirements.
64cp go.mod.redundant go.mod
65go list all
66cmp go.mod go.mod.redundant
67
68cp go.mod.indirect go.mod
69go list all
70cmp go.mod go.mod.indirect
71
72
73# If we identify a missing package as a dependency of some other package in the
74# main module, we should suggest 'go mod tidy' instead of resolving it.
75
76cp go.mod.untidy go.mod
77! go list all
78stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
79
80! go list -deps .
81stderr '^x.go:2:8: no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
82
83# However, if we didn't see an import from the main module, we should suggest
84# 'go get' instead, because we don't know whether 'go mod tidy' would add it.
85! go list rsc.io/quote
86stderr '^no required module provides package rsc.io/quote; to add it:\n\tgo get rsc.io/quote$'
87
88
89-- go.mod --
90module m
91
92go 1.16
93
94-- x.go --
95package x
96import _ "rsc.io/quote"
97-- go.mod.nogo --
98module m
99
100require (
101 rsc.io/quote v1.5.2
102 rsc.io/testonly v1.0.0 // indirect
103)
104-- go.mod.redundant --
105module m
106
107go 1.16
108
109require (
110 rsc.io/quote v1.5.2
111 rsc.io/sampler v1.3.0 // indirect
112 rsc.io/testonly v1.0.0 // indirect
113)
114-- go.mod.indirect --
115module m
116
117go 1.16
118
119require (
120 rsc.io/quote v1.5.2 // indirect
121 rsc.io/sampler v1.3.0 // indirect
122 rsc.io/testonly v1.0.0 // indirect
123)
124-- go.mod.untidy --
125module m
126
127go 1.16
128
129require (
130 rsc.io/sampler v1.3.0 // indirect
131)
View as plain text