-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.go
59 lines (53 loc) · 1.85 KB
/
packages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package stamets
import (
"errors"
"fmt"
"time"
"golang.org/x/tools/go/packages"
)
// PackagesLoad loads packages according to the specified configuration and further
// filters them with `query`. It performs additional filtering when the configuration includes
// test packages.
func PackagesLoad(config *packages.Config, query string) BaseMetrics[[]*packages.Package] {
start := time.Now()
pkgs, err := packages.Load(config, query)
if err != nil {
return BaseMetrics[[]*packages.Package]{
err: err,
}
} else if packages.PrintErrors(pkgs) > 0 {
return BaseMetrics[[]*packages.Package]{
err: errors.New("errors encountered while loading packages"),
}
}
if config.Tests {
// Deduplicate packages that have test functions (such packages are
// returned twice, once with no tests and once with tests. We discard
// the package without tests.) This prevents duplicate versions of the
// same types, functions, ssa values, etc., which can be very confusing
// when debugging.
packageIDs := map[string]bool{}
for _, pkg := range pkgs {
packageIDs[pkg.ID] = true
}
filteredPkgs := []*packages.Package{}
for _, pkg := range pkgs {
if !packageIDs[fmt.Sprintf("%s [%s.test]", pkg.ID, pkg.ID)] {
filteredPkgs = append(filteredPkgs, pkg)
}
}
pkgs = filteredPkgs
}
return BaseMetrics[[]*packages.Package]{
Payload: pkgs,
Duration: time.Since(start),
}
}
// PackagesLoadWithTimeout loads packages according to the specified configuration within
// the alloted time limit, and further filters them with `query`. It performs additional
// filtering when the configuration includes test packages.
func PackagesLoadWithTimeout(t time.Duration, config *packages.Config, query string) (BaseMetrics[[]*packages.Package], bool) {
return TaskWithTimeout(t, func() BaseMetrics[[]*packages.Package] {
return PackagesLoad(config, query)
})
}