Commit 48b7bdd0 authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go: always add 'go' directive to the go.mod file if missing

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: default avatarJay Conrod <jayconrod@google.com>
parent e7b7f874
...@@ -64,7 +64,6 @@ func runTidy(cmd *base.Command, args []string) { ...@@ -64,7 +64,6 @@ func runTidy(cmd *base.Command, args []string) {
} }
} }
modload.SetBuildList(keep) modload.SetBuildList(keep)
modload.AddGoStmt()
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
modload.WriteGoMod() modload.WriteGoMod()
} }
......
...@@ -421,7 +421,7 @@ func legacyModInit() { ...@@ -421,7 +421,7 @@ func legacyModInit() {
fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path) fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path)
modFile = new(modfile.File) modFile = new(modfile.File)
modFile.AddModuleStmt(path) modFile.AddModuleStmt(path)
AddGoStmt() addGoStmt() // Add the go directive before converted module requirements.
} }
for _, name := range altConfigs { for _, name := range altConfigs {
...@@ -432,7 +432,6 @@ func legacyModInit() { ...@@ -432,7 +432,6 @@ func legacyModInit() {
if convert == nil { if convert == nil {
return return
} }
AddGoStmt()
fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(cfg)) fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(cfg))
cfg = filepath.ToSlash(cfg) cfg = filepath.ToSlash(cfg)
if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil { if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil {
...@@ -447,9 +446,9 @@ func legacyModInit() { ...@@ -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. // 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 != "" { if modFile.Go != nil && modFile.Go.Version != "" {
return return
} }
...@@ -653,6 +652,8 @@ func WriteGoMod() { ...@@ -653,6 +652,8 @@ func WriteGoMod() {
return return
} }
addGoStmt()
if loaded != nil { if loaded != nil {
reqs := MinReqs() reqs := MinReqs()
min, err := reqs.Required(Target) min, err := reqs.Required(Target)
......
...@@ -21,6 +21,7 @@ grep v1.5.1 $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/list ...@@ -21,6 +21,7 @@ grep v1.5.1 $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/list
-- $WORK/x/go.mod -- -- $WORK/x/go.mod --
module x module x
go 1.13
require rsc.io/quote v1.5.1 require rsc.io/quote v1.5.1
-- $WORK/x/x.go -- -- $WORK/x/x.go --
package x package x
......
...@@ -10,6 +10,8 @@ stdout '^example.com$' ...@@ -10,6 +10,8 @@ stdout '^example.com$'
-- go.mod -- -- go.mod --
module example.com module example.com
go 1.13
-- main.go -- -- main.go --
package main package main
......
...@@ -37,6 +37,8 @@ cmp stderr list-missing-test-expected ...@@ -37,6 +37,8 @@ cmp stderr list-missing-test-expected
-- go.mod.orig -- -- go.mod.orig --
module m module m
go 1.13
require example.com/badchain/a v1.0.0 require example.com/badchain/a v1.0.0
-- use/use.go -- -- use/use.go --
package use package use
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment