Skip to content

Commit 62b9674

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 2208e16 commit 62b9674

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

internal/imports/mod.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type ModuleJSON struct {
4141
Path string // module path
4242
Replace *ModuleJSON // replaced by this module
4343
Main bool // is this the main module?
44+
Indirect bool // is this module only an indirect dependency of main module?
4445
Dir string // directory holding files for this module, if any
4546
GoMod string // path to go.mod file for this module, if any
4647
GoVersion string // go version used in module
@@ -433,10 +434,14 @@ func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
433434
}
434435

435436
importPath := info.nonCanonicalImportPath
436-
relevance := 2
437+
relevance := 3
437438
// Check if the directory is underneath a module that's in scope.
438439
if mod := r.findModuleByDir(info.dir); mod != nil {
439-
relevance = 1
440+
if mod.Indirect {
441+
relevance = 2
442+
} else {
443+
relevance = 1
444+
}
440445
// It is. If dir is the target of a replace directive,
441446
// our guessed import path is wrong. Use the real one.
442447
if mod.Dir == info.dir {

internal/imports/mod_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,11 @@ import _ "rsc.io/quote"
856856
// Stdlib
857857
{"bytes", "bytes"},
858858
{"http", "net/http"},
859-
// In scope modules
860-
{"language", "golang.org/x/text/language"},
859+
// Direct module deps
861860
{"quote", "rsc.io/quote"},
861+
// Indirect deps
862862
{"rpackage", "example.com/rpackage"},
863+
{"language", "golang.org/x/text/language"},
863864
// Out of scope modules
864865
{"quote", "rsc.io/quote/v2"},
865866
}

0 commit comments

Comments
 (0)