aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-04-21 13:09:17 -0400
committerHeschi Kreinick <heschi@google.com>2022-05-09 20:16:25 +0000
commit2d6881b91a9bc62d55db30bf4546f929d52072be (patch)
tree16503222ad72d76a5c86bf84358ea0b108826abe
parentf4f19990c6b4de65e906e6bb2327cfa45ca1d072 (diff)
downloadgo-2d6881b91a9bc62d55db30bf4546f929d52072be.tar.gz
go-2d6881b91a9bc62d55db30bf4546f929d52072be.zip
[release-branch.go1.18] 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. Updates #52331. Fixes #52468. Change-Id: Iccf12e8e633215abe4bfa1c3ca2fe3a8391b5ba5 Reviewed-on: https://go-review.googlesource.com/c/go/+/401536 Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> (cherry picked from commit cf697253abb781e8a3e8825b7a4b5b96a534b907) Reviewed-on: https://go-review.googlesource.com/c/go/+/404094 Reviewed-by: David Chase <drchase@google.com>
-rw-r--r--src/cmd/go/internal/modload/load.go6
-rw-r--r--src/cmd/go/testdata/script/mod_run_issue52331.txt35
2 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index d4847efb98..9dfe9ad3a2 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -715,6 +715,12 @@ func ImportFromFiles(ctx context.Context, gofiles []string) {
},
})
requirements = loaded.requirements
+
+ if !ExplicitWriteGoMod {
+ if err := commitRequirements(ctx); err != nil {
+ base.Fatalf("go: %v", err)
+ }
+ }
}
// DirImportPath returns the effective import path for dir,
diff --git a/src/cmd/go/testdata/script/mod_run_issue52331.txt b/src/cmd/go/testdata/script/mod_run_issue52331.txt
new file mode 100644
index 0000000000..917e890211
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_run_issue52331.txt
@@ -0,0 +1,35 @@
+# Regression test for https://go.dev/issue/52331: 'go run -mod=mod'
+# failed to write go.mod and go.sum with the resolved dependencies.
+
+[short] skip
+
+! go run main.go
+# stderr '^main\.go:6:2: no required module provides package example\.com/version; to add it:\n\tgo get example\.com/version\n\z'
+
+go run -mod=mod main.go
+cmp go.mod go.mod.want
+grep -count=1 '^example\.com/version v1.1.0 h1:' go.sum
+grep -count=1 '^example\.com/version v1.1.0/go.mod h1:' go.sum
+
+-- go.mod --
+module example
+
+go 1.17
+-- go.mod.want --
+module example
+
+go 1.17
+
+require example.com/version v1.1.0 // indirect
+-- main.go --
+package main
+
+import (
+ "fmt"
+
+ "example.com/version"
+)
+
+func main() {
+ fmt.Println(version.V)
+}