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 #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

+372
-134
lines changed

doc/go1.17.html

+8
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

+1-1
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

+2-2
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

+5-1
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

+5
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

+3-4
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

+2-1
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

+5-5
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

+3-4
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

+2-1
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

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ cmp go.mod go.mod.postget
9090
cp go.mod.orig go.mod
9191
go mod edit -go=1.17 go.mod
9292
go mod edit -go=1.17 go.mod.tidye
93-
go mod edit -go=1.17 go.mod.postget
9493

9594
go mod tidy -e
9695
cmp go.mod go.mod.tidye
@@ -99,7 +98,7 @@ stderr '^example\.net/m imports\n\texample\.net/x: package example\.net/x provid
9998

10099
go get -d example.net/[email protected] example.net/[email protected]
101100
go mod tidy
102-
cmp go.mod go.mod.postget
101+
cmp go.mod go.mod.postget-117
103102

104103

105104
-- go.mod --
@@ -144,6 +143,21 @@ require (
144143
example.net/x v0.1.0
145144
example.net/y v0.1.0 // indirect
146145
)
146+
-- go.mod.postget-117 --
147+
module example.net/m
148+
149+
go 1.17
150+
151+
replace (
152+
example.net/x v0.1.0 => ./x1
153+
example.net/x v0.2.0-pre => ./x2-pre
154+
example.net/y v0.1.0 => ./y1
155+
example.net/y v0.2.0 => ./y2
156+
)
157+
158+
require example.net/x v0.1.0
159+
160+
require example.net/y v0.1.0 // indirect
147161
-- m.go --
148162
package m
149163

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ cmpenv go.mod go.mod.latest
9292
-- go.mod --
9393
module example.com/m
9494

95+
require example.net/a v0.1.0
96+
9597
require (
96-
example.net/a v0.1.0
9798
example.net/c v0.1.0 // indirect
9899
example.net/d v0.1.0 // indirect
99100
)
@@ -118,8 +119,9 @@ module example.com/m
118119

119120
go 1.15
120121

122+
require example.net/a v0.1.0
123+
121124
require (
122-
example.net/a v0.1.0
123125
example.net/c v0.1.0 // indirect
124126
example.net/d v0.1.0 // indirect
125127
)
@@ -139,8 +141,9 @@ module example.com/m
139141

140142
go 1.15
141143

144+
require example.net/a v0.1.0
145+
142146
require (
143-
example.net/a v0.1.0
144147
example.net/c v0.1.0 // indirect
145148
example.net/d v0.2.0 // indirect
146149
)
@@ -160,10 +163,9 @@ module example.com/m
160163

161164
go 1.16
162165

163-
require (
164-
example.net/a v0.1.0
165-
example.net/c v0.1.0 // indirect
166-
)
166+
require example.net/a v0.1.0
167+
168+
require example.net/c v0.1.0 // indirect
167169

168170
replace (
169171
example.net/a v0.1.0 => ./a
@@ -180,8 +182,9 @@ module example.com/m
180182

181183
go 1.17
182184

185+
require example.net/a v0.1.0
186+
183187
require (
184-
example.net/a v0.1.0
185188
example.net/b v0.1.0 // indirect
186189
example.net/c v0.1.0 // indirect
187190
)
@@ -201,8 +204,9 @@ module example.com/m
201204

202205
go $goversion
203206

207+
require example.net/a v0.1.0
208+
204209
require (
205-
example.net/a v0.1.0
206210
example.net/b v0.1.0 // indirect
207211
example.net/c v0.1.0 // indirect
208212
)

src/cmd/vendor/golang.org/x/mod/modfile/read.go

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

0 commit comments

Comments
 (0)