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) {
}
}
modload.SetBuildList(keep)
modload.AddGoStmt()
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
modload.WriteGoMod()
}
......
......@@ -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)
......
......@@ -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
......
......@@ -10,6 +10,8 @@ stdout '^example.com$'
-- go.mod --
module example.com
go 1.13
-- main.go --
package main
......
......@@ -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
......
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