@@ -266,10 +266,11 @@ package, in the directory `b`. At least one file with the `.go` extension must
266
266
be present in a directory for it to be considered a package. [ Build
267
267
constraints] ( /pkg/go/build/#hdr-Build_Constraints ) are not applied for this
268
268
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.
273
274
274
275
<!-- NOTE(golang.org/issue/27899): the go command reports an error when two
275
276
or more modules provide a package with the same path as above. In the future,
@@ -630,9 +631,14 @@ replace (
630
631
631
632
### Automatic updates {#go-mod-file-updates}
632
633
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:
636
642
637
643
```
638
644
module example.com/M
@@ -648,8 +654,8 @@ require (
648
654
exclude example.com/D v1.2.3
649
655
```
650
656
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 `
653
659
becomes ` v1.0.0 ` , and ` example.com/E ` 's ` dev ` becomes the pseudo-version for the
654
660
latest commit on the ` dev ` branch, perhaps ` v0.0.0-20180523231146-b3f5c0f6e5f1 ` .
655
661
@@ -671,16 +677,13 @@ that future mechanical changes will result in minimal diffs. The `go` command
671
677
will not update ` go.mod ` if only formatting changes are needed.
672
678
673
679
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 ` .
677
682
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.
684
687
685
688
## Minimal version selection (MVS) {#minimal-version-selection}
686
689
@@ -981,9 +984,9 @@ commands accept the following flags, common to all module commands.
981
984
* ` -mod=vendor ` tells the ` go ` command to use the ` vendor ` directory. In this
982
985
mode, the ` go ` command will not use the network or the module cache.
983
986
* 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.
987
990
* The ` -modcacherw ` flag instructs the ` go ` command to create new directories
988
991
in the module cache with read-write permissions instead of making them
989
992
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
1029
1032
will be used automatically if the [ ` go ` version] ( #go-mod-file-go ) in the main
1030
1033
module's [ ` go.mod ` file] ( #glos-go-mod-file ) is ` 1.14 ` or higher. To explicitly
1031
1034
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` .
1033
1036
1034
1037
When vendoring is enabled, [ build commands] ( #build-commands ) like ` go build ` and
1035
1038
` go test ` load packages from the ` vendor ` directory instead of accessing the
0 commit comments