]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: write changes to go.mod and go.sum after loading the command-line-arguments...
authorBryan C. Mills <bcmills@google.com>
Thu, 21 Apr 2022 17:09:17 +0000 (13:09 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 4 May 2022 16:42:28 +0000 (16:42 +0000)
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 <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_run_issue52331.txt [new file with mode: 0644]

index 7f1a88ffe7bc2889c82baf7538f5a70245ecd0e5..29c0a4280a9c041aaaf1dc99888578a476708c2a 100644 (file)
@@ -717,6 +717,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 (file)
index 0000000..917e890
--- /dev/null
@@ -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)
+}