Skip to content

Commit 48b7bdd

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent e7b7f87 commit 48b7bdd

File tree

5 files changed

+11
-6
lines changed

5 files changed

+11
-6
lines changed

src/cmd/go/internal/modcmd/tidy.go

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func runTidy(cmd *base.Command, args []string) {
6464
}
6565
}
6666
modload.SetBuildList(keep)
67-
modload.AddGoStmt()
6867
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
6968
modload.WriteGoMod()
7069
}

src/cmd/go/internal/modload/init.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func legacyModInit() {
421421
fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path)
422422
modFile = new(modfile.File)
423423
modFile.AddModuleStmt(path)
424-
AddGoStmt()
424+
addGoStmt() // Add the go directive before converted module requirements.
425425
}
426426

427427
for _, name := range altConfigs {
@@ -432,7 +432,6 @@ func legacyModInit() {
432432
if convert == nil {
433433
return
434434
}
435-
AddGoStmt()
436435
fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(cfg))
437436
cfg = filepath.ToSlash(cfg)
438437
if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil {
@@ -447,9 +446,9 @@ func legacyModInit() {
447446
}
448447
}
449448

450-
// AddGoStmt adds a go directive to the go.mod file if it does not already include one.
449+
// addGoStmt adds a go directive to the go.mod file if it does not already include one.
451450
// The 'go' version added, if any, is the latest version supported by this toolchain.
452-
func AddGoStmt() {
451+
func addGoStmt() {
453452
if modFile.Go != nil && modFile.Go.Version != "" {
454453
return
455454
}
@@ -653,6 +652,8 @@ func WriteGoMod() {
653652
return
654653
}
655654

655+
addGoStmt()
656+
656657
if loaded != nil {
657658
reqs := MinReqs()
658659
min, err := reqs.Required(Target)

src/cmd/go/testdata/script/mod_file_proxy.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ grep v1.5.1 $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/list
2121

2222
-- $WORK/x/go.mod --
2323
module x
24+
go 1.13
2425
require rsc.io/quote v1.5.1
2526
-- $WORK/x/x.go --
2627
package x
@@ -31,4 +32,4 @@ golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fq
3132
rsc.io/quote v1.5.1 h1:ZE3OgnVGrhXtFkGw90HwW992ZRqcdli/33DLqEYsoxA=
3233
rsc.io/quote v1.5.1/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
3334
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
34-
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
35+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

src/cmd/go/testdata/script/mod_init_empty.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ stdout '^example.com$'
1010

1111
-- go.mod --
1212
module example.com
13+
14+
go 1.13
1315
-- main.go --
1416
package main
1517

src/cmd/go/testdata/script/mod_load_badchain.txt

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ cmp stderr list-missing-test-expected
3737
-- go.mod.orig --
3838
module m
3939

40+
go 1.13
41+
4042
require example.com/badchain/a v1.0.0
4143
-- use/use.go --
4244
package use

0 commit comments

Comments
 (0)