From 67d73b2960c82b2c8db0b9d0694c66a789a1db11 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 10 Jul 2023 12:07:43 -0400 Subject: [PATCH] internal/gopathwalk: use opts.Logf for errors if set Change-Id: Ic963c179a9fda8f7c5c6220a7e4558cccff374be Reviewed-on: https://go-review.googlesource.com/c/tools/+/508505 Reviewed-by: Heschi Kreinick Run-TryBot: Bryan Mills Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot --- internal/gopathwalk/walk.go | 17 ++++++++++++----- internal/imports/mod_test.go | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/gopathwalk/walk.go b/internal/gopathwalk/walk.go index 317363d09..452e342c5 100644 --- a/internal/gopathwalk/walk.go +++ b/internal/gopathwalk/walk.go @@ -9,7 +9,6 @@ package gopathwalk import ( "bufio" "bytes" - "fmt" "log" "os" "path/filepath" @@ -77,7 +76,7 @@ func walkDir(root Root, add func(Root, string), skip func(root Root, dir string) } start := time.Now() if opts.Logf != nil { - opts.Logf("gopathwalk: scanning %s", root.Path) + opts.Logf("scanning %s", root.Path) } w := &walker{ root: root, @@ -87,11 +86,15 @@ func walkDir(root Root, add func(Root, string), skip func(root Root, dir string) } w.init() if err := fastwalk.Walk(root.Path, w.walk); err != nil { - log.Printf("gopathwalk: scanning directory %v: %v", root.Path, err) + logf := opts.Logf + if logf == nil { + logf = log.Printf + } + logf("scanning directory %v: %v", root.Path, err) } if opts.Logf != nil { - opts.Logf("gopathwalk: scanned %s in %v", root.Path, time.Since(start)) + opts.Logf("scanned %s in %v", root.Path, time.Since(start)) } } @@ -221,7 +224,11 @@ func (w *walker) walk(path string, typ os.FileMode) error { func (w *walker) shouldTraverse(path string) bool { ts, err := os.Stat(path) if err != nil { - fmt.Fprintln(os.Stderr, err) + logf := w.opts.Logf + if logf == nil { + logf = log.Printf + } + logf("%v", err) return false } if !ts.IsDir() { diff --git a/internal/imports/mod_test.go b/internal/imports/mod_test.go index 92d57b580..46831d466 100644 --- a/internal/imports/mod_test.go +++ b/internal/imports/mod_test.go @@ -1292,7 +1292,7 @@ import ( func BenchmarkScanModCache(b *testing.B) { env := &ProcessEnv{ GocmdRunner: &gocommand.Runner{}, - Logf: log.Printf, + Logf: b.Logf, } exclude := []gopathwalk.RootType{gopathwalk.RootGOROOT} resolver, err := env.GetResolver()