go/packages: avoid loading some packages in when computing overlays
In some cases, it's safe to avoid loading additional packages when computing overlays. However it's not always safe to do so. Avoid some unnecessary loads when it's completely safe to do so. Updates golang/go#32538 Change-Id: Ie12204735940a540c9b3f29742f8479bcab5f077 Reviewed-on: https://go-review.googlesource.com/c/tools/+/181917 Run-TryBot: Michael Matloob <matloob@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
aac0b97cf9
commit
19ff4fff88
|
@ -32,6 +32,13 @@ func processGolistOverlay(cfg *Config, response *responseDeduper) (modifiedPkgs,
|
|||
var rootDirs map[string]string
|
||||
var onceGetRootDirs sync.Once
|
||||
|
||||
// If no new imports are added, it is safe to avoid loading any needPkgs.
|
||||
// Otherwise, it's hard to tell which package is actually being loaded
|
||||
// (due to vendoring) and whether any modified package will show up
|
||||
// in the transitive set of dependencies (because new imports are added,
|
||||
// potentially modifying the transitive set of dependencies).
|
||||
var overlayAddsImports bool
|
||||
|
||||
for opath, contents := range cfg.Overlay {
|
||||
base := filepath.Base(opath)
|
||||
dir := filepath.Dir(opath)
|
||||
|
@ -136,6 +143,7 @@ func processGolistOverlay(cfg *Config, response *responseDeduper) (modifiedPkgs,
|
|||
for _, imp := range imports {
|
||||
_, found := pkg.Imports[imp]
|
||||
if !found {
|
||||
overlayAddsImports = true
|
||||
// TODO(matloob): Handle cases when the following block isn't correct.
|
||||
// These include imports of test variants, imports of vendored packages, etc.
|
||||
id, ok := havePkgs[imp]
|
||||
|
@ -171,10 +179,12 @@ func processGolistOverlay(cfg *Config, response *responseDeduper) (modifiedPkgs,
|
|||
}
|
||||
}
|
||||
|
||||
if overlayAddsImports {
|
||||
needPkgs = make([]string, 0, len(needPkgsSet))
|
||||
for pkg := range needPkgsSet {
|
||||
needPkgs = append(needPkgs, pkg)
|
||||
}
|
||||
}
|
||||
modifiedPkgs = make([]string, 0, len(modifiedPkgsSet))
|
||||
for pkg := range modifiedPkgsSet {
|
||||
modifiedPkgs = append(modifiedPkgs, pkg)
|
||||
|
|
Loading…
Reference in New Issue