-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdk.go
110 lines (97 loc) · 2.64 KB
/
sdk.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 Derivita Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bazelpackagesdriver
import (
"bufio"
"fmt"
"os"
"path/filepath"
"time"
"github.com/bazelbuild/bazel-watcher/bazel"
"github.com/derivita/bazelpackagesdriver/driver"
"golang.org/x/tools/go/packages"
)
type gosdk struct {
goroot string
packages map[string]bool
}
func newGoSDK(bazel bazel.Bazel) (*gosdk, error) {
info, err := bazel.Info()
if err != nil {
return nil, err
}
bazelBin := info["bazel-bin"]
_, err = bazel.Build("@go_sdk//:package_list")
if err != nil {
return nil, err
}
root, err := findGoroot(bazel)
if err != nil {
return nil, err
}
s := &gosdk{
goroot: root,
}
return s.readPackages(filepath.Join(bazelBin, "external/go_sdk/packages.txt"))
}
func (s *gosdk) readPackages(filename string) (*gosdk, error) {
s.packages = make(map[string]bool)
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s.packages[scanner.Text()] = true
}
return s, scanner.Err()
}
func (s *gosdk) loadPackages(cfg *driver.Request, pkgs map[string]bool) ([]*packages.Package, error) {
words := make([]string, 0, len(pkgs))
for pkg, included := range pkgs {
if included {
words = append(words, pkg)
}
}
if len(words) == 0 {
return nil, nil
}
sdkConfig := &packages.Config{
Mode: cfg.Mode,
Env: append(cfg.Env, fmt.Sprintf("GOROOT=%v", s.goroot), "GOPACKAGESDRIVER=off"),
BuildFlags: cfg.BuildFlags,
Tests: cfg.Tests,
}
// Concurrent calls can result in "failed to cache compiled Go files" errors.
// So retry if we get an error.
// https://github.com/golang/go/issues/29667
var err error
var result []*packages.Package
for i := 0; i < 5; i++ {
if result, err = packages.Load(sdkConfig, words...); err == nil {
return result, nil
}
time.Sleep(1 * time.Millisecond)
}
return nil, err
}
func findGoroot(bazel bazel.Bazel) (string, error) {
results, err := bazel.Query("@go_sdk//:ROOT")
if err != nil {
return "", err
}
buildfile := results.Target[0].GetSourceFile().GetLocation()
return filepath.Dir(buildfile), nil
}