Skip to content

Commit 617b416

Browse files
author
Bryan C. Mills
committed
cmd/go: adjust module-related logging
Suppress “finding” messages unless they are unusually slow, and “extracting” messages always (they almost always occur conjunction with “downloading”, which is already logged). Log “found” messages for module dependencies added to satisfy missing import paths. Log top-level version changes in 'go get' when the selected version is not identical to the version requested on the command line. Updates #26152 Updates #33284 Change-Id: I4d0de60fab58d7cc7df8a2aff05c8b5b2220e626 Reviewed-on: https://go-review.googlesource.com/c/go/+/204777 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c9d89f6 commit 617b416

File tree

8 files changed

+63
-14
lines changed

8 files changed

+63
-14
lines changed

src/cmd/go/internal/modfetch/cache.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path/filepath"
1515
"strings"
16+
"time"
1617

1718
"cmd/go/internal/base"
1819
"cmd/go/internal/cfg"
@@ -25,10 +26,10 @@ import (
2526
"golang.org/x/mod/semver"
2627
)
2728

28-
var QuietLookup bool // do not print about lookups
29-
3029
var PkgMod string // $GOPATH/pkg/mod; set by package modload
3130

31+
const logFindingDelay = 1 * time.Second
32+
3233
func cacheDir(path string) (string, error) {
3334
if PkgMod == "" {
3435
return "", fmt.Errorf("internal error: modfetch.PkgMod not set")
@@ -140,6 +141,11 @@ func (r *cachingRepo) Versions(prefix string) ([]string, error) {
140141
err error
141142
}
142143
c := r.cache.Do("versions:"+prefix, func() interface{} {
144+
logTimer := time.AfterFunc(logFindingDelay, func() {
145+
fmt.Fprintf(os.Stderr, "go: finding versions for %s\n", r.path)
146+
})
147+
defer logTimer.Stop()
148+
143149
list, err := r.r.Versions(prefix)
144150
return cached{list, err}
145151
}).(cached)
@@ -162,9 +168,11 @@ func (r *cachingRepo) Stat(rev string) (*RevInfo, error) {
162168
return cachedInfo{info, nil}
163169
}
164170

165-
if !QuietLookup {
171+
logTimer := time.AfterFunc(logFindingDelay, func() {
166172
fmt.Fprintf(os.Stderr, "go: finding %s %s\n", r.path, rev)
167-
}
173+
})
174+
defer logTimer.Stop()
175+
168176
info, err = r.r.Stat(rev)
169177
if err == nil {
170178
// If we resolved, say, 1234abcde to v0.0.0-20180604122334-1234abcdef78,
@@ -192,9 +200,11 @@ func (r *cachingRepo) Stat(rev string) (*RevInfo, error) {
192200

193201
func (r *cachingRepo) Latest() (*RevInfo, error) {
194202
c := r.cache.Do("latest:", func() interface{} {
195-
if !QuietLookup {
203+
logTimer := time.AfterFunc(logFindingDelay, func() {
196204
fmt.Fprintf(os.Stderr, "go: finding %s latest\n", r.path)
197-
}
205+
})
206+
defer logTimer.Stop()
207+
198208
info, err := r.r.Latest()
199209

200210
// Save info for likely future Stat call.

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ func download(mod module.Version, dir string) (err error) {
7171
return err
7272
}
7373

74-
if cfg.CmdName != "mod download" {
75-
fmt.Fprintf(os.Stderr, "go: extracting %s %s\n", mod.Path, mod.Version)
76-
}
77-
7874
unlock, err := lockVersion(mod)
7975
if err != nil {
8076
return err

src/cmd/go/internal/modget/get.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func runGet(cmd *base.Command, args []string) {
372372
continue
373373

374374
default:
375-
// The argument is a package path.
375+
// The argument is a package or module path.
376376
if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
377377
// The path is in the main module. Nothing to query.
378378
if vers != "upgrade" && vers != "patch" {
@@ -763,6 +763,9 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo
763763

764764
info, err := modload.Query(path, vers, prevM.Version, modload.Allowed)
765765
if err == nil {
766+
if info.Version != vers && info.Version != prevM.Version {
767+
logOncef("go: %s %s => %s", path, vers, info.Version)
768+
}
766769
return module.Version{Path: path, Version: info.Version}, nil
767770
}
768771

@@ -791,14 +794,23 @@ func getQuery(path, vers string, prevM module.Version, forceModulePath bool) (mo
791794
if !strings.Contains(path, "...") {
792795
var modErr *modload.PackageNotInModuleError
793796
if errors.As(err, &modErr) && modErr.Mod.Path == path {
797+
if modErr.Mod.Version != vers {
798+
logOncef("go: %s %s => %s", path, vers, modErr.Mod.Version)
799+
}
794800
return modErr.Mod, nil
795801
}
796802
}
797803

798804
return module.Version{}, err
799805
}
800806

801-
return results[0].Mod, nil
807+
m := results[0].Mod
808+
if m.Path != path {
809+
logOncef("go: found %s in %s %s", path, m.Path, m.Version)
810+
} else if m.Version != vers {
811+
logOncef("go: %s %s => %s", path, vers, m.Version)
812+
}
813+
return m, nil
802814
}
803815

804816
// An upgrader adapts an underlying mvs.Reqs to apply an
@@ -955,6 +967,9 @@ func (u *upgrader) Upgrade(m module.Version) (module.Version, error) {
955967
return m, nil
956968
}
957969

970+
if info.Version != m.Version {
971+
logOncef("go: %s %s => %s", m.Path, getU, info.Version)
972+
}
958973
return module.Version{Path: m.Path, Version: info.Version}, nil
959974
}
960975

@@ -983,3 +998,12 @@ func (r *lostUpgradeReqs) Required(mod module.Version) ([]module.Version, error)
983998
}
984999
return r.Reqs.Required(mod)
9851000
}
1001+
1002+
var loggedLines sync.Map
1003+
1004+
func logOncef(format string, args ...interface{}) {
1005+
msg := fmt.Sprintf(format, args...)
1006+
if _, dup := loggedLines.LoadOrStore(msg, true); !dup {
1007+
fmt.Fprintln(os.Stderr, msg)
1008+
}
1009+
}

src/cmd/go/internal/modload/load.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ func (ld *loader) load(roots func() []string) {
626626
added[pkg.path] = true
627627
numAdded++
628628
if !haveMod[err.Module] {
629+
fmt.Fprintf(os.Stderr, "go: found %s in %s %s\n", pkg.path, err.Module.Path, err.Module.Version)
629630
haveMod[err.Module] = true
630631
modAddedBy[err.Module] = pkg
631632
buildList = append(buildList, err.Module)

src/cmd/go/internal/modload/query.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"sync"
1414

15+
"cmd/go/internal/cfg"
1516
"cmd/go/internal/imports"
1617
"cmd/go/internal/modfetch"
1718
"cmd/go/internal/search"
@@ -62,10 +63,24 @@ func Query(path, query, current string, allowed func(module.Version) bool) (*mod
6263
return info, err
6364
}
6465

66+
var errQueryDisabled error = queryDisabledError{}
67+
68+
type queryDisabledError struct{}
69+
70+
func (queryDisabledError) Error() string {
71+
if cfg.BuildModReason == "" {
72+
return fmt.Sprintf("cannot query module due to -mod=%s", cfg.BuildMod)
73+
}
74+
return fmt.Sprintf("cannot query module due to -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
75+
}
76+
6577
func queryProxy(proxy, path, query, current string, allowed func(module.Version) bool) (*modfetch.RevInfo, error) {
6678
if current != "" && !semver.IsValid(current) {
6779
return nil, fmt.Errorf("invalid previous version %q", current)
6880
}
81+
if cfg.BuildMod != "" && cfg.BuildMod != "mod" {
82+
return nil, errQueryDisabled
83+
}
6984
if allowed == nil {
7085
allowed = func(module.Version) bool { return true }
7186
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ go mod init m
1111
cmp stderr stderr-expected
1212

1313
-- stderr-expected --
14-
go: finding example.com/newcycle v1.0.0
1514
go get: inconsistent versions:
1615
example.com/newcycle/[email protected] requires example.com/newcycle/[email protected] (not example.com/newcycle/[email protected])

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$'
1111
stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
1212

1313
! go list -mod=vendor -m rsc.io/quote@latest
14-
stderr 'go list -m: rsc.io/quote@latest: module lookup disabled by -mod=vendor'
14+
stderr 'go list -m: rsc.io/quote@latest: cannot query module due to -mod=vendor'
1515
! go get -mod=vendor -u
1616
stderr 'flag provided but not defined: -mod'
1717

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ import (
5656

5757
func Test(t *testing.T) {}
5858
-- update-main-expected --
59+
go: example.com/badchain/c upgrade => v1.1.0
5960
go get: example.com/badchain/[email protected] updating to
6061
example.com/badchain/[email protected]: parsing go.mod:
6162
module declares its path as: badchain.example.com/c
6263
but was required as: example.com/badchain/c
6364
-- update-a-expected --
65+
go: example.com/badchain/a upgrade => v1.1.0
6466
go get: example.com/badchain/[email protected] requires
6567
example.com/badchain/[email protected] requires
6668
example.com/badchain/[email protected]: parsing go.mod:
@@ -73,11 +75,13 @@ go: example.com/badchain/[email protected] requires
7375
module declares its path as: badchain.example.com/c
7476
but was required as: example.com/badchain/c
7577
-- list-missing-expected --
78+
go: found example.com/badchain/c in example.com/badchain/c v1.1.0
7679
go: m/use imports
7780
example.com/badchain/c: example.com/badchain/[email protected]: parsing go.mod:
7881
module declares its path as: badchain.example.com/c
7982
but was required as: example.com/badchain/c
8083
-- list-missing-test-expected --
84+
go: found example.com/badchain/c in example.com/badchain/c v1.1.0
8185
go: m/testuse tested by
8286
m/testuse.test imports
8387
example.com/badchain/c: example.com/badchain/[email protected]: parsing go.mod:

0 commit comments

Comments
 (0)