]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: only add a 'go' directive on 'go mod tidy' or when a conversion occurs
authorBryan C. Mills <bcmills@google.com>
Thu, 28 Mar 2019 17:18:37 +0000 (13:18 -0400)
committerBryan C. Mills <bcmills@google.com>
Fri, 19 Apr 2019 21:16:38 +0000 (21:16 +0000)
If the go.mod file exists and is empty, we initialize it from any of
various formats supported by legacy dependency-management tools.

We also initialize the 'go' directive at that point: we know that the
go.mod file is incomplete, because it does not reflect the information
in the legacy configuration file, and since we know that the go.mod
file is incomplete, we should complete it with as much information as
we have — including the version of the language currently in use.

However, if there is no legacy configuration file present, then we
cannot infer that the go.mod file is incomplete: it may correctly
specify a module without external dependencies. In that case, we
should not initialize the 'go' directive either: the user will not be
expecting unnecessary edits to the go.mod file, and we generally do
not make unnecessary-but-helpful edits unless 'go mod tidy' is invoked
explicitly.

Fixes #30790
Fixes #31100

Change-Id: I05a7872bce54a917c10d910cd9a616cab52e2730
Reviewed-on: https://go-review.googlesource.com/c/go/+/169877
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_init_empty.txt [new file with mode: 0644]
src/cmd/go/testdata/script/mod_std_vendor.txt

index 789e93660854f7b14756e69eb840f2820efbbc7d..8c68ec51d8f5da9c70e5c7332ac927b4758267f9 100644 (file)
@@ -64,6 +64,7 @@ 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()
 }
index 4bc4a2449c71f8300efcc9c81c9ecab21ab52242..fad204a2ddcc9526e6444c34a8ddd4f5c893d867 100644 (file)
@@ -408,10 +408,9 @@ func legacyModInit() {
                fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path)
                modFile = new(modfile.File)
                modFile.AddModuleStmt(path)
+               AddGoStmt()
        }
 
-       addGoStmt()
-
        for _, name := range altConfigs {
                cfg := filepath.Join(modRoot, name)
                data, err := ioutil.ReadFile(cfg)
@@ -420,6 +419,7 @@ 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 {
@@ -434,8 +434,12 @@ func legacyModInit() {
        }
 }
 
-// addGoStmt adds a go statement referring to the current version.
-func addGoStmt() {
+// 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() {
+       if modFile.Go != nil && modFile.Go.Version != "" {
+               return
+       }
        tags := build.Default.ReleaseTags
        version := tags[len(tags)-1]
        if !strings.HasPrefix(version, "go") || !modfile.GoVersionRE.MatchString(version[2:]) {
diff --git a/src/cmd/go/testdata/script/mod_init_empty.txt b/src/cmd/go/testdata/script/mod_init_empty.txt
new file mode 100644 (file)
index 0000000..b6357bb
--- /dev/null
@@ -0,0 +1,16 @@
+env GO111MODULE=on
+
+env GOPATH=$devnull
+
+go list -m
+stdout '^example.com$'
+
+go list
+stdout '^example.com$'
+
+-- go.mod --
+module example.com
+-- main.go --
+package main
+
+func main() {}
index 5aa544cb77e75ea1cb5fa4982f9ecca255cbd8db..5986cff594665aa34bdeddac4019c261a10a7bd9 100644 (file)
@@ -20,7 +20,7 @@ stdout ^vendor/golang.org/x/crypto # dep of .TestImports
 # Modules outside the standard library should not use the packages vendored there...
 cd broken
 ! go build -mod=readonly
-stderr 'updates to go.mod needed'
+stderr 'disabled by -mod=readonly'
 ! go build -mod=vendor
 stderr 'cannot find package'
 stderr 'hpack'