Skip to content

Commit a1c481d

Browse files
author
Bryan C. Mills
committed
cmd/go: only add a 'go' directive on 'go mod tidy' or when a conversion occurs
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9f9e17a commit a1c481d

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func runTidy(cmd *base.Command, args []string) {
6464
}
6565
}
6666
modload.SetBuildList(keep)
67+
modload.AddGoStmt()
6768
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
6869
modload.WriteGoMod()
6970
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,9 @@ func legacyModInit() {
408408
fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", path)
409409
modFile = new(modfile.File)
410410
modFile.AddModuleStmt(path)
411+
AddGoStmt()
411412
}
412413

413-
addGoStmt()
414-
415414
for _, name := range altConfigs {
416415
cfg := filepath.Join(modRoot, name)
417416
data, err := ioutil.ReadFile(cfg)
@@ -420,6 +419,7 @@ func legacyModInit() {
420419
if convert == nil {
421420
return
422421
}
422+
AddGoStmt()
423423
fmt.Fprintf(os.Stderr, "go: copying requirements from %s\n", base.ShortPath(cfg))
424424
cfg = filepath.ToSlash(cfg)
425425
if err := modconv.ConvertLegacyConfig(modFile, cfg, data); err != nil {
@@ -434,8 +434,12 @@ func legacyModInit() {
434434
}
435435
}
436436

437-
// addGoStmt adds a go statement referring to the current version.
438-
func addGoStmt() {
437+
// AddGoStmt adds a go directive to the go.mod file if it does not already include one.
438+
// The 'go' version added, if any, is the latest version supported by this toolchain.
439+
func AddGoStmt() {
440+
if modFile.Go != nil && modFile.Go.Version != "" {
441+
return
442+
}
439443
tags := build.Default.ReleaseTags
440444
version := tags[len(tags)-1]
441445
if !strings.HasPrefix(version, "go") || !modfile.GoVersionRE.MatchString(version[2:]) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
env GO111MODULE=on
2+
3+
env GOPATH=$devnull
4+
5+
go list -m
6+
stdout '^example.com$'
7+
8+
go list
9+
stdout '^example.com$'
10+
11+
-- go.mod --
12+
module example.com
13+
-- main.go --
14+
package main
15+
16+
func main() {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ stdout ^vendor/golang.org/x/crypto # dep of .TestImports
2020
# Modules outside the standard library should not use the packages vendored there...
2121
cd broken
2222
! go build -mod=readonly
23-
stderr 'updates to go.mod needed'
23+
stderr 'disabled by -mod=readonly'
2424
! go build -mod=vendor
2525
stderr 'cannot find package'
2626
stderr 'hpack'

0 commit comments

Comments
 (0)