resolve.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 golang
  13. import (
  14. "errors"
  15. "fmt"
  16. "go/build"
  17. "log"
  18. "path"
  19. "regexp"
  20. "strings"
  21. "github.com/bazelbuild/bazel-gazelle/config"
  22. "github.com/bazelbuild/bazel-gazelle/label"
  23. "github.com/bazelbuild/bazel-gazelle/pathtools"
  24. "github.com/bazelbuild/bazel-gazelle/repo"
  25. "github.com/bazelbuild/bazel-gazelle/resolve"
  26. "github.com/bazelbuild/bazel-gazelle/rule"
  27. )
  28. func (_ *goLang) Imports(_ *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec {
  29. if !isGoLibrary(r.Kind()) {
  30. return nil
  31. }
  32. if importPath := r.AttrString("importpath"); importPath == "" {
  33. return []resolve.ImportSpec{}
  34. } else {
  35. return []resolve.ImportSpec{{goName, importPath}}
  36. }
  37. }
  38. func (_ *goLang) Embeds(r *rule.Rule, from label.Label) []label.Label {
  39. embedStrings := r.AttrStrings("embed")
  40. if isGoProtoLibrary(r.Kind()) {
  41. embedStrings = append(embedStrings, r.AttrString("proto"))
  42. }
  43. embedLabels := make([]label.Label, 0, len(embedStrings))
  44. for _, s := range embedStrings {
  45. l, err := label.Parse(s)
  46. if err != nil {
  47. continue
  48. }
  49. l = l.Abs(from.Repo, from.Pkg)
  50. embedLabels = append(embedLabels, l)
  51. }
  52. return embedLabels
  53. }
  54. func (gl *goLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, importsRaw interface{}, from label.Label) {
  55. if importsRaw == nil {
  56. // may not be set in tests.
  57. return
  58. }
  59. imports := importsRaw.(rule.PlatformStrings)
  60. r.DelAttr("deps")
  61. resolve := ResolveGo
  62. if r.Kind() == "go_proto_library" {
  63. resolve = resolveProto
  64. }
  65. deps, errs := imports.Map(func(imp string) (string, error) {
  66. l, err := resolve(c, ix, rc, imp, from)
  67. if err == skipImportError {
  68. return "", nil
  69. } else if err != nil {
  70. return "", err
  71. }
  72. for _, embed := range gl.Embeds(r, from) {
  73. if embed.Equal(l) {
  74. return "", nil
  75. }
  76. }
  77. l = l.Rel(from.Repo, from.Pkg)
  78. return l.String(), nil
  79. })
  80. for _, err := range errs {
  81. log.Print(err)
  82. }
  83. if !deps.IsEmpty() {
  84. if r.Kind() == "go_proto_library" {
  85. // protos may import the same library multiple times by different names,
  86. // so we need to de-duplicate them. Protos are not platform-specific,
  87. // so it's safe to just flatten them.
  88. r.SetAttr("deps", deps.Flat())
  89. } else {
  90. r.SetAttr("deps", deps)
  91. }
  92. }
  93. }
  94. var (
  95. skipImportError = errors.New("std or self import")
  96. notFoundError = errors.New("rule not found")
  97. )
  98. // ResolveGo resolves a Go import path to a Bazel label, possibly using the
  99. // given rule index and remote cache. Some special cases may be applied to
  100. // known proto import paths, depending on the current proto mode.
  101. //
  102. // This may be used directly by other language extensions related to Go
  103. // (gomock). Gazelle calls Language.Resolve instead.
  104. func ResolveGo(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, imp string, from label.Label) (label.Label, error) {
  105. gc := getGoConfig(c)
  106. pcMode := getProtoMode(c)
  107. if build.IsLocalImport(imp) {
  108. cleanRel := path.Clean(path.Join(from.Pkg, imp))
  109. if build.IsLocalImport(cleanRel) {
  110. return label.NoLabel, fmt.Errorf("relative import path %q from %q points outside of repository", imp, from.Pkg)
  111. }
  112. imp = path.Join(gc.prefix, cleanRel)
  113. }
  114. if IsStandard(imp) {
  115. return label.NoLabel, skipImportError
  116. }
  117. if l, ok := resolve.FindRuleWithOverride(c, resolve.ImportSpec{Lang: "go", Imp: imp}, "go"); ok {
  118. return l, nil
  119. }
  120. if pcMode.ShouldUseKnownImports() {
  121. // These are commonly used libraries that depend on Well Known Types.
  122. // They depend on the generated versions of these protos to avoid conflicts.
  123. // However, since protoc-gen-go depends on these libraries, we generate
  124. // its rules in disable_global mode (to avoid cyclic dependency), so the
  125. // "go_default_library" versions of these libraries depend on the
  126. // pre-generated versions of the proto libraries.
  127. switch imp {
  128. case "github.com/golang/protobuf/proto":
  129. return label.New("com_github_golang_protobuf", "proto", "go_default_library"), nil
  130. case "github.com/golang/protobuf/jsonpb":
  131. return label.New("com_github_golang_protobuf", "jsonpb", "go_default_library_gen"), nil
  132. case "github.com/golang/protobuf/descriptor":
  133. return label.New("com_github_golang_protobuf", "descriptor", "go_default_library_gen"), nil
  134. case "github.com/golang/protobuf/ptypes":
  135. return label.New("com_github_golang_protobuf", "ptypes", "go_default_library_gen"), nil
  136. case "github.com/golang/protobuf/protoc-gen-go/generator":
  137. return label.New("com_github_golang_protobuf", "protoc-gen-go/generator", "go_default_library_gen"), nil
  138. case "google.golang.org/grpc":
  139. return label.New("org_golang_google_grpc", "", "go_default_library"), nil
  140. }
  141. if l, ok := knownGoProtoImports[imp]; ok {
  142. return l, nil
  143. }
  144. }
  145. if l, err := resolveWithIndexGo(ix, imp, from); err == nil || err == skipImportError {
  146. return l, err
  147. } else if err != notFoundError {
  148. return label.NoLabel, err
  149. }
  150. // Special cases for rules_go and bazel_gazelle.
  151. // These have names that don't following conventions and they're
  152. // typeically declared with http_archive, not go_repository, so Gazelle
  153. // won't recognize them.
  154. if pathtools.HasPrefix(imp, "github.com/bazelbuild/rules_go") {
  155. pkg := pathtools.TrimPrefix(imp, "github.com/bazelbuild/rules_go")
  156. return label.New("io_bazel_rules_go", pkg, "go_default_library"), nil
  157. } else if pathtools.HasPrefix(imp, "github.com/bazelbuild/bazel-gazelle") {
  158. pkg := pathtools.TrimPrefix(imp, "github.com/bazelbuild/bazel-gazelle")
  159. return label.New("bazel_gazelle", pkg, "go_default_library"), nil
  160. }
  161. if !c.IndexLibraries {
  162. // packages in current repo were not indexed, relying on prefix to decide what may have been in
  163. // current repo
  164. if pathtools.HasPrefix(imp, gc.prefix) {
  165. pkg := path.Join(gc.prefixRel, pathtools.TrimPrefix(imp, gc.prefix))
  166. return label.New("", pkg, defaultLibName), nil
  167. }
  168. }
  169. if gc.depMode == externalMode {
  170. return resolveExternal(gc.moduleMode, rc, imp)
  171. } else {
  172. return resolveVendored(rc, imp)
  173. }
  174. }
  175. // IsStandard returns whether a package is in the standard library.
  176. func IsStandard(imp string) bool {
  177. return stdPackages[imp]
  178. }
  179. func resolveWithIndexGo(ix *resolve.RuleIndex, imp string, from label.Label) (label.Label, error) {
  180. matches := ix.FindRulesByImport(resolve.ImportSpec{Lang: "go", Imp: imp}, "go")
  181. var bestMatch resolve.FindResult
  182. var bestMatchIsVendored bool
  183. var bestMatchVendorRoot string
  184. var matchError error
  185. for _, m := range matches {
  186. // Apply vendoring logic for Go libraries. A library in a vendor directory
  187. // is only visible in the parent tree. Vendored libraries supercede
  188. // non-vendored libraries, and libraries closer to from.Pkg supercede
  189. // those further up the tree.
  190. isVendored := false
  191. vendorRoot := ""
  192. parts := strings.Split(m.Label.Pkg, "/")
  193. for i := len(parts) - 1; i >= 0; i-- {
  194. if parts[i] == "vendor" {
  195. isVendored = true
  196. vendorRoot = strings.Join(parts[:i], "/")
  197. break
  198. }
  199. }
  200. if isVendored {
  201. }
  202. if isVendored && !label.New(m.Label.Repo, vendorRoot, "").Contains(from) {
  203. // vendor directory not visible
  204. continue
  205. }
  206. if bestMatch.Label.Equal(label.NoLabel) || isVendored && (!bestMatchIsVendored || len(vendorRoot) > len(bestMatchVendorRoot)) {
  207. // Current match is better
  208. bestMatch = m
  209. bestMatchIsVendored = isVendored
  210. bestMatchVendorRoot = vendorRoot
  211. matchError = nil
  212. } else if (!isVendored && bestMatchIsVendored) || (isVendored && len(vendorRoot) < len(bestMatchVendorRoot)) {
  213. // Current match is worse
  214. } else {
  215. // Match is ambiguous
  216. // TODO: consider listing all the ambiguous rules here.
  217. matchError = fmt.Errorf("rule %s imports %q which matches multiple rules: %s and %s. # gazelle:resolve may be used to disambiguate", from, imp, bestMatch.Label, m.Label)
  218. }
  219. }
  220. if matchError != nil {
  221. return label.NoLabel, matchError
  222. }
  223. if bestMatch.Label.Equal(label.NoLabel) {
  224. return label.NoLabel, notFoundError
  225. }
  226. if bestMatch.IsSelfImport(from) {
  227. return label.NoLabel, skipImportError
  228. }
  229. return bestMatch.Label, nil
  230. }
  231. var modMajorRex = regexp.MustCompile(`/v\d+(?:/|$)`)
  232. func resolveExternal(moduleMode bool, rc *repo.RemoteCache, imp string) (label.Label, error) {
  233. // If we're in module mode, use "go list" to find the module path and
  234. // repository name. Otherwise, use special cases (for github.com, golang.org)
  235. // or send a GET with ?go-get=1 to find the root. If the path contains
  236. // a major version suffix (e.g., /v2), treat it as a module anyway though.
  237. //
  238. // Eventually module mode will be the only mode. But for now, it's expensive
  239. // and not the common case, especially when known repositories aren't
  240. // listed in WORKSPACE (which is currently the case within go_repository).
  241. if !moduleMode {
  242. moduleMode = pathWithoutSemver(imp) != ""
  243. }
  244. var prefix, repo string
  245. var err error
  246. if moduleMode {
  247. prefix, repo, err = rc.Mod(imp)
  248. } else {
  249. prefix, repo, err = rc.Root(imp)
  250. }
  251. if err != nil {
  252. return label.NoLabel, err
  253. }
  254. var pkg string
  255. if pathtools.HasPrefix(imp, prefix) {
  256. pkg = pathtools.TrimPrefix(imp, prefix)
  257. } else if impWithoutSemver := pathWithoutSemver(imp); pathtools.HasPrefix(impWithoutSemver, prefix) {
  258. // We may have used minimal module compatibility to resolve a path
  259. // without a semantic import version suffix to a repository that has one.
  260. pkg = pathtools.TrimPrefix(impWithoutSemver, prefix)
  261. }
  262. return label.New(repo, pkg, defaultLibName), nil
  263. }
  264. func resolveVendored(rc *repo.RemoteCache, imp string) (label.Label, error) {
  265. return label.New("", path.Join("vendor", imp), defaultLibName), nil
  266. }
  267. func resolveProto(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, imp string, from label.Label) (label.Label, error) {
  268. pcMode := getProtoMode(c)
  269. if wellKnownProtos[imp] {
  270. return label.NoLabel, skipImportError
  271. }
  272. if l, ok := resolve.FindRuleWithOverride(c, resolve.ImportSpec{Lang: "proto", Imp: imp}, "go"); ok {
  273. return l, nil
  274. }
  275. if l, ok := knownProtoImports[imp]; ok && pcMode.ShouldUseKnownImports() {
  276. if l.Equal(from) {
  277. return label.NoLabel, skipImportError
  278. } else {
  279. return l, nil
  280. }
  281. }
  282. if l, err := resolveWithIndexProto(ix, imp, from); err == nil || err == skipImportError {
  283. return l, err
  284. } else if err != notFoundError {
  285. return label.NoLabel, err
  286. }
  287. // As a fallback, guess the label based on the proto file name. We assume
  288. // all proto files in a directory belong to the same package, and the
  289. // package name matches the directory base name. We also assume that protos
  290. // in the vendor directory must refer to something else in vendor.
  291. rel := path.Dir(imp)
  292. if rel == "." {
  293. rel = ""
  294. }
  295. if from.Pkg == "vendor" || strings.HasPrefix(from.Pkg, "vendor/") {
  296. rel = path.Join("vendor", rel)
  297. }
  298. return label.New("", rel, defaultLibName), nil
  299. }
  300. // wellKnownProtos is the set of proto sets for which we don't need to add
  301. // an explicit dependency in go_proto_library.
  302. // TODO(jayconrod): generate from
  303. // @io_bazel_rules_go//proto/wkt:WELL_KNOWN_TYPE_PACKAGES
  304. var wellKnownProtos = map[string]bool{
  305. "google/protobuf/any.proto": true,
  306. "google/protobuf/api.proto": true,
  307. "google/protobuf/compiler/plugin.proto": true,
  308. "google/protobuf/descriptor.proto": true,
  309. "google/protobuf/duration.proto": true,
  310. "google/protobuf/empty.proto": true,
  311. "google/protobuf/field_mask.proto": true,
  312. "google/protobuf/source_context.proto": true,
  313. "google/protobuf/struct.proto": true,
  314. "google/protobuf/timestamp.proto": true,
  315. "google/protobuf/type.proto": true,
  316. "google/protobuf/wrappers.proto": true,
  317. }
  318. func resolveWithIndexProto(ix *resolve.RuleIndex, imp string, from label.Label) (label.Label, error) {
  319. matches := ix.FindRulesByImport(resolve.ImportSpec{Lang: "proto", Imp: imp}, "go")
  320. if len(matches) == 0 {
  321. return label.NoLabel, notFoundError
  322. }
  323. if len(matches) > 1 {
  324. return label.NoLabel, fmt.Errorf("multiple rules (%s and %s) may be imported with %q from %s", matches[0].Label, matches[1].Label, imp, from)
  325. }
  326. if matches[0].IsSelfImport(from) {
  327. return label.NoLabel, skipImportError
  328. }
  329. return matches[0].Label, nil
  330. }
  331. func isGoLibrary(kind string) bool {
  332. return kind == "go_library" || isGoProtoLibrary(kind)
  333. }
  334. func isGoProtoLibrary(kind string) bool {
  335. return kind == "go_proto_library" || kind == "go_grpc_library"
  336. }