From 62b967431217501d34a55d0a2417733eb87185ff Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Wed, 18 Dec 2019 19:08:00 -0500 Subject: [PATCH] internal/imports: consider direct mod deps more relevant As a followup to CL 204203, prefer direct dependencies over indirect. This should improve results for common names like "log" and "errors". Updates golang/go#36077. Change-Id: I3f8cfa070832c2035aec60c4e583ee1c0abf5085 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212021 Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/imports/mod.go | 9 +++++++-- internal/imports/mod_test.go | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/imports/mod.go b/internal/imports/mod.go index 0f9b87eb73..97660723ae 100644 --- a/internal/imports/mod.go +++ b/internal/imports/mod.go @@ -41,6 +41,7 @@ type ModuleJSON struct { Path string // module path Replace *ModuleJSON // replaced by this module Main bool // is this the main module? + Indirect bool // is this module only an indirect dependency of main module? Dir string // directory holding files for this module, if any GoMod string // path to go.mod file for this module, if any GoVersion string // go version used in module @@ -433,10 +434,14 @@ func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) { } importPath := info.nonCanonicalImportPath - relevance := 2 + relevance := 3 // Check if the directory is underneath a module that's in scope. if mod := r.findModuleByDir(info.dir); mod != nil { - relevance = 1 + if mod.Indirect { + relevance = 2 + } else { + relevance = 1 + } // It is. If dir is the target of a replace directive, // our guessed import path is wrong. Use the real one. if mod.Dir == info.dir { diff --git a/internal/imports/mod_test.go b/internal/imports/mod_test.go index 57c6c59c38..200dfbf7bb 100644 --- a/internal/imports/mod_test.go +++ b/internal/imports/mod_test.go @@ -856,10 +856,11 @@ import _ "rsc.io/quote" // Stdlib {"bytes", "bytes"}, {"http", "net/http"}, - // In scope modules - {"language", "golang.org/x/text/language"}, + // Direct module deps {"quote", "rsc.io/quote"}, + // Indirect deps {"rpackage", "example.com/rpackage"}, + {"language", "golang.org/x/text/language"}, // Out of scope modules {"quote", "rsc.io/quote/v2"}, }