github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/lang.go (about) 1 /* Copyright 2018 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 // Package golang provides support for Go and Go proto rules. It generates 17 // go_library, go_binary, go_test, and go_proto_library rules. 18 // 19 // Configuration 20 // 21 // Go rules support the flags -build_tags, -go_prefix, and -external. 22 // They also support the directives # gazelle:build_tags, # gazelle:prefix, 23 // and # gazelle:importmap_prefix. See 24 // https://github.com/bazelbuild/bazel-gazelle/blob/master/README.rst#directives 25 // for information on these. 26 // 27 // Rule generation 28 // 29 // Currently, Gazelle generates rules for one Go package per directory. In 30 // general, we aim to support Go code which is compatible with "go build". If 31 // there are no buildable packages, Gazelle will delete existing rules with 32 // default names. If there are multiple packages, Gazelle will pick one that 33 // matches the directory name or will print an error if no such package is 34 // found. 35 // 36 // Gazelle names library and test rules somewhat oddly: go_default_library, and 37 // go_default_test. This is for historic reasons: before the importpath 38 // attribute was mandatory, import paths were inferred from label names. Even if 39 // we never support multiple packages in the future (we should), we should 40 // migrate away from this because it's surprising. Libraries should generally 41 // be named after their directories. 42 // 43 // Dependency resolution 44 // 45 // Go libraries are indexed by their importpath attribute. Gazelle attempts to 46 // resolve libraries by import path using the index, filtered using the 47 // vendoring algorithm. If an import doesn't match any known library, Gazelle 48 // guesses a name for it, locally (if the import path is under the current 49 // prefix), or in an external repository or vendor directory (depending 50 // on external mode). 51 // 52 // Gazelle has special cases for import paths associated with proto Well 53 // Known Types and Google APIs. rules_go declares canonical rules for these. 54 package golang 55 56 import "github.com/bazelbuild/bazel-gazelle/language" 57 58 const goName = "go" 59 60 type goLang struct { 61 // goPkgRels is a set of relative paths to directories containing buildable 62 // Go code. If the value is false, it means the directory does not contain 63 // buildable Go code, but it has a subdir which does. 64 goPkgRels map[string]bool 65 } 66 67 func (*goLang) Name() string { return goName } 68 69 func NewLanguage() language.Language { 70 return &goLang{goPkgRels: make(map[string]bool)} 71 }