Skip to content

Commit dbde566

Browse files
author
Jay Conrod
committed
cmd/go: default to -mod=readonly in most commands
For #40728 Change-Id: I6618f1b5a632e8b353a483a83bb0cdf4ef6df72c Reviewed-on: https://go-review.googlesource.com/c/go/+/251881 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 3ab825d commit dbde566

File tree

5 files changed

+96
-112
lines changed

5 files changed

+96
-112
lines changed

src/cmd/go/alldocs.go

+38-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modload/help.go

+38-47
Original file line numberDiff line numberDiff line change
@@ -124,72 +124,63 @@ and the build list. For example:
124124
125125
Maintaining module requirements
126126
127-
The go.mod file is meant to be readable and editable by both
128-
programmers and tools. The go command itself automatically updates the go.mod file
129-
to maintain a standard formatting and the accuracy of require statements.
130-
131-
Any go command that finds an unfamiliar import will look up the module
132-
containing that import and add the latest version of that module
133-
to go.mod automatically. In most cases, therefore, it suffices to
134-
add an import to source code and run 'go build', 'go test', or even 'go list':
135-
as part of analyzing the package, the go command will discover
136-
and resolve the import and update the go.mod file.
137-
138-
Any go command can determine that a module requirement is
139-
missing and must be added, even when considering only a single
140-
package from the module. On the other hand, determining that a module requirement
141-
is no longer necessary and can be deleted requires a full view of
142-
all packages in the module, across all possible build configurations
143-
(architectures, operating systems, build tags, and so on).
144-
The 'go mod tidy' command builds that view and then
145-
adds any missing module requirements and removes unnecessary ones.
127+
The go.mod file is meant to be readable and editable by both programmers and
128+
tools. Most updates to dependencies can be performed using "go get" and
129+
"go mod tidy". Other module-aware build commands may be invoked using the
130+
-mod=mod flag to automatically add missing requirements and fix inconsistencies.
131+
132+
The "go get" command updates go.mod to change the module versions used in a
133+
build. An upgrade of one module may imply upgrading others, and similarly a
134+
downgrade of one module may imply downgrading others. The "go get" command
135+
makes these implied changes as well. See "go help module-get".
136+
137+
The "go mod" command provides other functionality for use in maintaining
138+
and understanding modules and go.mod files. See "go help mod", particularly
139+
"go help mod tidy" and "go help mod edit".
146140
147141
As part of maintaining the require statements in go.mod, the go command
148142
tracks which ones provide packages imported directly by the current module
149143
and which ones provide packages only used indirectly by other module
150144
dependencies. Requirements needed only for indirect uses are marked with a
151-
"// indirect" comment in the go.mod file. Indirect requirements are
145+
"// indirect" comment in the go.mod file. Indirect requirements may be
152146
automatically removed from the go.mod file once they are implied by other
153147
direct requirements. Indirect requirements only arise when using modules
154148
that fail to state some of their own dependencies or when explicitly
155149
upgrading a module's dependencies ahead of its own stated requirements.
156150
157-
Because of this automatic maintenance, the information in go.mod is an
158-
up-to-date, readable description of the build.
159-
160-
The 'go get' command updates go.mod to change the module versions used in a
161-
build. An upgrade of one module may imply upgrading others, and similarly a
162-
downgrade of one module may imply downgrading others. The 'go get' command
163-
makes these implied changes as well. If go.mod is edited directly, commands
164-
like 'go build' or 'go list' will assume that an upgrade is intended and
165-
automatically make any implied upgrades and update go.mod to reflect them.
166-
167-
The 'go mod' command provides other functionality for use in maintaining
168-
and understanding modules and go.mod files. See 'go help mod'.
169-
170-
The -mod build flag provides additional control over updating and use of go.mod.
171-
172-
If invoked with -mod=readonly, the go command is disallowed from the implicit
173-
automatic updating of go.mod described above. Instead, it fails when any changes
174-
to go.mod are needed. This setting is most useful to check that go.mod does
175-
not need updates, such as in a continuous integration and testing system.
176-
The "go get" command remains permitted to update go.mod even with -mod=readonly,
177-
and the "go mod" commands do not take the -mod flag (or any other build flags).
151+
The -mod build flag provides additional control over the updating and use of
152+
go.mod for commands that build packages like "go build" and "go test".
153+
154+
If invoked with -mod=readonly (the default in most situations), the go command
155+
reports an error if a package named on the command line or an imported package
156+
is not provided by any module in the build list computed from the main module's
157+
requirements. The go command also reports an error if a module's checksum is
158+
missing from go.sum (see Module downloading and verification). Either go.mod or
159+
go.sum must be updated in these situations.
160+
161+
If invoked with -mod=mod, the go command automatically updates go.mod and
162+
go.sum, fixing inconsistencies and adding missing requirements and checksums
163+
as needed. If the go command finds an unfamiliar import, it looks up the
164+
module containing that import and adds a requirement for the latest version
165+
of that module to go.mod. In most cases, therefore, one may add an import to
166+
source code and run "go build", "go test", or even "go list" with -mod=mod:
167+
as part of analyzing the package, the go command will resolve the import and
168+
update the go.mod file.
178169
179170
If invoked with -mod=vendor, the go command loads packages from the main
180171
module's vendor directory instead of downloading modules to and loading packages
181172
from the module cache. The go command assumes the vendor directory holds
182173
correct copies of dependencies, and it does not compute the set of required
183174
module versions from go.mod files. However, the go command does check that
184-
vendor/modules.txt (generated by 'go mod vendor') contains metadata consistent
175+
vendor/modules.txt (generated by "go mod vendor") contains metadata consistent
185176
with go.mod.
186177
187-
If invoked with -mod=mod, the go command loads modules from the module cache
188-
even if there is a vendor directory present.
178+
If the go command is not invoked with a -mod flag, and the vendor directory
179+
is present, and the "go" version in go.mod is 1.14 or higher, the go command
180+
will act as if it were invoked with -mod=vendor. Otherwise, the -mod flag
181+
defaults to -mod=readonly.
189182
190-
If the go command is not invoked with a -mod flag and the vendor directory
191-
is present and the "go" version in go.mod is 1.14 or higher, the go command
192-
will act as if it were invoked with -mod=vendor.
183+
Note that neither "go get" nor the "go mod" subcommands accept the -mod flag.
193184
194185
Pseudo-versions
195186

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func setDefaultBuildMod() {
571571
return
572572
}
573573
if modRoot == "" {
574-
cfg.BuildMod = "mod"
574+
cfg.BuildMod = "readonly"
575575
return
576576
}
577577

