Skip to content

Commit 5d750db

Browse files
committed
cmd/go: fix go mod tidy crash on empty module
Fixes #27066. Change-Id: Iede4385ad86b42d7d90814965b161a7e64d29833 Reviewed-on: https://go-review.googlesource.com/129799 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent cc880de commit 5d750db

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ func runTidy(cmd *base.Command, args []string) {
4444

4545
// LoadALL adds missing modules.
4646
// Remove unused modules.
47-
used := map[module.Version]bool{modload.Target: true}
47+
used := make(map[module.Version]bool)
4848
for _, pkg := range modload.LoadALL() {
4949
used[modload.PackageModule(pkg)] = true
5050
}
51+
used[modload.Target] = true // note: LoadALL initializes Target
5152

5253
inGoMod := make(map[string]bool)
5354
for _, r := range modload.ModFile().Require {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ go list -m all
1010
stdout '^w.1 v1.2.0'
1111
stdout '^z.1 v1.2.0'
1212

13+
# empty tidy should not crash
14+
cd triv
15+
go mod tidy
16+
1317
-- go.mod --
1418
module m
1519

@@ -55,3 +59,6 @@ module z
5559

5660
-- z/sub/sub.go --
5761
package sub
62+
63+
-- triv/go.mod --
64+
module triv

0 commit comments

Comments
 (0)