1 env GO111MODULE=on
2
3 # Set go version so that we can test produced mod files for equality.
4 env TESTGO_VERSION=go1.26.0
5
6 # Test that go mod edits and related mod flags work.
7 # Also test that they can use a dummy name that isn't resolvable. golang.org/issue/24100
8
9 # go mod init
10 ! go mod init
11 stderr 'cannot determine module path'
12 ! exists go.mod
13
14 go mod init x.x/y/z
15 stderr 'creating new go.mod: module x.x/y/z'
16 cmp go.mod $WORK/go.mod.init
17
18 ! go mod init
19 cmp go.mod $WORK/go.mod.init
20
21 # go mod edits
22 go mod edit -droprequire=x.1 -require=x.1@v1.0.0 -require=x.2@v1.1.0 -droprequire=x.2 -exclude='x.1 @ v1.2.0' -exclude=x.1@v1.2.1 -exclude=x.1@v2.0.0+incompatible -replace=x.1@v1.3.0=y.1@v1.4.0 -replace='x.1@v1.4.0 = ../z' -retract=v1.6.0 -retract=[v1.1.0,v1.2.0] -retract=[v1.3.0,v1.4.0] -retract=v1.0.0
23 cmp go.mod $WORK/go.mod.edit1
24 go mod edit -droprequire=x.1 -dropexclude=x.1@v1.2.1 -dropexclude=x.1@v2.0.0+incompatible -dropreplace=x.1@v1.3.0 -require=x.3@v1.99.0 -dropretract=v1.0.0 -dropretract=[v1.1.0,v1.2.0]
25 cmp go.mod $WORK/go.mod.edit2
26
27 # -exclude and -retract reject invalid versions.
28 ! go mod edit -exclude=example.com/m@bad
29 stderr '^go: -exclude=example.com/m@bad: version "bad" invalid: must be of the form v1.2.3$'
30 ! go mod edit -retract=bad
31 stderr '^go: -retract=bad: version "bad" invalid: must be of the form v1.2.3$'
32
33 ! go mod edit -exclude=example.com/m@v2.0.0
34 stderr '^go: -exclude=example.com/m@v2\.0\.0: version "v2\.0\.0" invalid: should be v2\.0\.0\+incompatible \(or module example\.com/m/v2\)$'
35
36 ! go mod edit -exclude=example.com/m/v2@v1.0.0
37 stderr '^go: -exclude=example.com/m/v2@v1\.0\.0: version "v1\.0\.0" invalid: should be v2, not v1$'
38
39 ! go mod edit -exclude=gopkg.in/example.v1@v2.0.0
40 stderr '^go: -exclude=gopkg\.in/example\.v1@v2\.0\.0: version "v2\.0\.0" invalid: should be v1, not v2$'
41
42 cmp go.mod $WORK/go.mod.edit2
43
44 # go mod edit -json
45 go mod edit -json
46 cmp stdout $WORK/go.mod.json
47
48 # go mod edit -json (retractions with rationales)
49 go mod edit -json $WORK/go.mod.retractrationale
50 cmp stdout $WORK/go.mod.retractrationale.json
51
52 # go mod edit -json (deprecation)
53 go mod edit -json $WORK/go.mod.deprecation
54 cmp stdout $WORK/go.mod.deprecation.json
55
56 # go mod edit -json (empty mod file)
57 go mod edit -json $WORK/go.mod.empty
58 cmp stdout $WORK/go.mod.empty.json
59
60 # go mod edit -replace
61 go mod edit -replace=x.1@v1.3.0=y.1/v2@v2.3.5 -replace=x.1@v1.4.0=y.1/v2@v2.3.5
62 cmp go.mod $WORK/go.mod.edit3
63 go mod edit -replace=x.1=y.1/v2@v2.3.6
64 cmp go.mod $WORK/go.mod.edit4
65 go mod edit -dropreplace=x.1
66 cmp go.mod $WORK/go.mod.edit5
67 go mod edit -replace=x.1=../y.1/@v2
68 cmp go.mod $WORK/go.mod.edit6
69 ! go mod edit -replace=x.1=y.1/@v2
70 stderr '^go: -replace=x.1=y.1/@v2: invalid new path: malformed import path "y.1/": trailing slash$'
71
72 # go mod edit -fmt
73 cp $WORK/go.mod.badfmt go.mod
74 go mod edit -fmt -print # -print should avoid writing file
75 cmp stdout $WORK/go.mod.goodfmt
76 cmp go.mod $WORK/go.mod.badfmt
77 go mod edit -fmt # without -print, should write file (and nothing to stdout)
78 ! stdout .
79 cmp go.mod $WORK/go.mod.goodfmt
80
81 # go mod edit -module
82 cd $WORK/m
83 go mod init a.a/b/c
84 go mod edit -module x.x/y/z
85 cmp go.mod go.mod.edit
86
87 # golang.org/issue/30513: don't require go-gettable module paths.
88 cd $WORK/local
89 go mod init foo
90 go mod edit -module local-only -require=other-local@v1.0.0 -replace other-local@v1.0.0=./other
91 cmp go.mod go.mod.edit
92
93 # go mod edit -godebug
94 cd $WORK/g
95 cp go.mod.start go.mod
96 go mod edit -godebug key=value
97 cmp go.mod go.mod.edit
98 go mod edit -dropgodebug key2
99 cmp go.mod go.mod.edit
100 go mod edit -dropgodebug key
101 cmp go.mod go.mod.start
102
103 # go mod edit -tool
104 cd $WORK/h
105 cp go.mod.start go.mod
106 go mod edit -tool example.com/tool
107 cmp go.mod go.mod.edit
108 go mod edit -droptool example.com/tool2
109 cmp go.mod go.mod.edit
110 go mod edit -droptool example.com/tool
111 cmp go.mod go.mod.start
112
113 # go mod edit -ignore
114 cd $WORK/i
115 cp go.mod.start go.mod
116 go mod edit -ignore example.com/ignore
117 cmp go.mod go.mod.edit
118 go mod edit -dropignore example.com/ignore2
119 cmp go.mod go.mod.edit
120 go mod edit -dropignore example.com/ignore
121 cmp go.mod go.mod.start
122
123 -- x.go --
124 package x
125
126 -- w/w.go --
127 package w
128
129 -- $WORK/go.mod.init --
130 module x.x/y/z
131
132 go 1.25.0
133 -- $WORK/go.mod.edit1 --
134 module x.x/y/z
135
136 go 1.25.0
137
138 require x.1 v1.0.0
139
140 exclude (
141 x.1 v1.2.0
142 x.1 v1.2.1
143 x.1 v2.0.0+incompatible
144 )
145
146 replace (
147 x.1 v1.3.0 => y.1 v1.4.0
148 x.1 v1.4.0 => ../z
149 )
150
151 retract (
152 v1.6.0
153 [v1.3.0, v1.4.0]
154 [v1.1.0, v1.2.0]
155 v1.0.0
156 )
157 -- $WORK/go.mod.edit2 --
158 module x.x/y/z
159
160 go 1.25.0
161
162 exclude x.1 v1.2.0
163
164 replace x.1 v1.4.0 => ../z
165
166 retract (
167 v1.6.0
168 [v1.3.0, v1.4.0]
169 )
170
171 require x.3 v1.99.0
172 -- $WORK/go.mod.json --
173 {
174 "Module": {
175 "Path": "x.x/y/z"
176 },
177 "Go": "1.25.0",
178 "Require": [
179 {
180 "Path": "x.3",
181 "Version": "v1.99.0"
182 }
183 ],
184 "Exclude": [
185 {
186 "Path": "x.1",
187 "Version": "v1.2.0"
188 }
189 ],
190 "Replace": [
191 {
192 "Old": {
193 "Path": "x.1",
194 "Version": "v1.4.0"
195 },
196 "New": {
197 "Path": "../z"
198 }
199 }
200 ],
201 "Retract": [
202 {
203 "Low": "v1.6.0",
204 "High": "v1.6.0"
205 },
206 {
207 "Low": "v1.3.0",
208 "High": "v1.4.0"
209 }
210 ],
211 "Tool": null,
212 "Ignore": null
213 }
214 -- $WORK/go.mod.edit3 --
215 module x.x/y/z
216
217 go 1.25.0
218
219 exclude x.1 v1.2.0
220
221 replace (
222 x.1 v1.3.0 => y.1/v2 v2.3.5
223 x.1 v1.4.0 => y.1/v2 v2.3.5
224 )
225
226 retract (
227 v1.6.0
228 [v1.3.0, v1.4.0]
229 )
230
231 require x.3 v1.99.0
232 -- $WORK/go.mod.edit4 --
233 module x.x/y/z
234
235 go 1.25.0
236
237 exclude x.1 v1.2.0
238
239 replace x.1 => y.1/v2 v2.3.6
240
241 retract (
242 v1.6.0
243 [v1.3.0, v1.4.0]
244 )
245
246 require x.3 v1.99.0
247 -- $WORK/go.mod.edit5 --
248 module x.x/y/z
249
250 go 1.25.0
251
252 exclude x.1 v1.2.0
253
254 retract (
255 v1.6.0
256 [v1.3.0, v1.4.0]
257 )
258
259 require x.3 v1.99.0
260 -- $WORK/go.mod.edit6 --
261 module x.x/y/z
262
263 go 1.25.0
264
265 exclude x.1 v1.2.0
266
267 retract (
268 v1.6.0
269 [v1.3.0, v1.4.0]
270 )
271
272 require x.3 v1.99.0
273
274 replace x.1 => ../y.1/@v2
275 -- $WORK/local/go.mod.edit --
276 module local-only
277
278 go 1.25.0
279
280 require other-local v1.0.0
281
282 replace other-local v1.0.0 => ./other
283 -- $WORK/go.mod.badfmt --
284 module x.x/y/z
285
286 go 1.10
287
288 exclude x.1 v1.2.0
289
290 replace x.1 => y.1/v2 v2.3.6
291
292 require x.3 v1.99.0
293
294 retract [ "v1.8.1" , "v1.8.2" ]
295 -- $WORK/go.mod.goodfmt --
296 module x.x/y/z
297
298 go 1.10
299
300 exclude x.1 v1.2.0
301
302 replace x.1 => y.1/v2 v2.3.6
303
304 require x.3 v1.99.0
305
306 retract [v1.8.1, v1.8.2]
307 -- $WORK/m/go.mod.edit --
308 module x.x/y/z
309
310 go 1.25.0
311 -- $WORK/go.mod.retractrationale --
312 module x.x/y/z
313
314 go 1.15
315
316 // a
317 retract v1.0.0
318
319 // b
320 retract (
321 v1.0.1
322 v1.0.2 // c
323 )
324 -- $WORK/go.mod.retractrationale.json --
325 {
326 "Module": {
327 "Path": "x.x/y/z"
328 },
329 "Go": "1.15",
330 "Require": null,
331 "Exclude": null,
332 "Replace": null,
333 "Retract": [
334 {
335 "Low": "v1.0.0",
336 "High": "v1.0.0",
337 "Rationale": "a"
338 },
339 {
340 "Low": "v1.0.1",
341 "High": "v1.0.1",
342 "Rationale": "b"
343 },
344 {
345 "Low": "v1.0.2",
346 "High": "v1.0.2",
347 "Rationale": "c"
348 }
349 ],
350 "Tool": null,
351 "Ignore": null
352 }
353 -- $WORK/go.mod.deprecation --
354 // Deprecated: and the new one is not ready yet
355 module m
356 -- $WORK/go.mod.deprecation.json --
357 {
358 "Module": {
359 "Path": "m",
360 "Deprecated": "and the new one is not ready yet"
361 },
362 "Require": null,
363 "Exclude": null,
364 "Replace": null,
365 "Retract": null,
366 "Tool": null,
367 "Ignore": null
368 }
369 -- $WORK/go.mod.empty --
370 -- $WORK/go.mod.empty.json --
371 {
372 "Module": {
373 "Path": ""
374 },
375 "Require": null,
376 "Exclude": null,
377 "Replace": null,
378 "Retract": null,
379 "Tool": null,
380 "Ignore": null
381 }
382 -- $WORK/g/go.mod.start --
383 module g
384
385 go 1.10
386 -- $WORK/g/go.mod.edit --
387 module g
388
389 go 1.10
390
391 godebug key=value
392 -- $WORK/h/go.mod.start --
393 module g
394
395 go 1.24
396 -- $WORK/h/go.mod.edit --
397 module g
398
399 go 1.24
400
401 tool example.com/tool
402 -- $WORK/i/go.mod.start --
403 module g
404
405 go 1.24
406 -- $WORK/i/go.mod.edit --
407 module g
408
409 go 1.24
410
411 ignore example.com/ignore
412
View as plain text