Skip to content

Commit 4be75fa

Browse files
author
Jay Conrod
committed
cmd/go: make fewer 'go mod' commands update go.mod
'go mod graph', 'go mod vendor', 'go mod verify', and 'go mod why' will no longer edit go.mod or go.sum. 'go mod graph', 'go mod verify', and 'go mod why' may still fetch files and look up packages as if they were able to update go.mod. They're useful for debugging and should still work when go.mod is a little broken. This is implemented in modload.setDefaultBuildMod based on command name for now. Super gross. Sorry. This should be fixed with a larger refactoring for #40775. Fixes #45551 Change-Id: If5f225937180d32e9a5dd252c78d988042bbdedf Reviewed-on: https://go-review.googlesource.com/c/go/+/336151 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/341933 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 1fffedd commit 4be75fa

13 files changed

+61
-27
lines changed

src/cmd/go/internal/modload/init.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,25 @@ func setDefaultBuildMod() {
686686
return
687687
}
688688

689-
if cfg.CmdName == "get" || strings.HasPrefix(cfg.CmdName, "mod ") {
690-
// 'get' and 'go mod' commands may update go.mod automatically.
691-
// TODO(jayconrod): should this narrower? Should 'go mod download' or
692-
// 'go mod graph' update go.mod by default?
689+
// TODO(#40775): commands should pass in the module mode as an option
690+
// to modload functions instead of relying on an implicit setting
691+
// based on command name.
692+
switch cfg.CmdName {
693+
case "get", "mod download", "mod init", "mod tidy":
694+
// These commands are intended to update go.mod and go.sum.
693695
cfg.BuildMod = "mod"
694696
return
697+
case "mod graph", "mod verify", "mod why":
698+
// These commands should not update go.mod or go.sum, but they should be
699+
// able to fetch modules not in go.sum and should not report errors if
700+
// go.mod is inconsistent. They're useful for debugging, and they need
701+
// to work in buggy situations.
702+
cfg.BuildMod = "mod"
703+
allowWriteGoMod = false
704+
return
705+
case "mod vendor":
706+
cfg.BuildMod = "readonly"
707+
return
695708
}
696709
if modRoot == "" {
697710
if allowMissingModuleImports {

src/cmd/go/testdata/script/mod_all.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ go 1.15
315315

316316
require (
317317
example.com/a v0.1.0
318-
example.com/b v0.1.0
318+
example.com/b v0.1.0 // indirect
319319
example.com/q v0.1.0
320320
example.com/r v0.1.0 // indirect
321321
example.com/t v0.1.0

src/cmd/go/testdata/script/mod_e.txt

+15-8
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ cmp go.mod.orig go.mod
2424

2525
! go mod vendor
2626

27-
stderr '^example.com/untidy imports\n\texample.net/directnotfound: cannot find module providing package example.net/directnotfound: module example.net/directnotfound: reading http://.*: 404 Not Found$'
27+
stderr '^example.com/untidy imports\n\texample.net/directnotfound: no required module provides package example.net/directnotfound; to add it:\n\tgo get example.net/directnotfound$'
2828

29-
stderr '^example.com/untidy imports\n\texample.net/m imports\n\texample.net/indirectnotfound: cannot find module providing package example.net/indirectnotfound: module example.net/indirectnotfound: reading http://.*: 404 Not Found$'
29+
stderr '^example.com/untidy imports\n\texample.net/m: module example.net/m provides package example.net/m and is replaced but not required; to add it:\n\tgo get example.net/[email protected]$'
3030

31-
stderr '^example.com/untidy tested by\n\texample.com/untidy.test imports\n\texample.net/directtestnotfound: cannot find module providing package example.net/directtestnotfound: module example.net/directtestnotfound: reading http://.*: 404 Not Found$'
31+
stderr '^example.com/untidy tested by\n\texample.com/untidy.test imports\n\texample.net/directtestnotfound: no required module provides package example.net/directtestnotfound; to add it:\n\tgo get example.net/directtestnotfound$'
3232

3333
! stderr 'indirecttestnotfound' # Vendor prunes test dependencies.
3434

@@ -43,15 +43,22 @@ stderr -count=4 'cannot find module providing package'
4343
cmp go.mod.final go.mod
4444

4545

46-
# 'go mod vendor -e' still logs the errors, but succeeds and updates go.mod.
47-
46+
# 'go mod vendor -e' still logs the errors, but creates a vendor directory
47+
# and exits with status 0.
48+
# 'go mod vendor -e' does not update go.mod and will not vendor packages that
49+
# would require changing go.mod, for example, by adding a requirement.
4850
cp go.mod.orig go.mod
4951
go mod vendor -e
50-
stderr -count=3 'cannot find module providing package'
51-
cmp go.mod.final go.mod
52+
stderr -count=2 'no required module provides package'
53+
stderr '^example.com/untidy imports\n\texample.net/m: module example.net/m provides package example.net/m and is replaced but not required; to add it:\n\tgo get example.net/[email protected]$'
5254
exists vendor/modules.txt
53-
exists vendor/example.net/m/m.go
55+
! exists vendor/example.net
5456

57+
go mod edit -require example.net/[email protected]
58+
go mod vendor -e
59+
stderr -count=3 'no required module provides package'
60+
exists vendor/modules.txt
61+
exists vendor/example.net/m/m.go
5562

5663
-- go.mod --
5764
module example.com/untidy

src/cmd/go/testdata/script/mod_get_commit.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ go mod edit -require rsc.io/quote@23179ee
4444
grep 'rsc.io/quote 23179ee' go.mod
4545

4646
# but other commands fix them
47-
go mod graph
47+
go list -m -mod=mod all
4848
grep 'rsc.io/quote v1.5.1' go.mod
4949

5050
-- go.mod --

src/cmd/go/testdata/script/mod_getmode_vendor.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ stderr 'go list -m: can''t match module patterns using the vendor directory\n\t\
2525
-- go.mod --
2626
module x
2727

28+
go 1.16
2829
-- x.go --
2930
package x
3031
import _ "rsc.io/quote"

src/cmd/go/testdata/script/mod_list_retract.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ module example.com/use
101101
go 1.15
102102

103103
require example.com/retract v1.0.0-bad
104-
104+
-- go.sum --
105+
example.com/retract v1.0.0-bad h1:liAW69rbtjY67x2CcNzat668L/w+YGgNX3lhJsWIJis=
106+
example.com/retract v1.0.0-bad/go.mod h1:0DvGGofJ9hr1q63cBrOY/jSY52OwhRGA0K47NE80I5Y=
105107
-- use.go --
106108
package use
107109

src/cmd/go/testdata/script/mod_retention.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ go list -mod=mod all
3939
cmp go.mod go.mod.tidy
4040

4141
# "// indirect" comments should be added if appropriate.
42+
# TODO(#42504): add case for 'go list -mod=mod -tags=any all' when -tags=any
43+
# is supported. Only a command that loads "all" without build constraints
44+
# (except "ignore") has enough information to add "// indirect" comments.
45+
# 'go mod tidy' and 'go mod vendor' are the only commands that do that,
46+
# but 'go mod vendor' cannot write go.mod.
4247
cp go.mod.toodirect go.mod
4348
go list all
4449
cmp go.mod go.mod.toodirect
45-
go mod vendor # loads everything, so adds "// indirect" comments.
46-
cmp go.mod go.mod.tidy
47-
rm -r vendor
4850

4951

5052
# Redundant requirements should be preserved...

src/cmd/go/testdata/script/mod_tidy_error.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/non
1010

1111
! go mod vendor
1212
! stderr 'package nonexist is not in GOROOT'
13-
stderr '^issue27063 imports\n\tnonexist.example.com: cannot find module providing package nonexist.example.com'
14-
stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: cannot find module providing package other.example.com/nonexist'
13+
stderr '^issue27063 imports\n\tnonexist.example.com: no required module provides package nonexist.example.com; to add it:\n\tgo get nonexist.example.com$'
14+
stderr '^issue27063 imports\n\tissue27063/other imports\n\tother.example.com/nonexist: no required module provides package other.example.com/nonexist; to add it:\n\tgo get other.example.com/nonexist$'
1515

1616
-- go.mod --
1717
module issue27063

src/cmd/go/testdata/script/mod_vendor_replace.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module example.com/replace
3636

3737
require rsc.io/quote/v3 v3.0.0
3838
replace rsc.io/quote/v3 => ./local/not-rsc.io/quote/v3
39-
4039
-- imports.go --
4140
package replace
4241

@@ -64,3 +63,7 @@ require (
6463
not-rsc.io/quote/v3 v3.0.0
6564
)
6665
replace not-rsc.io/quote/v3 => rsc.io/quote/v3 v3.0.0
66+
-- multiple-paths/go.sum --
67+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
68+
rsc.io/quote/v3 v3.0.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
69+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

src/cmd/go/testdata/script/mod_vendor_trimpath.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ stdout '^example.com/[email protected]/stack.go$'
2929
-- go.mod --
3030
module example.com/main
3131

32-
require example.com/stack v1.0.0
32+
go 1.17
3333

34+
require example.com/stack v1.0.0
35+
-- go.sum --
36+
example.com/stack v1.0.0 h1:IEDLeew5NytZ8vrgCF/QVem3H3SR3QMttdu9HfJvk9I=
37+
example.com/stack v1.0.0/go.mod h1:7wFEbaV5e5O7wJ8aBdqQOR//UXppm/pwnwziMKViuI4=
3438
-- main.go --
3539
package main
3640

src/cmd/go/testdata/script/mod_vendor_unused_only.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module example.com/m
1212
go 1.14
1313

1414
require example.com v1.0.0 // indirect
15+
-- go.sum --
16+
example.com v1.0.0/go.mod h1:WRiieAqDBb1hVdDXLLdxNtCDWNfehn7FWyPC5Oz2vB4=
1517
-- go1.14-modules.txt --
1618
# example.com v1.0.0
1719
## explicit

src/cmd/go/testdata/script/mod_verify.txt

-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ stderr 'go.mod: checksum mismatch'
3939

4040
# go.sum should be created and updated automatically.
4141
rm go.sum
42-
go mod graph
43-
exists go.sum
44-
grep '^rsc.io/quote v1.1.0/go.mod ' go.sum
45-
! grep '^rsc.io/quote v1.1.0 ' go.sum
46-
4742
go mod tidy
4843
grep '^rsc.io/quote v1.1.0/go.mod ' go.sum
4944
grep '^rsc.io/quote v1.1.0 ' go.sum

src/cmd/go/testdata/script/modfile_flag.txt

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ stdout '^go.alt.mod$'
2424
go mod edit -require rsc.io/[email protected]
2525
grep rsc.io/quote go.alt.mod
2626

27+
# 'go list -m' should add sums to the alternate go.sum.
28+
go list -m -mod=mod all
29+
grep '^rsc.io/quote v1.5.2/go.mod ' go.alt.sum
30+
! grep '^rsc.io/quote v1.5.2 ' go.alt.sum
31+
2732
# other 'go mod' commands should work. 'go mod vendor' is tested later.
2833
go mod download rsc.io/quote
2934
go mod graph

0 commit comments

Comments
 (0)