-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
574 lines (495 loc) · 15.3 KB
/
resolve.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright 2019 The Bazel Authors. All rights reserved.
// Modifications copyright (C) 2021 BenchSci Analytics Inc.
// Modifications copyright (C) 2018 Ecosia GmbH
// 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 nestjs
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/repo"
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
)
// BUILTINS list taken from https://github.com/sindresorhus/builtin-modules/blob/master/builtin-modules.json
var BUILTINS = map[string]bool{
"assert": true,
"async_hooks": true,
"buffer": true,
"child_process": true,
"cluster": true,
"console": true,
"constants": true,
"crypto": true,
"dgram": true,
"dns": true,
"domain": true,
"events": true,
"fs": true,
"http": true,
"http2": true,
"https": true,
"inspector": true,
"module": true,
"net": true,
"os": true,
"path": true,
"perf_hooks": true,
"process": true,
"punycode": true,
"querystring": true,
"readline": true,
"repl": true,
"stream": true,
"string_decoder": true,
"timers": true,
"tls": true,
"trace_events": true,
"tty": true,
"url": true,
"util": true,
"v8": true,
"vm": true,
"wasi": true,
"worker_threads": true,
"zlib": true,
}
// maps resolve.Resolver -> *JS
// Resolver is an interface that language extensions can implement to resolve
// dependencies in rules they generate.
// Imports returns a list of ImportSpecs that can be used to import the rule
// r. This is used to populate RuleIndex.
//
// If nil is returned, the rule will not be indexed. If any non-nil slice is
// returned, including an empty slice, the rule will be indexed.
func (lang *NestJS) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
nestjsConfigs := c.Exts[languageName].(NestjsConfigs)
nestjsConfig := nestjsConfigs[f.Pkg]
srcs := r.AttrStrings("srcs")
importSpecs := make([]resolve.ImportSpec, 0)
// index each source file
for _, src := range srcs {
filePath := path.Join(f.Pkg, src)
importSpecs = append(importSpecs, resolve.ImportSpec{
Lang: lang.Name(),
Imp: filePath,
})
}
isBarrel := false
// look for index.js and mark this rule as a module rule
for _, src := range srcs {
if isBarrelFile(src) {
isBarrel = true
break
}
}
// modules can be resolved via the directory containing them
if isBarrel {
importSpecs = append(importSpecs, resolve.ImportSpec{
Lang: lang.Name(),
Imp: f.Pkg,
})
}
// Any subfolders could be used to depend on this rule
folderImports := nestjsConfig.CollectAll && (r.Kind() == getKind(c, "ts_project") || r.Kind() == getKind(c, "js_library"))
if folderImports {
base := filepath.Dir(f.Path)
subDirectories := make(map[string]bool)
for _, src := range srcs {
dir := filepath.Dir(src)
subDirectories[dir] = true
}
for subDirectory := range subDirectories {
root := strings.TrimSuffix(base, f.Pkg)
relPath := strings.TrimPrefix(subDirectory, root)
path := fmt.Sprintf("%s/%s", f.Pkg, relPath)
importSpecs = append(importSpecs, resolve.ImportSpec{
Lang: lang.Name(),
Imp: path,
})
}
}
log.Print(Info("Indexing %s, imports: %+v", r.Name(), importSpecs))
return importSpecs
}
// Embeds returns a list of labels of rules that the given rule embeds. If
// a rule is embedded by another importable rule of the same language, only
// the embedding rule will be indexed. The embedding rule will inherit
// the imports of the embedded rule.
func (*NestJS) Embeds(r *rule.Rule, from label.Label) []label.Label {
return nil
}
// Resolve translates imported libraries for a given rule into Bazel
// dependencies. Information about imported libraries is returned for each
// rule generated by language.GenerateRules in
// language.GenerateResult.Imports. Resolve generates a "deps" attribute (or
// the appropriate language-specific equivalent) for each import according to
// language-specific rules and heuristics.
// https://www.typescriptlang.org/docs/handbook/module-resolution.html#classic
func (lang *NestJS) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, _imports interface{}, from label.Label) {
log.Print(Info("Resolving %s, imports: %+v", r.Name(), _imports))
nestjsConfigs := c.Exts[languageName].(NestjsConfigs)
nestjsConfig := nestjsConfigs[from.Pkg]
packageJSON := "//:package"
packageResolveResult := lang.tryResolve("package.json", c, ix, from)
if packageResolveResult.err != nil {
log.Print(Err("%v", packageResolveResult.err))
return
}
if packageResolveResult.selfImport {
// ignore self imports
return
}
if packageResolveResult.label != label.NoLabel {
// add discovered label
lbl := packageResolveResult.label
packageJSON = lbl.Abs(from.Repo, from.Pkg).String()
}
imports := _imports.(*imports)
depSet := make(map[string]bool)
dataSet := make(map[string]bool)
for name := range imports.set {
// is it a package.json import?
if name == "package" || name == "package.json" {
depSet[packageJSON] = true
continue
}
// fix aliases
match := nestjsConfig.ImportAliasPattern.FindStringSubmatch(name)
if len(match) > 0 {
prefix := match[0]
alias := ""
for _, impAlias := range nestjsConfig.ImportAliases {
if impAlias.From == prefix {
alias = impAlias.To
break
}
}
name = alias + strings.TrimPrefix(name, prefix)
}
// is it an npm dependency?
isNpm, npmLabel, devDep := lang.isNpmDependency(name, nestjsConfig)
if isNpm {
s := strings.Split(name, "/")
name = s[0]
if strings.HasPrefix(name, "@") && len(s) >= 2 {
name += "/" + s[1]
}
depSet[fmt.Sprintf("%s/%s", npmLabel, name)] = true
if !devDep {
// Runtime dependency
dataSet[fmt.Sprintf("%s/%s", npmLabel, name)] = true
}
if nestjsConfig.LookupTypes && r.Kind() == "ts_project" {
// does it have a corresponding @types/[...] declaration?
typesFound, npmLabel, _ := lang.isNpmDependency("@types/"+name, nestjsConfig)
if typesFound {
depSet[fmt.Sprintf("%s@types/%s", npmLabel, name)] = true
}
}
continue
}
// is it a builtin?
if _, ok := BUILTINS[name]; ok || strings.HasPrefix(name, "node:") {
// add @types/node when using node.js builtin and have @types/nodes installed
if nestjsConfig.LookupTypes && r.Kind() == "ts_project" {
typesFound, npmLabel, _ := lang.isNpmDependency("@types/node", nestjsConfig)
if typesFound {
depSet[fmt.Sprintf("%s@types/node", npmLabel)] = true
}
}
continue
}
// Is user resolved
resolveResult := lang.tryResolve(name, c, ix, from)
if resolveResult.err == nil && !resolveResult.selfImport && resolveResult.label != label.NoLabel {
// add discovered label
lbl := resolveResult.label
dep := lbl.Rel(from.Repo, from.Pkg).String()
depSet[dep] = true
continue
}
lang.resolveWalkParents(name, depSet, dataSet, c, ix, rc, r, from)
}
// Add in additional jest dependencies
if r.Kind() == getKind(c, "jest_test") {
// All deps are also data for jest_test rules.
for name := range depSet {
dataSet[name] = true
}
for name, npmLabel := range nestjsConfig.NpmDependencies.DevDependencies {
if name == "jest-cli" || name == "jest-junit" {
continue
}
if strings.HasPrefix(name, "@types/jest") {
depSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
dataSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
}
if strings.HasPrefix(name, "jest") {
depSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
dataSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
}
}
isExistDepsAttributeInJestRule := lang.Kinds()["jest_test"].NonEmptyAttrs["deps"]
// Ignore deps attribute if it is not exist in jest_test rule
if !isExistDepsAttributeInJestRule {
depSet = make(map[string]bool)
}
packageLocation := nestjsConfig.RootPkg
if packageLocation == "." {
packageLocation = ""
dataSet[fmt.Sprintf("//%s:package_json", packageLocation)] = true
}
}
// Add in additional jest dependencies for ts_project for testing
if r.Kind() == getKind(c, "ts_project") && strings.HasSuffix(r.Name(), "_test") {
// All deps are also data for jest_test rules.
for name := range depSet {
dataSet[name] = true
}
for name, npmLabel := range nestjsConfig.NpmDependencies.DevDependencies {
if name == "jest-cli" || name == "jest-junit" {
continue
}
if strings.HasPrefix(name, "@types/jest") {
depSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
dataSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
}
if strings.HasPrefix(name, "jest") {
depSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
dataSet[fmt.Sprintf("%s%s", npmLabel, name)] = true
}
}
}
if r.Kind() == getKind(c, "npm_package") {
// All deps are also data for npm_package rules.
data := []string{}
for name := range depSet {
data = append(data, name)
}
if len(r.AttrStrings("srcs")) > 0 {
data = append(data, r.AttrStrings("srcs")...)
r.SetAttr("srcs", data)
} else {
r.SetAttr("srcs", data)
}
depSet = make(map[string]bool)
}
deps := []string{}
for dep := range depSet {
deps = append(deps, dep)
}
if len(deps) > 0 {
r.SetAttr("deps", deps)
} else {
r.DelAttr("deps")
}
data := []string{}
for d := range dataSet {
data = append(data, d)
}
if len(data) > 0 {
r.SetAttr("data", data)
} else {
r.DelAttr("data")
}
log.Print(Info("Resolved %s, imports: %+v, deps: %+v, data: %+v", r.Name(), _imports, deps, data))
}
func (lang *NestJS) resolveWalkParents(name string, depSet map[string]bool, dataSet map[string]bool, c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, from label.Label) {
nestjsConfigs := c.Exts[languageName].(NestjsConfigs)
nestjsConfig := nestjsConfigs[from.Pkg]
parents := ""
tries := []string{}
for {
if name == "package" {
name = "package.json"
}
if name == "." {
name = "index"
}
localDir := path.Join(from.Pkg, parents)
target := path.Join(localDir, name)
// add supported extensions to target name to get a filePath
extraExtensionsToTry := []string{""}
if !lang.isWebAsset(nestjsConfig, target) {
extraExtensionsToTry = append(append(extraExtensionsToTry, tsExtensions...), jsExtensions...)
}
for _, ext := range extraExtensionsToTry {
filePath := target + ext
tries = append(tries, filePath)
// try to find a rule providing the filePath
resolveResult := lang.tryResolve(filePath, c, ix, from)
if resolveResult.err != nil {
log.Print(Err("%v", resolveResult.err))
return
}
if resolveResult.selfImport {
// ignore self imports
return
}
if resolveResult.label != label.NoLabel {
// add discovered label
lbl := resolveResult.label
dep := lbl.Rel(from.Repo, from.Pkg).String()
if !lang.isWebAsset(nestjsConfig, filePath) {
depSet[dep] = true
} else {
dataSet[dep] = true
}
return
}
if resolveResult.fileName != "" {
// add discovered file
pkgName := path.Dir(target)
data := fmt.Sprintf("//%s:%s", pkgName, resolveResult.fileName)
dataSet[data] = true
return
}
}
if nestjsConfig.RootPkg == localDir || localDir == "." {
// unable to resolve import
if !nestjsConfig.Quiet {
log.Print(Err("[%s] import %v not found", from.Abs(from.Repo, from.Pkg).String(), name))
}
if nestjsConfig.Verbose {
log.Print(Warn("tried node_modules/%s", name))
for _, try := range tries {
log.Print(Warn("tried %s", try))
}
}
return
}
// continue to search one directory higher
parents += "../"
}
}
// https://nodejs.org/api/modules.html#modules_all_together
func (lang *NestJS) isNpmDependency(imp string, nestjsConfig *NestjsConfig) (bool, string, bool) {
// These prefixes cannot be NPM dependencies
var prefixes = []string{".", "/", "../", "~/", "@/", "~~/"}
if hasPrefix(prefixes, imp) {
return false, "", false
}
// Grab the first part of the import (ie "foo/bar" -> "foo")
packageRoot := imp
for i := range imp {
if imp[i] == '/' {
prefix := imp[:i]
if prefix == "@types" {
continue
} else {
packageRoot = prefix
break
}
}
}
// Is the package root found in package.json ?
if npmLabel, ok := nestjsConfig.NpmDependencies.Dependencies[packageRoot]; ok {
return true, npmLabel, false
}
if npmLabel, ok := nestjsConfig.NpmDependencies.DevDependencies[packageRoot]; ok {
return true, npmLabel, true
}
// Assume all @ imports are npm dependencies
if strings.HasPrefix(imp, "@types/") {
// Need to ignore @types/, since these are checked greedily
return false, "", false
}
if strings.HasPrefix(imp, "@") {
return true, nestjsConfig.DefaultNpmLabel, false
}
return false, "", false
}
func hasPrefix(suffixes []string, x string) bool {
for _, suffix := range suffixes {
if strings.HasPrefix(x, suffix) {
return true
}
}
return false
}
type resolveResult struct {
label label.Label
selfImport bool
fileName string
err error
}
func (lang *NestJS) tryResolve(target string, c *config.Config, ix *resolve.RuleIndex, from label.Label) resolveResult {
importSpec := resolve.ImportSpec{
Lang: lang.Name(),
Imp: target,
}
if override, ok := resolve.FindRuleWithOverride(c, importSpec, lang.Name()); ok {
if override.Repo == "" {
override.Repo = from.Repo
}
if !override.Equal(from) {
if override.Repo == from.Repo {
override.Repo = ""
}
return resolveResult{
label: override,
selfImport: false,
fileName: "",
err: nil,
}
}
}
matches := ix.FindRulesByImportWithConfig(c, importSpec, lang.Name())
// too many matches
if len(matches) > 1 {
return resolveResult{
label: label.NoLabel,
selfImport: false,
fileName: "",
err: fmt.Errorf("multiple rules (%s and %s) provide %s", matches[0].Label, matches[1].Label, target),
}
}
// no matches
if len(matches) == 0 {
// no rule is found for this file
// it could be a regular file w/o a target
if fileInfo, err := os.Stat(path.Join(c.RepoRoot, target)); err == nil && !fileInfo.IsDir() {
// found a file matching the target
return resolveResult{
label: label.NoLabel,
selfImport: false,
fileName: fileInfo.Name(),
err: nil,
}
}
return resolveResult{
label: label.NoLabel,
selfImport: false,
fileName: "",
err: nil,
}
}
if matches[0].IsSelfImport(from) {
return resolveResult{
label: label.NoLabel,
selfImport: true,
fileName: "",
err: nil,
}
}
return resolveResult{
label: matches[0].Label,
selfImport: false,
fileName: "",
err: nil,
}
}