Commit c5aea7a4 authored by Ian Lance Taylor's avatar Ian Lance Taylor

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 6887d8b1
......@@ -972,6 +972,8 @@
// 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.
//
......@@ -985,6 +987,7 @@
//
// type GoMod struct {
// Module Module
// Go string
// Require []Require
// Exclude []Module
// Replace []Replace
......
......@@ -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.
......@@ -75,6 +77,7 @@ writing it back to go.mod. The JSON output corresponds to these Go types:
type GoMod struct {
Module Module
Go string
Require []Require
Exclude []Module
Replace []Replace
......@@ -103,7 +106,7 @@ by invoking 'go mod edit' with -require, -exclude, and so on.
var (
editFmt = cmdEdit.Flag.Bool("fmt", false, "")
// editGo = cmdEdit.Flag.String("go", "", "")
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})
}
......
......@@ -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",
......
# 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
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