@@ -594,13 +594,7 @@ func setDefaultBuildMod() {
594594
cfg.BuildModReason = fmt.Sprintf("Go version in go.mod is %s, so vendor directory was not used.", modGo)
595595
}
596596

597-
p := ModFilePath()
598-
if fi, err := os.Stat(p); err == nil && !hasWritePerm(p, fi) {
599-
cfg.BuildMod = "readonly"
600-
cfg.BuildModReason = "go.mod file is read-only."
601-
return
602-
}
603-
cfg.BuildMod = "mod"
597+
cfg.BuildMod = "readonly"
604598
}
605599

606600
func legacyModInit() {
@@ -898,10 +892,12 @@ func WriteGoMod() {
898892
if dirty && cfg.BuildMod == "readonly" {
899893
// If we're about to fail due to -mod=readonly,
900894
// prefer to report a dirty go.mod over a dirty go.sum
901-
if cfg.BuildModReason != "" {
902-
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
903-
} else if cfg.BuildModExplicit {
895+
if cfg.BuildModExplicit {
904896
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly")
897+
} else if cfg.BuildModReason != "" {
898+
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly\n\t(%s)", cfg.BuildModReason)
899+
} else {
900+
base.Fatalf("go: updates to go.mod needed; try 'go mod tidy' first")
905901
}
906902
}
907903

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ stderr '^x.go:2:8: cannot find module providing package rsc\.io/quote: import lo
1010
! stderr '\(\)' # If we don't have a reason for -mod=readonly, don't log an empty one.
1111
cmp go.mod go.mod.empty
1212

13-
# -mod=readonly should be set implicitly if the go.mod file is read-only
14-
chmod 0400 go.mod
13+
# -mod=readonly should be set by default.
1514
env GOFLAGS=
1615
! go list all
17-
stderr '^x.go:2:8: cannot find module providing package rsc\.io/quote: import lookup disabled by -mod=readonly\n\t\(go.mod file is read-only\.\)$'
16+
stderr '^x.go:2:8: cannot find module providing package rsc\.io/quote$'
17+
cmp go.mod go.mod.empty
1818

19-
chmod 0600 go.mod
2019
env GOFLAGS=-mod=readonly
2120

2221
# update go.mod - go get allowed
@@ -48,18 +47,26 @@ cp go.mod go.mod.inconsistent
4847
stderr 'go: updates to go.mod needed, disabled by -mod=readonly'
4948
cmp go.mod go.mod.inconsistent
5049

50+
# We get a different message when -mod=readonly is used by default.
51+
env GOFLAGS=
52+
! go list
53+
stderr '^go: updates to go.mod needed; try ''go mod tidy'' first$'
54+
5155
# However, it should not reject files missing a 'go' directive,
5256
# since that was not always required.
5357
cp go.mod.nogo go.mod
5458
go list all
59+
cmp go.mod go.mod.nogo
5560

5661
# Nor should it reject files with redundant (not incorrect)
5762
# requirements.
5863
cp go.mod.redundant go.mod
5964
go list all
65+
cmp go.mod go.mod.redundant
6066

6167
cp go.mod.indirect go.mod
6268
go list all
69+
cmp go.mod go.mod.indirect
6370

6471
-- go.mod --
6572
module m

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
env GO111MODULE=on
22

3-
# 'go list -mod=readonly' should not add requirements even if they can be
4-
# resolved locally.
3+
# 'go list' should not add requirements even if they can be resolved locally.
54
cp go.mod go.mod.orig
6-
! go list -mod=readonly all
5+
! go list all
76
cmp go.mod go.mod.orig
87

98
# 'go list' should resolve imports using replacements.

0 commit comments

Comments
 (0)