@@ -571,7 +571,8 @@ specified by the `go` directive. This has the following effects:
571
571
1.16` and lower, an [ indirect dependency] ( #glos-direct-dependency ) is
572
572
included only if [ minimal version selection] ( #minimal-version-selection )
573
573
would otherwise select a different version.) This extra information enables
574
- [ lazy loading] ( #lazy-loading ) and [ module graph pruning] ( #graph-pruning ) .
574
+ [ module graph pruning] ( #graph-pruning ) and [ lazy module
575
+ loading] ( #lazy-loading ) .
575
576
* Because there may be many more ` // indirect ` dependencies than in previous
576
577
` go ` versions, indirect dependencies are recorded in a separate block
577
578
within the ` go.mod ` file.
@@ -580,7 +581,7 @@ specified by the `go` directive. This has the following effects:
580
581
subdirectories of ` vendor ` to identify the correct main module.)
581
582
* ` go mod vendor ` records the ` go ` version from each dependency's ` go.mod `
582
583
file in ` vendor/modules.txt ` .
583
- <!-- If you update this list, also update /doc/modules/gomod-ref#go. -->
584
+ <!-- If you update this list, also update /doc/modules/gomod-ref#go-notes . -->
584
585
585
586
A ` go.mod ` file may contain at most one ` go ` directive. Most commands will add a
586
587
` go ` directive with the current Go version if one is not present.
@@ -620,9 +621,11 @@ requirement in its own `go.mod` file (such as a dependency that lacks a `go.mod`
620
621
file altogether).
621
622
622
623
At ` go 1.17 ` and above, the ` go ` command adds an indirect requirement for each
623
- module that provides a package transitively imported by any package or test in
624
- the main module. These extra requirements enable [ lazy loading] ( #lazy-loading )
625
- and [ module graph pruning] ( #graph-pruning ) .
624
+ module that provides any package imported (even
625
+ [ indirectly] ( #glos-indirect-dependency ) ) by a package or test in the main module
626
+ or passed as an argument to ` go get ` . These more comprehensive requirements
627
+ enable [ module graph pruning] ( #graph-pruning ) and [ lazy module
628
+ loading] ( #lazy-loading ) .
626
629
627
630
```
628
631
RequireDirective = "require" ( RequireSpec | "(" newline { RequireSpec } ")" newline ) .
@@ -835,6 +838,8 @@ For example, consider this `go.mod` file:
835
838
```
836
839
module example.com/M
837
840
841
+ go 1.16
842
+
838
843
require (
839
844
example.com/A v1
840
845
example.com/B v1.0.0
@@ -989,62 +994,25 @@ is changed to 1.1.
989
994
suffix after an argument. This works similarly to a downgrade. All versions
990
995
of the named module are removed from the module graph.
991
996
992
- ## Module graph redundancy {#graph-redundancy}
993
-
994
- In a module with a [ ` go ` directive] ( #go-mod-file-go ) at ` go 1.17 ` or higher, the
995
- ` go.mod ` file is expected to include an explicit [ ` require `
996
- directive] ( #go-mod-file-require ) to the ` go.mod ` file for the [ selected
997
- version] ( #glos-selected-version ) of every module that provides any package
998
- imported (even [ indirectly] ( #glos-indirect-dependency ) ) by a package or test in
999
- the [ main module] ( #glos-main-module ) .
1000
-
1001
- When the ` go ` command loads packages it makes a best effort to check and
1002
- maintain this property: [ ` go mod tidy ` ] ( #go-mod-tidy ) adds indirect requirements
1003
- for all packages imported by the main module, and [ ` go get ` ] ( #go-get ) adds
1004
- indirect requirements for the packages imported by those named on the command
1005
- line.
1006
-
1007
- A module that _ does not_ provide any imported package cannot affect the compiled
1008
- artifacts produced by ` go build ` and ` go test ` , so this added redundancy allows
1009
- packages in the main module to be built and tested using only the requirements
1010
- listed in the its own ` go.mod ` file. The ` go ` command takes advantage of that
1011
- property to substantially reduce the size of the overall module graph.
1012
-
1013
- (At ` go 1.16 ` and below, the ` go.mod ` file includes only [ direct
1014
- dependencies] ( #glos-direct-dependency ) , so a much larger graph must be loaded to
1015
- ensure that all indirect dependencies are included.)
1016
-
1017
- See [ the design document] ( /design/36460-lazy-module-loading ) for more detail.
1018
-
1019
- ### Lazy loading {#lazy-loading}
1020
-
1021
- If the main module is at ` go 1.17 ` or higher, the ` go ` command avoids loading
1022
- the complete module graph until (and unless) it is needed. Instead, it loads
1023
- only the main module's ` go.mod ` file, then attempts to load the packages to be
1024
- built using only those requirements. If a package to be imported (for example, a
1025
- dependency of a test for a package outside the main module) is not found among
1026
- those requirements, then the rest of the module graph is loaded on demand.
1027
-
1028
- If all packages can be found without loading the full module graph, the ` go `
1029
- command then loads the ` go.mod ` files for _ only_ the modules containing those
1030
- packages, and their requirements are checked against the requirements of the
1031
- main module to ensure that they are locally consistent. (Inconsistencies can
1032
- arise due to version-control merges, hand-edits, and changes in modules that
1033
- have been [ replaced] ( #go-mod-file-replace ) using local filesystem paths.)
1034
-
1035
- ### Module graph pruning {#graph-pruning}
997
+ ## Module graph pruning {#graph-pruning}
1036
998
1037
999
If the main module is at ` go 1.17 ` or higher, the [ module
1038
1000
graph] ( #glos-module-graph ) used for [ minimal version
1039
- selection] ( #minimal-version-selection ) includes only the _ immediate_ (not
1040
- transitive) requirements for each module dependency that specifies ` go 1.17 ` or
1041
- higher in its own ` go.mod ` file, unless that version of the module is also
1042
- (transitively) required by some _ other_ dependency at ` go 1.16 ` or below.
1043
-
1044
- Since a ` go 1.17 ` ` go.mod ` file includes every dependency needed to build any
1045
- package or test in that module, the pruned module graph includes every
1046
- dependency needed to ` go build ` or ` go test ` the packages in every dependency
1047
- explicitly [ required] ( #go-mod-file-require ) by the main module.
1001
+ selection] ( #minimal-version-selection ) includes only the _ immediate_
1002
+ requirements for each module dependency that specifies ` go 1.17 ` or higher in
1003
+ its own ` go.mod ` file, unless that version of the module is also (transitively)
1004
+ required by some _ other_ dependency at ` go 1.16 ` or below. (The _ transitive_
1005
+ dependencies of ` go 1.17 ` dependencies are _ pruned out_ of the module graph.)
1006
+
1007
+ Since a ` go 1.17 ` ` go.mod ` file includes a [ require
1008
+ directive] ( #go-mod-file-require ) for every dependency needed to build any
1009
+ package or test in that module, the pruned module graph includes all of the
1010
+ dependencies needed to ` go build ` or ` go test ` the packages in any dependency
1011
+ explicitly required by the [ main module] ( #glos-main-module ) . A module that is
1012
+ _ not_ needed to build any package or test in a given module cannot affect the
1013
+ run-time behavior of its packages, so the dependencies that are pruned out of
1014
+ the module graph would only cause interference between otherwise-unrelated
1015
+ modules.
1048
1016
1049
1017
Modules whose requirements have been pruned out still appear in the module graph
1050
1018
and are still reported by ` go list -m all ` : their [ selected
@@ -1058,10 +1026,11 @@ named package to an explicit dependency, allowing `go build` or `go test` to be
1058
1026
invoked on that package.
1059
1027
1060
1028
Because Go 1.16 and earlier did not support module graph pruning, the full
1061
- transitive closure of dependencies — including transitive ` go 1.17 ` dependencies
1062
- — is still included for each module that specifies ` go 1.16 ` or lower. (The
1063
- ` go.mod ` file for a ` go 1.16 ` or lower module does not necessarily include the
1064
- requirements needed to build the packages and tests in that module.)
1029
+ transitive closure of dependencies — including transitive ` go 1.17 `
1030
+ dependencies — is still included for each module that specifies ` go 1.16 ` or
1031
+ lower. (At ` go 1.16 ` and below, the ` go.mod ` file includes only
1032
+ [ direct dependencies] ( #glos-direct-dependency ) , so a much larger graph must be
1033
+ loaded to ensure that all indirect dependencies are included.)
1065
1034
1066
1035
The [ ` go.sum ` file] ( #go-sum-files ) recorded by [ ` go mod tidy ` ] ( #go-mod-tidy ) for
1067
1036
a module by default includes checksums needed by the Go version _ one below_ the
@@ -1072,6 +1041,26 @@ module graph loaded by Go 1.17. The `-compat` flag can be used to override the
1072
1041
default version (for example, to prune the ` go.sum ` file more aggressively in a
1073
1042
` go 1.17 ` module).
1074
1043
1044
+ See [ the design document] ( /design/36460-lazy-module-loading ) for more detail.
1045
+
1046
+ ### Lazy module loading {#lazy-loading}
1047
+
1048
+ The more comprehensive requirements added for module graph pruning also enable
1049
+ another optimization when working within a module. If the main module is at
1050
+ ` go 1.17 ` or higher, the ` go ` command avoids loading the complete module graph
1051
+ until (and unless) it is needed. Instead, it loads only the main module's
1052
+ ` go.mod ` file, then attempts to load the packages to be built using only those
1053
+ requirements. If a package to be imported (for example, a dependency of a test
1054
+ for a package outside the main module) is not found among those requirements,
1055
+ then the rest of the module graph is loaded on demand.
1056
+
1057
+ If all imported packages can be found without loading the module graph, the
1058
+ ` go ` command then loads the ` go.mod ` files for _ only_ the modules containing
1059
+ those packages, and their requirements are checked against the requirements of
1060
+ the main module to ensure that they are locally consistent. (Inconsistencies can
1061
+ arise due to version-control merges, hand-edits, and changes in modules that
1062
+ have been [ replaced] ( #go-mod-file-replace ) using local filesystem paths.)
1063
+
1075
1064
## Compatibility with non-module repositories {#non-module-compat}
1076
1065
1077
1066
To ensure a smooth transition from ` GOPATH ` to modules, the ` go ` command can
@@ -1927,12 +1916,12 @@ imported by other packages.
1927
1916
1928
1917
Once ` go mod tidy ` has loaded this set of packages, it ensures that each module
1929
1918
that provides one or more packages has a ` require ` directive in the main
1930
- module's ` go.mod ` file or, if the main module is at ` go 1.16 ` or below (see
1931
- [ Module graph redundancy ] ( #graph-redundancy ) ), is required by another required
1932
- module. ` go mod tidy ` will add a requirement on the latest version on each
1933
- missing module (see [ Version queries ] ( # version-queries ) for the definition of
1934
- the ` latest ` version). ` go mod tidy ` will remove ` require ` directives for
1935
- modules that don't provide any packages in the set described above.
1919
+ module's ` go.mod ` file or — if the main module is at ` go 1.16 ` or below — is
1920
+ required by another required module. ` go mod tidy ` will add a requirement on the
1921
+ latest version of each missing module (see [ Version queries ] ( # version-queries )
1922
+ for the definition of the ` latest ` version). ` go mod tidy ` will remove ` require `
1923
+ directives for modules that don't provide any packages in the set described
1924
+ above.
1936
1925
1937
1926
` go mod tidy ` may also add or remove ` // indirect ` comments on ` require `
1938
1927
directives. An ` // indirect ` comment denotes a module that does not provide a
@@ -1942,8 +1931,9 @@ dependencies and comments are added.)
1942
1931
1943
1932
If the ` -go ` flag is set, ` go mod tidy ` will update the [ ` go `
1944
1933
directive] ( #go-mod-file-go ) to the indicated version, enabling or disabling
1945
- [ lazy loading] ( #lazy-loading ) and [ module graph pruning] ( #graph-pruning ) (and
1946
- adding or removing indirect requirements as needed) according to that version.
1934
+ [ module graph pruning] ( #graph-pruning ) and [ lazy module loading] ( #lazy-loading )
1935
+ (and adding or removing indirect requirements as needed) according to that
1936
+ version.
1947
1937
1948
1938
By default, ` go mod tidy ` will check that the [ selected
1949
1939
versions] ( #glos-selected-version ) of modules do not change when the module graph
@@ -4062,6 +4052,11 @@ or a module that appears in the [module graph](#glos-module-graph) but does not
4062
4052
provide any package directly imported by the main module.
4063
4053
(Compare [ direct dependency] ( #glos-direct-dependency ) .)
4064
4054
4055
+ <a id =" glos-lazy-module-loading " ></a >
4056
+ ** lazy module loading:** A change in Go 1.17 that avoids loading the [ module
4057
+ graph] ( #glos-module-graph ) for commands that do not need it in modules that
4058
+ specify ` go 1.17 ` or higher. See [ Lazy module loading] ( #lazy-loading ) .
4059
+
4065
4060
<a id =" glos-main-module " ></a >
4066
4061
** main module:** The module in which the ` go ` command is invoked. The main
4067
4062
module is defined by a [ ` go.mod ` file] ( #glos-go-mod-file ) in the current
@@ -4113,6 +4108,11 @@ module](#glos-main-module). Each vertex in the graph is a module; each edge is a
4113
4108
version from a ` require ` statement in a ` go.mod ` file (subject to ` replace ` and
4114
4109
` exclude ` statements in the main module's ` go.mod ` file.
4115
4110
4111
+ <a id =" glos-module-graph-pruning " ></a >
4112
+ ** module graph pruning:** A change in Go 1.17 that reduces the size of the
4113
+ module graph by omitting transitive dependencies of modules that specify `go
4114
+ 1.17` or higher. See [ Module graph pruning] ( #graph-pruning ) .
4115
+
4116
4116
<a id =" glos-module-path " ></a >
4117
4117
** module path:** A path that identifies a module and acts as a prefix for
4118
4118
package import paths within the module. For example, ` "golang.org/x/net" ` .
0 commit comments