-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
318 lines (285 loc) · 8.6 KB
/
driver.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2019 The Bazel Authors. All rights reserved.
// 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 (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/bazelbuild/bazel-watcher/bazel"
"github.com/bazelbuild/bazel-watcher/third_party/bazel/master/src/main/protobuf/blaze_query"
"github.com/derivita/bazelpackagesdriver/driver"
"github.com/derivita/bazelpackagesdriver/pkgconv"
"golang.org/x/tools/go/packages"
"golang.org/x/xerrors"
)
const fileQueryPrefix = "file="
var gorootPattern = regexp.MustCompile(`^bazel-[^/]+/external/go_sdk\b`)
const supportedModes = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypesSizes | packages.NeedModule
type bazelDriver struct {
cfg driver.Request
resp driver.Response
bazel bazel.Bazel
sdk *gosdk
workspaceRoot string
stdlibImports map[string]bool
fileQueries map[string]bool
importQueries map[string]bool
wildcardQuery bool
}
// New returns a Driver implementation based on bazel query.
func New() driver.Driver {
return func(cfg driver.Request, patterns ...string) (*driver.Response, error) {
bzl := bazel.New()
sdk, err := newGoSDK(bzl)
if err != nil {
return nil, err
}
driver := &bazelDriver{
cfg: cfg,
bazel: bzl,
sdk: sdk,
stdlibImports: make(map[string]bool),
fileQueries: make(map[string]bool),
importQueries: make(map[string]bool),
}
return driver.loadPackages(patterns...)
}
}
func (d *bazelDriver) loadPackages(patterns ...string) (*driver.Response, error) {
if info, err := d.bazel.Info(); err != nil {
return &driver.Response{NotHandled: true}, nil
} else {
d.workspaceRoot = info["workspace"]
}
log.Printf("mode: %v", d.cfg.Mode)
unsupportedModes := d.cfg.Mode &^ supportedModes
if unsupportedModes != 0 {
return nil, fmt.Errorf("%v (%b) not implemented", unsupportedModes, unsupportedModes)
}
if len(patterns) == 0 {
patterns = append(patterns, ".")
}
var queries []string
var ignoredPkg packages.Package
stdlibRoot := filepath.Join(d.sdk.goroot, "src")
for _, patt := range patterns {
if strings.HasPrefix(patt, fileQueryPrefix) {
fp := strings.TrimPrefix(patt, fileQueryPrefix)
if len(fp) == 0 {
return nil, fmt.Errorf("\"file=\" prefix given with no query after it")
}
if filepath.IsAbs(fp) {
if strings.HasPrefix(fp, stdlibRoot) {
if rel, err := filepath.Rel(stdlibRoot, fp); err == nil {
pkg := filepath.Dir(rel)
if d.sdk.packages[pkg] {
d.stdlibImports[pkg] = true
}
}
continue
}
if rel, err := filepath.Rel(d.workspaceRoot, fp); err == nil {
fp = rel
}
}
if prefix := gorootPattern.FindString(fp); prefix != "" {
ignoredPkg.Name = prefix
ignoredPkg.ID = prefix
ignoredPkg.IgnoredFiles = append(ignoredPkg.IgnoredFiles, fp)
continue
}
query, err := d.convertFileQuery(fp)
if err != nil {
return nil, err
}
queries = append(queries, query)
d.fileQueries[fp] = true
} else if patt == "." {
queries = append(queries, goFilter(":all"))
d.wildcardQuery = true
} else if patt == "./..." {
queries = append(queries, goFilter("..."))
d.wildcardQuery = true
} else if d.sdk.packages[patt] {
d.stdlibImports[patt] = true
} else {
// TODO: do we need to check for a package ID instead of import path?
d.importQueries[patt] = true
queries = append(queries, fmt.Sprintf("attr(importpath, '%v', deps(%v))", patt, goFilter("//...")))
}
}
var resp driver.Response
for pkg := range d.stdlibImports {
resp.Roots = append(resp.Roots, pkg)
}
if len(queries) != 0 {
pkgs, roots, err := d.packagesFromQueries(queries)
if err != nil {
return nil, err
}
resp.Packages = append(resp.Packages, pkgs...)
resp.Roots = append(resp.Roots, roots...)
}
if len(ignoredPkg.IgnoredFiles) > 0 {
log.Printf("Ignored %v files", len(ignoredPkg.IgnoredFiles))
ignoredPkg.GoFiles = ignoredPkg.IgnoredFiles[:1]
ignoredPkg.CompiledGoFiles = ignoredPkg.GoFiles
ignoredPkg.IgnoredFiles = ignoredPkg.IgnoredFiles[1:]
resp.Packages = append(resp.Packages, &ignoredPkg)
resp.Roots = append(resp.Roots, ignoredPkg.ID)
}
stdlib, err := d.sdk.loadPackages(&d.cfg, d.stdlibImports)
if err != nil {
return nil, err
}
resp.Packages = append(resp.Packages, stdlib...)
if d.cfg.Mode&packages.NeedTypesSizes != 0 {
goarch := driver.GetEnv(&d.cfg, "GOARCH", runtime.GOARCH)
// SizesFor always return StdSizesm,
resp.Sizes = types.SizesFor("gc", goarch).(*types.StdSizes)
}
return &resp, nil
}
func (d *bazelDriver) convertFileQuery(path string) (string, error) {
log.Printf("bazel query %#v", path)
result, err := d.bazel.Query(path)
if err != nil {
return "", err
}
if len(result.Target) == 0 {
return "", fmt.Errorf("no source file %#v", path)
} else if len(result.Target) > 1 {
return "", fmt.Errorf("multiple results for %#v", path)
}
t := result.Target[0]
if t.GetType() != blaze_query.Target_SOURCE_FILE {
return "", fmt.Errorf("wanted SOURCE_FILE, but %v is %v", path, t.GetType())
}
name := t.GetSourceFile().GetName()
pkg := strings.Split(name, ":")[0]
return fmt.Sprintf("attr(srcs, '%v', '%v:*')", name, pkg), nil
}
func (d *bazelDriver) packagesFromQueries(queries []string) (pkgs []*packages.Package, roots []string, err error) {
query := strings.Join(queries, "+")
if d.cfg.Mode&packages.NeedDeps != 0 {
query = goFilter(fmt.Sprintf("deps(%v)", query))
}
log.Printf("bazel query %#v", query)
results, err := d.bazel.Query(query)
if err != nil {
return
}
pkgs = pkgconv.Load(results.GetTarget())
log.Printf("file queries: %v", d.fileQueries)
for _, p := range pkgs {
if d.includeInRoots(p) {
roots = append(roots, p.ID)
}
}
if d.cfg.Mode&packages.NeedCompiledGoFiles != 0 {
for _, pkg := range pkgs {
pkg.CompiledGoFiles = pkg.GoFiles
}
}
if d.cfg.Mode&(packages.NeedImports|packages.NeedName) != 0 {
for _, pkg := range pkgs {
name, imports, err := d.parsePackage(pkg)
if err != nil {
return nil, nil, xerrors.Errorf("parsePackage: %w", err)
}
pkg.Name = name
for _, i := range imports {
if i, err = strconv.Unquote(i); err == nil {
if d.sdk.packages[i] {
if pkg.Imports == nil {
pkg.Imports = make(map[string]*packages.Package)
}
pkg.Imports[i] = &packages.Package{ID: i}
d.stdlibImports[i] = true
}
}
}
}
}
return
}
func (d *bazelDriver) includeInRoots(pkg *packages.Package) bool {
if d.wildcardQuery {
return strings.HasPrefix(pkg.ID, "//")
}
if pkg.PkgPath != "" && d.importQueries[pkg.PkgPath] {
return true
} else if len(d.fileQueries) > 0 && !strings.HasPrefix(pkg.ID, "@") {
for _, f := range pkg.GoFiles {
if d.fileQueries[f] {
return true
}
if rel, err := filepath.Rel(d.workspaceRoot, f); err == nil && d.fileQueries[rel] {
return true
}
}
}
return false
}
func (d *bazelDriver) parsePackage(pkg *packages.Package) (packageName string, imports []string, err error) {
triedBuild := false
dedupe := make(map[string]bool)
fset := token.NewFileSet()
for _, filename := range pkg.GoFiles {
var f *os.File
f, err = os.Open(filename)
if os.IsNotExist(err) && !triedBuild {
triedBuild = true
log.Printf("bazel build %v\n", pkg.ID)
d.bazel.Build(pkg.ID)
f, err = os.Open(filename)
}
if err != nil {
return
}
defer f.Close()
var syntax *ast.File
if syntax, err = parser.ParseFile(fset, filename, f, parser.ImportsOnly); err == nil {
packageName = syntax.Name.Name
for _, i := range syntax.Imports {
pkg := i.Path.Value
if !dedupe[pkg] {
dedupe[pkg] = true
imports = append(imports, pkg)
}
}
if d.cfg.Mode&packages.NeedImports == 0 {
// We don't need to parse all the files just to get the package name.
return
}
}
}
if packageName == "" {
packageName = filepath.Base(pkg.PkgPath)
}
return
}
func goFilter(expr string) string {
return fmt.Sprintf("kind(\"alias|proto_library|go_\", %s)", expr)
}