Skip to content

Commit c5aea7a

Browse files
cmd/go: add go mod edit -go flag
It can be used to set the Go language version used by the module. RELNOTES=yes Updates #28221 Change-Id: Ief0dd185c01195a17be20dff8627c80943c436e7 Reviewed-on: https://go-review.googlesource.com/c/147282 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 6887d8b commit c5aea7a

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/cmd/go/alldocs.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modcmd/edit.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ The -require, -droprequire, -exclude, -dropexclude, -replace,
6262
and -dropreplace editing flags may be repeated, and the changes
6363
are applied in the order given.
6464
65+
The -go=version flag sets the expected Go language version.
66+
6567
The -print flag prints the final go.mod in its text format instead of
6668
writing it back to go.mod.
6769
@@ -74,7 +76,8 @@ writing it back to go.mod. The JSON output corresponds to these Go types:
7476
}
7577
7678
type GoMod struct {
77-
Module Module
79+
Module Module
80+
Go string
7881
Require []Require
7982
Exclude []Module
8083
Replace []Replace
@@ -102,8 +105,8 @@ by invoking 'go mod edit' with -require, -exclude, and so on.
102105
}
103106

104107
var (
105-
editFmt = cmdEdit.Flag.Bool("fmt", false, "")
106-
// editGo = cmdEdit.Flag.String("go", "", "")
108+
editFmt = cmdEdit.Flag.Bool("fmt", false, "")
109+
editGo = cmdEdit.Flag.String("go", "", "")
107110
editJSON = cmdEdit.Flag.Bool("json", false, "")
108111
editPrint = cmdEdit.Flag.Bool("print", false, "")
109112
editModule = cmdEdit.Flag.String("module", "", "")
@@ -131,6 +134,7 @@ func init() {
131134
func runEdit(cmd *base.Command, args []string) {
132135
anyFlags :=
133136
*editModule != "" ||
137+
*editGo != "" ||
134138
*editJSON ||
135139
*editPrint ||
136140
*editFmt ||
@@ -161,7 +165,11 @@ func runEdit(cmd *base.Command, args []string) {
161165
}
162166
}
163167

164-
// TODO(rsc): Implement -go= once we start advertising it.
168+
if *editGo != "" {
169+
if !modfile.GoVersionRE.MatchString(*editGo) {
170+
base.Fatalf(`go mod: invalid -go option; expecting something like "-go 1.12"`)
171+
}
172+
}
165173

166174
data, err := ioutil.ReadFile(gomod)
167175
if err != nil {
@@ -177,6 +185,12 @@ func runEdit(cmd *base.Command, args []string) {
177185
modFile.AddModuleStmt(modload.CmdModModule)
178186
}
179187

188+
if *editGo != "" {
189+
if err := modFile.AddGoStmt(*editGo); err != nil {
190+
base.Fatalf("go: internal error: %v", err)
191+
}
192+
}
193+
180194
if len(edits) > 0 {
181195
for _, edit := range edits {
182196
edit(modFile)
@@ -344,6 +358,7 @@ func flagDropReplace(arg string) {
344358
// fileJSON is the -json output data structure.
345359
type fileJSON struct {
346360
Module module.Version
361+
Go string `json:",omitempty"`
347362
Require []requireJSON
348363
Exclude []module.Version
349364
Replace []replaceJSON
@@ -364,6 +379,9 @@ type replaceJSON struct {
364379
func editPrintJSON(modFile *modfile.File) {
365380
var f fileJSON
366381
f.Module = modFile.Module.Mod
382+
if modFile.Go != nil {
383+
f.Go = modFile.Go.Version
384+
}
367385
for _, r := range modFile.Require {
368386
f.Require = append(f.Require, requireJSON{Path: r.Mod.Path, Version: r.Mod.Version, Indirect: r.Indirect})
369387
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cmpenv go.mod $WORK/go.mod.edit2
2323

2424
# go mod edit -json
2525
go mod edit -json
26-
cmp stdout $WORK/go.mod.json
26+
cmpenv stdout $WORK/go.mod.json
2727

2828
# go mod edit -replace
2929
@@ -83,6 +83,7 @@ require x.3 v1.99.0
8383
"Module": {
8484
"Path": "x.x/y/z"
8585
},
86+
"Go": "$goversion",
8687
"Require": [
8788
{
8889
"Path": "x.3",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Test support for go mod -edit to set language version.
2+
3+
env GO111MODULE=on
4+
! go build
5+
stderr 'type aliases only supported as of'
6+
go mod edit -go=1.9
7+
grep 'go 1.9' go.mod
8+
go build
9+
10+
-- go.mod --
11+
module m
12+
go 1.8
13+
14+
-- alias.go --
15+
package alias
16+
type T = int

0 commit comments

Comments
 (0)