lang.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 provides support for Go and Go proto rules. It generates
  13. // go_library, go_binary, go_test, and go_proto_library rules.
  14. //
  15. // Configuration
  16. //
  17. // Go rules support the flags -build_tags, -go_prefix, and -external.
  18. // They also support the directives # gazelle:build_tags, # gazelle:prefix,
  19. // and # gazelle:importmap_prefix. See
  20. // https://github.com/bazelbuild/bazel-gazelle/blob/master/README.rst#directives
  21. // for information on these.
  22. //
  23. // Rule generation
  24. //
  25. // Currently, Gazelle generates rules for one Go package per directory. In
  26. // general, we aim to support Go code which is compatible with "go build". If
  27. // there are no buildable packages, Gazelle will delete existing rules with
  28. // default names. If there are multiple packages, Gazelle will pick one that
  29. // matches the directory name or will print an error if no such package is
  30. // found.
  31. //
  32. // Gazelle names library and test rules somewhat oddly: go_default_library, and
  33. // go_default_test. This is for historic reasons: before the importpath
  34. // attribute was mandatory, import paths were inferred from label names. Even if
  35. // we never support multiple packages in the future (we should), we should
  36. // migrate away from this because it's surprising. Libraries should generally
  37. // be named after their directories.
  38. //
  39. // Dependency resolution
  40. //
  41. // Go libraries are indexed by their importpath attribute. Gazelle attempts to
  42. // resolve libraries by import path using the index, filtered using the
  43. // vendoring algorithm. If an import doesn't match any known library, Gazelle
  44. // guesses a name for it, locally (if the import path is under the current
  45. // prefix), or in an external repository or vendor directory (depending
  46. // on external mode).
  47. //
  48. // Gazelle has special cases for import paths associated with proto Well
  49. // Known Types and Google APIs. rules_go declares canonical rules for these.
  50. package golang
  51. import "github.com/bazelbuild/bazel-gazelle/internal/language"
  52. const goName = "go"
  53. type goLang struct {
  54. // goPkgDirs is a set of relative paths to directories containing buildable
  55. // Go code, including in subdirectories.
  56. goPkgRels map[string]bool
  57. }
  58. func (_ *goLang) Name() string { return goName }
  59. func New() language.Language {
  60. return &goLang{goPkgRels: make(map[string]bool)}
  61. }