/
repo.go
274 lines (248 loc) · 8.54 KB
/
repo.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
/* Copyright 2017 The Bazel Authors. All rights reserved.
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 repo provides functionality for managing Go repository rules.
//
// UNSTABLE: The exported APIs in this package may change. In the future,
// language extensions should implement an interface for repository
// rule management. The update-repos command will call interface methods,
// and most if this package's functionality will move to language/go.
// Moving this package to an internal directory would break existing
// extensions, since RemoteCache is referenced through the resolve.Resolver
// interface, which extensions are required to implement.
package repo
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bazelbuild/bazel-gazelle/rule"
)
const gazelleFromDirectiveKey = "_gazelle_from_directive"
// FindExternalRepo attempts to locate the directory where Bazel has fetched
// the external repository with the given name. An error is returned if the
// repository directory cannot be located.
func FindExternalRepo(repoRoot, name string) (string, error) {
// See https://docs.bazel.build/versions/master/output_directories.html
// for documentation on Bazel directory layout.
// We expect the bazel-out symlink in the workspace root directory to point to
// <output-base>/execroot/<workspace-name>/bazel-out
// We expect the external repository to be checked out at
// <output-base>/external/<name>
externalPath := strings.Join([]string{repoRoot, "bazel-out", "..", "..", "..", "external", name}, string(os.PathSeparator))
cleanPath, err := filepath.EvalSymlinks(externalPath)
if err != nil {
return "", err
}
st, err := os.Stat(cleanPath)
if err != nil {
return "", err
}
if !st.IsDir() {
return "", fmt.Errorf("%s: not a directory", externalPath)
}
return cleanPath, nil
}
type macroKey struct {
file, def string
}
type loader struct {
repos []*rule.Rule
repoRoot string
repoFileMap map[string]*rule.File // repo rule name => file that contains repo
repoIndexMap map[string]int // repo rule name => index of rule in "repos" slice
visited map[macroKey]struct{}
}
// IsFromDirective returns true if the repo rule was defined from a directive.
func IsFromDirective(repo *rule.Rule) bool {
b, ok := repo.PrivateAttr(gazelleFromDirectiveKey).(bool)
return ok && b
}
// add adds a repository rule to a file.
// In the case of duplicate rules, select the rule
// with the following prioritization:
// - rules that were provided as directives have precedence
// - rules that were provided last
func (l *loader) add(file *rule.File, repo *rule.Rule) {
name := repo.Name()
if name == "" {
return
}
if i, ok := l.repoIndexMap[repo.Name()]; ok {
if IsFromDirective(l.repos[i]) && !IsFromDirective(repo) {
// We always prefer directives over non-directives
return
}
// Update existing rule to new rule
l.repos[i] = repo
} else {
l.repos = append(l.repos, repo)
l.repoIndexMap[name] = len(l.repos) - 1
}
l.repoFileMap[name] = file
}
// visit returns true exactly once for each file,function key, and false for all future instances
func (l *loader) visit(file, function string) bool {
if _, ok := l.visited[macroKey{file, function}]; ok {
return false
}
l.visited[macroKey{file, function}] = struct{}{}
return true
}
// ListRepositories extracts metadata about repositories declared in a
// file.
func ListRepositories(workspace *rule.File) (repos []*rule.Rule, repoFileMap map[string]*rule.File, err error) {
l := &loader{
repoRoot: filepath.Dir(workspace.Path),
repoIndexMap: make(map[string]int),
repoFileMap: make(map[string]*rule.File),
visited: make(map[macroKey]struct{}),
}
for _, repo := range workspace.Rules {
l.add(workspace, repo)
}
if err := l.loadExtraRepos(workspace); err != nil {
return nil, nil, err
}
for _, d := range workspace.Directives {
switch d.Key {
case "repository_macro":
parsed, err := ParseRepositoryMacroDirective(d.Value)
if err != nil {
return nil, nil, err
}
if err := l.loadRepositoriesFromMacro(parsed); err != nil {
return nil, nil, err
}
}
}
return l.repos, l.repoFileMap, nil
}
func (l *loader) loadRepositoriesFromMacro(macro *RepoMacro) error {
f := filepath.Join(l.repoRoot, macro.Path)
if !l.visit(f, macro.DefName) {
return nil
}
macroFile, err := rule.LoadMacroFile(f, "", macro.DefName)
if err != nil {
return fmt.Errorf("failed to load %s in repoRoot %s: %w", f, l.repoRoot, err)
}
loads := map[string]*rule.Load{}
for _, load := range macroFile.Loads {
for _, name := range load.Symbols() {
loads[name] = load
}
}
for _, rule := range macroFile.Rules {
// (Incorrectly) assume that anything with a name attribute is a rule, not a macro to recurse into
if rule.Name() != "" {
l.add(macroFile, rule)
continue
}
if !macro.Leveled {
continue
}
// If another repository macro is loaded that macro defName must be called.
// When a defName is called, the defName of the function is the rule's "kind".
// This then must be matched with the Load that it is imported with, so that
// file can be loaded
kind := rule.Kind()
load := loads[kind]
if load == nil {
continue
}
resolved := loadToMacroDef(load, l.repoRoot, kind)
// TODO: Also handle the case where one macro calls another macro in the same bzl file
if macro.Path == "" {
continue
}
if err := l.loadRepositoriesFromMacro(resolved); err != nil {
return err
}
}
return l.loadExtraRepos(macroFile)
}
// loadToMacroDef takes a load
// e.g. for if called on
// load("package_name:package_dir/file.bzl", alias_name="original_def_name")
// with defAlias = "alias_name", it will return:
//
// -> "/Path/to/package_name/package_dir/file.bzl"
// -> "original_def_name"
func loadToMacroDef(l *rule.Load, repoRoot, defAlias string) *RepoMacro {
rel := strings.Replace(filepath.Clean(l.Name()), ":", string(filepath.Separator), 1)
// A loaded macro may refer to the macro by a different name (alias) in the load,
// thus, the original name must be resolved to load the macro file properly.
defName := l.Unalias(defAlias)
return &RepoMacro{
Path: rel,
DefName: defName,
}
}
func (l *loader) loadExtraRepos(f *rule.File) error {
extraRepos, err := parseRepositoryDirectives(f.Directives)
if err != nil {
return err
}
for _, repo := range extraRepos {
l.add(f, repo)
}
return nil
}
func parseRepositoryDirectives(directives []rule.Directive) (repos []*rule.Rule, err error) {
for _, d := range directives {
switch d.Key {
case "repository":
vals := strings.Fields(d.Value)
if len(vals) < 2 {
return nil, fmt.Errorf("failure parsing repository: %s, expected repository kind and attributes", d.Value)
}
kind := vals[0]
r := rule.NewRule(kind, "")
r.SetPrivateAttr(gazelleFromDirectiveKey, true)
for _, val := range vals[1:] {
kv := strings.SplitN(val, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("failure parsing repository: %s, expected format for attributes is attr1_name=attr1_value", d.Value)
}
r.SetAttr(kv[0], kv[1])
}
if r.Name() == "" {
return nil, fmt.Errorf("failure parsing repository: %s, expected a name attribute for the given repository", d.Value)
}
repos = append(repos, r)
}
}
return repos, nil
}
type RepoMacro struct {
Path string
DefName string
Leveled bool
}
// ParseRepositoryMacroDirective checks the directive is in proper format, and splits
// path and defName. Repository_macros prepended with a "+" (e.g. "# gazelle:repository_macro +file%def")
// indicates a "leveled" macro, which loads other macro files.
func ParseRepositoryMacroDirective(directive string) (*RepoMacro, error) {
vals := strings.Split(directive, "%")
if len(vals) != 2 {
return nil, fmt.Errorf("Failure parsing repository_macro: %s, expected format is macroFile%%defName", directive)
}
f := vals[0]
if strings.HasPrefix(f, "..") {
return nil, fmt.Errorf("Failure parsing repository_macro: %s, macro file path %s should not start with \"..\"", directive, f)
}
return &RepoMacro{
Path: strings.TrimPrefix(f, "+"),
DefName: vals[1],
Leveled: strings.HasPrefix(f, "+"),
}, nil
}