]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add go mod edit -go flag
authorIan Lance Taylor <iant@golang.org>
Sat, 3 Nov 2018 00:08:24 +0000 (17:08 -0700)
committerIan Lance Taylor <iant@golang.org>
Sat, 10 Nov 2018 01:42:03 +0000 (01:42 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/alldocs.go
src/cmd/go/internal/modcmd/edit.go
src/cmd/go/testdata/script/mod_edit.txt
src/cmd/go/testdata/script/mod_edit_go.txt [new file with mode: 0644]

index 27dbdd94f53e5c2077b0463be437130a9ba105ac..f8c4d2ffa912f43652333433b49f39b1c952fdce 100644 (file)
 // and -dropreplace editing flags may be repeated, and the changes
 // are applied in the order given.
 //
+// The -go=version flag sets the expected Go language version.
+//
 // The -print flag prints the final go.mod in its text format instead of
 // writing it back to go.mod.
 //
 //     }
 //
 //     type GoMod struct {
-//             Module Module
+//             Module  Module
+//             Go      string
 //             Require []Require
 //             Exclude []Module
 //             Replace []Replace
index 5fea3e48e0816d82531050ffe7802cbe720f612c..c589c6d4edff7d6b1d2250c1097db1ee21cdaa36 100644 (file)
@@ -62,6 +62,8 @@ The -require, -droprequire, -exclude, -dropexclude, -replace,
 and -dropreplace editing flags may be repeated, and the changes
 are applied in the order given.
 
+The -go=version flag sets the expected Go language version.
+
 The -print flag prints the final go.mod in its text format instead of
 writing it back to go.mod.
 
@@ -74,7 +76,8 @@ writing it back to go.mod. The JSON output corresponds to these Go types:
        }
 
        type GoMod struct {
-               Module Module
+               Module  Module
+               Go      string
                Require []Require
                Exclude []Module
                Replace []Replace
@@ -102,8 +105,8 @@ by invoking 'go mod edit' with -require, -exclude, and so on.
 }
 
 var (
-       editFmt = cmdEdit.Flag.Bool("fmt", false, "")
-       // editGo     = cmdEdit.Flag.String("go", "", "")
+       editFmt    = cmdEdit.Flag.Bool("fmt", false, "")
+       editGo     = cmdEdit.Flag.String("go", "", "")
        editJSON   = cmdEdit.Flag.Bool("json", false, "")
        editPrint  = cmdEdit.Flag.Bool("print", false, "")
        editModule = cmdEdit.Flag.String("module", "", "")
@@ -131,6 +134,7 @@ func init() {
 func runEdit(cmd *base.Command, args []string) {
        anyFlags :=
                *editModule != "" ||
+                       *editGo != "" ||
                        *editJSON ||
                        *editPrint ||
                        *editFmt ||
@@ -161,7 +165,11 @@ func runEdit(cmd *base.Command, args []string) {
                }
        }
 
-       // TODO(rsc): Implement -go= once we start advertising it.
+       if *editGo != "" {
+               if !modfile.GoVersionRE.MatchString(*editGo) {
+                       base.Fatalf(`go mod: invalid -go option; expecting something like "-go 1.12"`)
+               }
+       }
 
        data, err := ioutil.ReadFile(gomod)
        if err != nil {
@@ -177,6 +185,12 @@ func runEdit(cmd *base.Command, args []string) {
                modFile.AddModuleStmt(modload.CmdModModule)
        }
 
+       if *editGo != "" {
+               if err := modFile.AddGoStmt(*editGo); err != nil {
+                       base.Fatalf("go: internal error: %v", err)
+               }
+       }
+
        if len(edits) > 0 {
                for _, edit := range edits {
                        edit(modFile)
@@ -344,6 +358,7 @@ func flagDropReplace(arg string) {
 // fileJSON is the -json output data structure.
 type fileJSON struct {
        Module  module.Version
+       Go      string `json:",omitempty"`
        Require []requireJSON
        Exclude []module.Version
        Replace []replaceJSON
@@ -364,6 +379,9 @@ type replaceJSON struct {
 func editPrintJSON(modFile *modfile.File) {
        var f fileJSON
        f.Module = modFile.Module.Mod
+       if modFile.Go != nil {
+               f.Go = modFile.Go.Version
+       }
        for _, r := range modFile.Require {
                f.Require = append(f.Require, requireJSON{Path: r.Mod.Path, Version: r.Mod.Version, Indirect: r.Indirect})
        }
index bf6f2a22c709948b5483240b885c20c3e27f2b4b..61801d5021bf2a8334b5c633ebf6855bef71fc76 100644 (file)
@@ -23,7 +23,7 @@ cmpenv go.mod $WORK/go.mod.edit2
 
 # go mod edit -json
 go mod edit -json
-cmp stdout $WORK/go.mod.json
+cmpenv stdout $WORK/go.mod.json
 
 # go mod edit -replace
 go mod edit -replace=x.1@v1.3.0=y.1/v2@v2.3.5 -replace=x.1@v1.4.0=y.1/v2@v2.3.5
@@ -83,6 +83,7 @@ require x.3 v1.99.0
        "Module": {
                "Path": "x.x/y/z"
        },
+       "Go": "$goversion",
        "Require": [
                {
                        "Path": "x.3",
diff --git a/src/cmd/go/testdata/script/mod_edit_go.txt b/src/cmd/go/testdata/script/mod_edit_go.txt
new file mode 100644 (file)
index 0000000..3ec8137
--- /dev/null
@@ -0,0 +1,16 @@
+# Test support for go mod -edit to set language version.
+
+env GO111MODULE=on
+! go build
+stderr 'type aliases only supported as of'
+go mod edit -go=1.9
+grep 'go 1.9' go.mod
+go build
+
+-- go.mod --
+module m
+go 1.8
+
+-- alias.go --
+package alias
+type T = int