]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: always add 'go' directive to the go.mod file if missing
authorBryan C. Mills <bcmills@google.com>
Wed, 15 May 2019 14:12:07 +0000 (10:12 -0400)
committerBryan C. Mills <bcmills@google.com>
Thu, 16 May 2019 17:47:34 +0000 (17:47 +0000)
Updates #30790
Fixes #31960

Change-Id: Ib3ac074cf1f98fe69f53af82d2a0441582116570
Reviewed-on: https://go-review.googlesource.com/c/go/+/176925
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_file_proxy.txt
src/cmd/go/testdata/script/mod_init_empty.txt
src/cmd/go/testdata/script/mod_load_badchain.txt

index 8c68ec51d8f5da9c70e5c7332ac927b4758267f9..789e93660854f7b14756e69eb840f2820efbbc7d 100644 (file)
@@ -64,7 +64,6 @@ func runTidy(cmd *base.Command, args []string) {
                }
        }
        modload.SetBuildList(keep)
-       modload.AddGoStmt()
        modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
        modload.WriteGoMod()
 }
index 3f3e8f8526013d33ab7035ada65e6a1c6d760836..14fadbf74e2574a7a0e9cd835fa5359231be41fd 100644 (file)
@@ -421,7 +421,7 @@ func legacyModInit() {
                fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path)
                modFile = new(modfile.File)
                modFile.AddModuleStmt(path)
-               AddGoStmt()
+               addGoStmt() // Add the go directive before converted module requirements.
        }
 
        for _, name := range altConfigs {
@@ -432,7 +432,6 @@ func legacyModInit() {
                        if convert == nil {
                                return
                        }
-                       AddGoStmt()
                        fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(cfg))
                        cfg = filepath.ToSlash(cfg)
                        if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil {
@@ -447,9 +446,9 @@ func legacyModInit() {
        }
 }
 
-// AddGoStmt adds a go directive to the go.mod file if it does not already include one.
+// addGoStmt adds a go directive to the go.mod file if it does not already include one.
 // The 'go' version added, if any, is the latest version supported by this toolchain.
-func AddGoStmt() {
+func addGoStmt() {
        if modFile.Go != nil && modFile.Go.Version != "" {
                return
        }
@@ -653,6 +652,8 @@ func WriteGoMod() {
                return
        }
 
+       addGoStmt()
+
        if loaded != nil {
                reqs := MinReqs()
                min, err := reqs.Required(Target)
index 2769d4db07e068fa031c2337f3685434280c122a..cf097f8c8071f9671089c60c6179736298816267 100644 (file)
@@ -21,6 +21,7 @@ grep v1.5.1 $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/list
 
 -- $WORK/x/go.mod --
 module x
+go 1.13
 require rsc.io/quote v1.5.1
 -- $WORK/x/x.go --
 package x
@@ -31,4 +32,4 @@ golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fq
 rsc.io/quote v1.5.1 h1:ZE3OgnVGrhXtFkGw90HwW992ZRqcdli/33DLqEYsoxA=
 rsc.io/quote v1.5.1/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
 rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
-rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
\ No newline at end of file
+rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
index b6357bb053fe3310515a94a33338822bf10e56cf..1c3888caed739e3dadd74acf6be6ef5a375eccbf 100644 (file)
@@ -10,6 +10,8 @@ stdout '^example.com$'
 
 -- go.mod --
 module example.com
+
+go 1.13
 -- main.go --
 package main
 
index 907e7dc6168b58ecafb930bf3faa636b2744608b..7bde83963d64e78a880fdd83d7cc0b35c21ec69d 100644 (file)
@@ -37,6 +37,8 @@ cmp stderr list-missing-test-expected
 -- go.mod.orig --
 module m
 
+go 1.13
+
 require example.com/badchain/a v1.0.0
 -- use/use.go --
 package use