index.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Copyright 2018 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package resolve
  13. import (
  14. "log"
  15. "github.com/bazelbuild/bazel-gazelle/internal/config"
  16. "github.com/bazelbuild/bazel-gazelle/internal/label"
  17. "github.com/bazelbuild/bazel-gazelle/internal/repos"
  18. "github.com/bazelbuild/bazel-gazelle/internal/rule"
  19. )
  20. // ImportSpec describes a library to be imported. Imp is an import string for
  21. // the library. Lang is the language in which the import string appears (this
  22. // should match Resolver.Name).
  23. type ImportSpec struct {
  24. Lang, Imp string
  25. }
  26. // Resolver is an interface that language extensions can implement to resolve
  27. // dependencies in rules they generate.
  28. type Resolver interface {
  29. // Name returns the name of the language. This should be a prefix of the
  30. // kinds of rules generated by the language, e.g., "go" for the Go extension
  31. // since it generates "go_library" rules.
  32. Name() string
  33. // Imports returns a list of ImportSpecs that can be used to import the rule
  34. // r. This is used to populate RuleIndex.
  35. //
  36. // If nil is returned, the rule will not be indexed. If any non-nil slice is
  37. // returned, including an empty slice, the rule will be indexed.
  38. Imports(c *config.Config, r *rule.Rule, f *rule.File) []ImportSpec
  39. // Embeds returns a list of labels of rules that the given rule embeds. If
  40. // a rule is embedded by another importable rule of the same language, only
  41. // the embedding rule will be indexed. The embedding rule will inherit
  42. // the imports of the embedded rule.
  43. Embeds(r *rule.Rule, from label.Label) []label.Label
  44. // Resolve translates imported libraries for a given rule into Bazel
  45. // dependencies. A list of imported libraries is typically stored in a
  46. // private attribute of the rule when it's generated (this interface doesn't
  47. // dictate how that is stored or represented). Resolve generates a "deps"
  48. // attribute (or the appropriate language-specific equivalent) for each
  49. // import according to language-specific rules and heuristics.
  50. Resolve(c *config.Config, ix *RuleIndex, rc *repos.RemoteCache, r *rule.Rule, from label.Label)
  51. }
  52. // RuleIndex is a table of rules in a workspace, indexed by label and by
  53. // import path. Used by Resolver to map import paths to labels.
  54. type RuleIndex struct {
  55. rules []*ruleRecord
  56. labelMap map[label.Label]*ruleRecord
  57. importMap map[ImportSpec][]*ruleRecord
  58. kindToResolver map[string]Resolver
  59. }
  60. // ruleRecord contains information about a rule relevant to import indexing.
  61. type ruleRecord struct {
  62. rule *rule.Rule
  63. label label.Label
  64. // importedAs is a list of ImportSpecs by which this rule may be imported.
  65. // Used to build a map from ImportSpecs to ruleRecords.
  66. importedAs []ImportSpec
  67. // embeds is the transitive closure of labels for rules that this rule embeds
  68. // (as determined by the Embeds method). This only includes rules in the same
  69. // language (i.e., it includes a go_library embedding a go_proto_library, but
  70. // not a go_proto_library embedding a proto_library).
  71. embeds []label.Label
  72. // embedded indicates whether another rule of the same language embeds this
  73. // rule. Embedded rules should not be indexed.
  74. embedded bool
  75. didCollectEmbeds bool
  76. }
  77. // NewRuleIndex creates a new index.
  78. //
  79. // kindToResolver is a map from rule kinds (for example, "go_library") to
  80. // Resolvers that support those kinds.
  81. func NewRuleIndex(kindToResolver map[string]Resolver) *RuleIndex {
  82. return &RuleIndex{
  83. labelMap: make(map[label.Label]*ruleRecord),
  84. kindToResolver: kindToResolver,
  85. }
  86. }
  87. // AddRule adds a rule r to the index. The rule will only be indexed if there
  88. // is a known resolver for the rule's kind and Resolver.Imports returns a
  89. // non-nil slice.
  90. //
  91. // AddRule may only be called before Finish.
  92. func (ix *RuleIndex) AddRule(c *config.Config, r *rule.Rule, f *rule.File) {
  93. var imps []ImportSpec
  94. if rslv, ok := ix.kindToResolver[r.Kind()]; ok {
  95. imps = rslv.Imports(c, r, f)
  96. }
  97. // If imps == nil, the rule is not importable. If imps is the empty slice,
  98. // it may still be importable if it embeds importable libraries.
  99. if imps == nil {
  100. return
  101. }
  102. record := &ruleRecord{
  103. rule: r,
  104. label: label.New(c.RepoName, f.Pkg, r.Name()),
  105. importedAs: imps,
  106. }
  107. if _, ok := ix.labelMap[record.label]; ok {
  108. log.Printf("multiple rules found with label %s", record.label)
  109. return
  110. }
  111. ix.rules = append(ix.rules, record)
  112. ix.labelMap[record.label] = record
  113. }
  114. // Finish constructs the import index and performs any other necessary indexing
  115. // actions after all rules have been added. This step is necessary because
  116. // a rule may be indexed differently based on what rules are added later.
  117. //
  118. // Finish must be called after all AddRule calls and before any
  119. // FindRulesByImport calls.
  120. func (ix *RuleIndex) Finish() {
  121. for _, r := range ix.rules {
  122. ix.collectEmbeds(r)
  123. }
  124. ix.buildImportIndex()
  125. }
  126. func (ix *RuleIndex) collectEmbeds(r *ruleRecord) {
  127. if r.didCollectEmbeds {
  128. return
  129. }
  130. r.didCollectEmbeds = true
  131. embedLabels := ix.kindToResolver[r.rule.Kind()].Embeds(r.rule, r.label)
  132. r.embeds = embedLabels
  133. for _, e := range embedLabels {
  134. er, ok := ix.findRuleByLabel(e, r.label)
  135. if !ok {
  136. continue
  137. }
  138. ix.collectEmbeds(er)
  139. if ix.kindToResolver[r.rule.Kind()] == ix.kindToResolver[er.rule.Kind()] {
  140. er.embedded = true
  141. r.embeds = append(r.embeds, er.embeds...)
  142. }
  143. r.importedAs = append(r.importedAs, er.importedAs...)
  144. }
  145. }
  146. // buildImportIndex constructs the map used by FindRulesByImport.
  147. func (ix *RuleIndex) buildImportIndex() {
  148. ix.importMap = make(map[ImportSpec][]*ruleRecord)
  149. for _, r := range ix.rules {
  150. if r.embedded {
  151. continue
  152. }
  153. indexed := make(map[ImportSpec]bool)
  154. for _, imp := range r.importedAs {
  155. if indexed[imp] {
  156. continue
  157. }
  158. indexed[imp] = true
  159. ix.importMap[imp] = append(ix.importMap[imp], r)
  160. }
  161. }
  162. }
  163. func (ix *RuleIndex) findRuleByLabel(label label.Label, from label.Label) (*ruleRecord, bool) {
  164. label = label.Abs(from.Repo, from.Pkg)
  165. r, ok := ix.labelMap[label]
  166. return r, ok
  167. }
  168. type FindResult struct {
  169. // Label is the absolute label (including repository and package name) for
  170. // a matched rule.
  171. Label label.Label
  172. // Embeds is the transitive closure of labels for rules that the matched
  173. // rule embeds. It may contains duplicates and does not include the label
  174. // for the rule itself.
  175. Embeds []label.Label
  176. }
  177. // FindRulesByImport attempts to resolve an import string to a rule record.
  178. // imp is the import to resolve (which includes the target language). lang is
  179. // the language of the rule with the dependency (for example, in
  180. // go_proto_library, imp will have ProtoLang and lang will be GoLang).
  181. // from is the rule which is doing the dependency. This is used to check
  182. // vendoring visibility and to check for self-imports.
  183. //
  184. // FindRulesByImport returns a list of rules, since any number of rules may
  185. // provide the same import. Callers may need to resolve ambiguities using
  186. // language-specific heuristics.
  187. func (ix *RuleIndex) FindRulesByImport(imp ImportSpec, lang string) []FindResult {
  188. matches := ix.importMap[imp]
  189. results := make([]FindResult, 0, len(matches))
  190. for _, m := range matches {
  191. if ix.kindToResolver[m.rule.Kind()].Name() != lang {
  192. continue
  193. }
  194. results = append(results, FindResult{
  195. Label: m.label,
  196. Embeds: m.embeds,
  197. })
  198. }
  199. return results
  200. }
  201. // IsSelfImport returns true if the result's label matches the given label
  202. // or the result's rule transitively embeds the rule with the given label.
  203. // Self imports cause cyclic dependencies, so the caller may want to omit
  204. // the dependency or report an error.
  205. func (r FindResult) IsSelfImport(from label.Label) bool {
  206. if from.Equal(r.Label) {
  207. return true
  208. }
  209. for _, e := range r.Embeds {
  210. if from.Equal(e) {
  211. return true
  212. }
  213. }
  214. return false
  215. }