Skip to content

Commit 396b07d

Browse files
author
Jay Conrod
committed
_content/doc: document -mod=readonly enabled by default
For golang/go#40728 Change-Id: I9617d2ebd920d1a0de11c3d7ae9d99505f282b84 Reviewed-on: https://go-review.googlesource.com/c/website/+/285113 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 4543c8c commit 396b07d

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

_content/doc/mod.md

+25-22
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ package, in the directory `b`. At least one file with the `.go` extension must
266266
be present in a directory for it to be considered a package. [Build
267267
constraints](/pkg/go/build/#hdr-Build_Constraints) are not applied for this
268268
purpose. If exactly one module in the build list provides the package, that
269-
module is used. If two or more modules provide the package, an error is
270-
reported. If no modules provide the package, the `go` command will attempt to
271-
find a new module (unless the flags `-mod=readonly` or `-mod=vendor` are used,
272-
in which case, an error is reported).
269+
module is used. If no modules provide the package or if two or more modules
270+
provide the package, the `go` command reports an error. The `-mod=mod` flag
271+
instructs the `go` command to attempt to find new modules providing missing
272+
packages and to update `go.mod` and `go.sum`. The [`go get`](#go-get) and [`go
273+
mod tidy`](#go-mod-tidy) commands do this automatically.
273274

274275
<!-- NOTE(golang.org/issue/27899): the go command reports an error when two
275276
or more modules provide a package with the same path as above. In the future,
@@ -630,9 +631,14 @@ replace (
630631

631632
### Automatic updates {#go-mod-file-updates}
632633

633-
The `go` command automatically updates `go.mod` when it uses the module graph if
634-
some information is missing or `go.mod` doesn't accurately reflect reality. For
635-
example, consider this `go.mod` file:
634+
Most commands report an error if `go.mod` is missing information or doesn't
635+
accurately relect reality. The [`go get`](#go-get) and
636+
[`go mod tidy`](#go-mod-tidy) commands may be used to fix most of these
637+
problems. Additionally, the `-mod=mod` flag may be used with most module-aware
638+
commands (`go build`, `go test`, and so on) to instruct the `go` command to
639+
fix problems in `go.mod` and `go.sum` automatically.
640+
641+
For example, consider this `go.mod` file:
636642

637643
```
638644
module example.com/M
@@ -648,8 +654,8 @@ require (
648654
exclude example.com/D v1.2.3
649655
```
650656

651-
The update rewrites non-canonical version identifiers to
652-
[canonical](#glos-canonical-version) semver form, so `example.com/A`'s `v1`
657+
The update triggered with `-mod=mod` rewrites non-canonical version identifiers
658+
to [canonical](#glos-canonical-version) semver form, so `example.com/A`'s `v1`
653659
becomes `v1.0.0`, and `example.com/E`'s `dev` becomes the pseudo-version for the
654660
latest commit on the `dev` branch, perhaps `v0.0.0-20180523231146-b3f5c0f6e5f1`.
655661

@@ -671,16 +677,13 @@ that future mechanical changes will result in minimal diffs. The `go` command
671677
will not update `go.mod` if only formatting changes are needed.
672678

673679
Because the module graph defines the meaning of import statements, any commands
674-
that load packages also use and therefore update `go.mod`, including `go build`,
675-
`go get`, `go install`, `go list`, `go test`, `go mod graph`, `go mod tidy`, and
676-
`go mod why`.
680+
that load packages also use `go.mod` and can therefore update it, including
681+
`go build`, `go get`, `go install`, `go list`, `go test`, `go mod tidy`.
677682

678-
The `-mod=readonly` flag prevents commands from automatically updating
679-
`go.mod`. However, if a command needs to perform an action that would
680-
update to `go.mod`, it will report an error. For example, if
681-
`go build` is asked to build a package not provided by any module in the build
682-
list, `go build` will report an error instead of looking up the module and
683-
updating requirements in `go.mod`.
683+
In Go 1.15 and lower, the `-mod=mod` flag was enabled by default, so updates
684+
were performed automatically. Since Go 1.16, the `go` command acts as
685+
if `-mod=readonly` were set instead: if any changes to `go.mod` are needed,
686+
the `go` command reports an error and suggests a fix.
684687

685688
## Minimal version selection (MVS) {#minimal-version-selection}
686689

@@ -981,9 +984,9 @@ commands accept the following flags, common to all module commands.
981984
* `-mod=vendor` tells the `go` command to use the `vendor` directory. In this
982985
mode, the `go` command will not use the network or the module cache.
983986
* By default, if the [`go` version](#go-mod-file-go) in `go.mod` is `1.14` or
984-
higher and a `vendor` directory is present, the `go` command will act as if
985-
`-mod=vendor` were used. Otherwise, the `go` command will act as if
986-
`-mod=mod` were used.
987+
higher and a `vendor` directory is present, the `go` command acts as if
988+
`-mod=vendor` were used. Otherwise, the `go` command acts as if
989+
`-mod=readonly` were used.
987990
* The `-modcacherw` flag instructs the `go` command to create new directories
988991
in the module cache with read-write permissions instead of making them
989992
read-only. When this flag is used consistently (typically by setting
@@ -1029,7 +1032,7 @@ If the `vendor` directory is present in the main module's root directory, it
10291032
will be used automatically if the [`go` version](#go-mod-file-go) in the main
10301033
module's [`go.mod` file](#glos-go-mod-file) is `1.14` or higher. To explicitly
10311034
enable vendoring, invoke the `go` command with the flag `-mod=vendor`. To
1032-
disable vendoring, use the flag `-mod=mod`.
1035+
disable vendoring, use the flag `-mod=readonly` or `-mod=mod`.
10331036

10341037
When vendoring is enabled, [build commands](#build-commands) like `go build` and
10351038
`go test` load packages from the `vendor` directory instead of accessing the

0 commit comments

Comments
 (0)