Skip to content

Commit cf69725

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: write changes to go.mod and go.sum after loading the command-line-arguments package
This entrypoint was missed in CL 349600, and the behavior happened not to be covered by existing tests. Fixes #52331. Change-Id: Iccf12e8e633215abe4bfa1c3ca2fe3a8391b5ba5 Reviewed-on: https://go-review.googlesource.com/c/go/+/401536 Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 265b5fd commit cf69725

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/cmd/go/internal/modload/load.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@ func ImportFromFiles(ctx context.Context, gofiles []string) {
717717
},
718718
})
719719
requirements = loaded.requirements
720+
721+
if !ExplicitWriteGoMod {
722+
if err := commitRequirements(ctx); err != nil {
723+
base.Fatalf("go: %v", err)
724+
}
725+
}
720726
}
721727

722728
// DirImportPath returns the effective import path for dir,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Regression test for https://go.dev/issue/52331: 'go run -mod=mod'
2+
# failed to write go.mod and go.sum with the resolved dependencies.
3+
4+
[short] skip
5+
6+
! go run main.go
7+
# stderr '^main\.go:6:2: no required module provides package example\.com/version; to add it:\n\tgo get example\.com/version\n\z'
8+
9+
go run -mod=mod main.go
10+
cmp go.mod go.mod.want
11+
grep -count=1 '^example\.com/version v1.1.0 h1:' go.sum
12+
grep -count=1 '^example\.com/version v1.1.0/go.mod h1:' go.sum
13+
14+
-- go.mod --
15+
module example
16+
17+
go 1.17
18+
-- go.mod.want --
19+
module example
20+
21+
go 1.17
22+
23+
require example.com/version v1.1.0 // indirect
24+
-- main.go --
25+
package main
26+
27+
import (
28+
"fmt"
29+
30+
"example.com/version"
31+
)
32+
33+
func main() {
34+
fmt.Println(version.V)
35+
}

0 commit comments

Comments
 (0)