Skip to content

Commit 9498b01

Browse files
author
Bryan C. Mills
committed
cmd/go: in Go 1.17+ modules, add indirect go.mod dependencies separately from direct ones
Fixes golang#45965 Change-Id: If5c0d7b29e9f81be0763f3fa68051d4ef5419990 Reviewed-on: https://go-review.googlesource.com/c/go/+/325922 Trust: Bryan C. Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 949f00c commit 9498b01

15 files changed

Lines changed: 372 additions & 134 deletions

doc/go1.17.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ <h4 id="lazy-loading">Lazy module loading</h4>
137137
<!-- TODO(bcmills): replace the design-doc link with proper documentation. -->
138138
</p>
139139

140+
<p><!-- golang.org/issue/45965 -->
141+
Because the number of additional explicit requirements in the go.mod file may
142+
be substantial, in a Go 1.17 module the newly-added requirements
143+
on <em>indirect</em> dependencies are maintained in a
144+
separate <code>require</code> block from the block containing direct
145+
dependencies.
146+
</p>
147+
140148
<p><!-- golang.org/issue/45094 -->
141149
To facilitate the upgrade to lazy loading, the
142150
<code>go</code> <code>mod</code> <code>tidy</code> subcommand now supports

src/cmd/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639 // indirect
88
golang.org/x/arch v0.0.0-20210502124803-cbf565b21d1e
99
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e // indirect
10-
golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd
10+
golang.org/x/mod v0.4.3-0.20210608190319-0f08993efd8a
1111
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
1212
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
1313
golang.org/x/tools v0.1.2-0.20210519160823-49064d2332f9

src/cmd/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
1313
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e h1:8foAy0aoO5GkqCvAEJ4VC4P3zksTg4X4aJCDpZzmgQI=
1414
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
1515
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
16-
golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd h1:CuRnpyMrCCBulv0d/y0CswR4K0vGydgE3DZ2wYPIOo8=
17-
golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
16+
golang.org/x/mod v0.4.3-0.20210608190319-0f08993efd8a h1:e8qnjKz4EE6OjRki9wTadWSIogINvq10sMcuBRORxMY=
17+
golang.org/x/mod v0.4.3-0.20210608190319-0f08993efd8a/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
1818
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1919
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
2020
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,10 +999,14 @@ func commitRequirements(ctx context.Context, goVersion string, rs *Requirements)
999999
Indirect: !rs.direct[m.Path],
10001000
})
10011001
}
1002-
modFile.SetRequire(list)
10031002
if goVersion != "" {
10041003
modFile.AddGoStmt(goVersion)
10051004
}
1005+
if semver.Compare("v"+modFileGoVersion(), separateIndirectVersionV) < 0 {
1006+
modFile.SetRequire(list)
1007+
} else {
1008+
modFile.SetRequireSeparateIndirect(list)
1009+
}
10061010
modFile.Cleanup()
10071011

10081012
dirty := index.modFileIsDirty(modFile)

src/cmd/go/internal/modload/modfile.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ const (
3535
// module's go.mod file is expected to list explicit requirements on every
3636
// module that provides any package transitively imported by that module.
3737
lazyLoadingVersionV = "v1.17"
38+
39+
// separateIndirectVersionV is the Go version (plus leading "v") at which
40+
// "// indirect" dependencies are added in a block separate from the direct
41+
// ones. See https://golang.org/issue/45965.
42+
separateIndirectVersionV = "v1.17"
3843
)
3944

4045
const (

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ module example.com/m
7373

7474
go $goversion
7575

76-
require (
77-
example.com/dep v0.1.0
78-
example.com/testdep v0.1.0 // indirect
79-
)
76+
require example.com/dep v0.1.0
77+
78+
require example.com/testdep v0.1.0 // indirect
8079

8180
replace (
8281
example.com/dep v0.1.0 => ./dep

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ go 1.17
139139
require (
140140
a v0.1.0
141141
b v0.1.0
142-
c v0.1.0 // indirect
143142
)
144143

144+
require c v0.1.0 // indirect
145+
145146
replace (
146147
a v0.1.0 => ./a1
147148
b v0.1.0 => ./b1

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ module example.com/lazy
7878

7979
go 1.17
8080

81-
require (
82-
example.com/a v0.1.0
83-
example.com/b v0.1.0 // indirect
84-
)
81+
require example.com/a v0.1.0
82+
83+
require example.com/b v0.1.0 // indirect
8584

8685
replace (
8786
example.com/a v0.1.0 => ./a
@@ -94,8 +93,9 @@ module example.com/lazy
9493

9594
go 1.17
9695

96+
require example.com/a v0.1.0
97+
9798
require (
98-
example.com/a v0.1.0
9999
example.com/b v0.1.0 // indirect
100100
example.com/c v0.1.0 // indirect
101101
)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ module example.com/lazy
148148

149149
go 1.17
150150

151-
require (
152-
example.com/a v0.1.0
153-
example.com/b v0.1.0 // indirect
154-
)
151+
require example.com/a v0.1.0
152+
153+
require example.com/b v0.1.0 // indirect
155154

156155
replace (
157156
example.com/a v0.1.0 => ./a

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ module m
140140
go $goversion
141141

142142
require (
143-
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
144143
rsc.io/quote v1.5.2
145144
rsc.io/sampler v1.3.0 // indirect
146145
rsc.io/testonly v1.0.0 // indirect
147146
)
147+
148+
require golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect

0 commit comments

Comments
 (0)