github.com/wolfd/bazel-gazelle@v0.14.0/internal/language/proto/resolve.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 proto 17 18 import ( 19 "errors" 20 "fmt" 21 "log" 22 "path" 23 "strings" 24 25 "github.com/bazelbuild/bazel-gazelle/internal/config" 26 "github.com/bazelbuild/bazel-gazelle/internal/label" 27 "github.com/bazelbuild/bazel-gazelle/internal/repos" 28 "github.com/bazelbuild/bazel-gazelle/internal/resolve" 29 "github.com/bazelbuild/bazel-gazelle/internal/rule" 30 ) 31 32 func (_ *protoLang) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve.ImportSpec { 33 rel := f.Pkg 34 srcs := r.AttrStrings("srcs") 35 imports := make([]resolve.ImportSpec, len(srcs)) 36 for i, src := range srcs { 37 imports[i] = resolve.ImportSpec{Lang: "proto", Imp: path.Join(rel, src)} 38 } 39 return imports 40 } 41 42 func (_ *protoLang) Embeds(r *rule.Rule, from label.Label) []label.Label { 43 return nil 44 } 45 46 func (_ *protoLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repos.RemoteCache, r *rule.Rule, from label.Label) { 47 pc := GetProtoConfig(c) 48 importsRaw := r.PrivateAttr(config.GazelleImportsKey) 49 if importsRaw == nil { 50 // may not be set in tests. 51 return 52 } 53 imports := importsRaw.([]string) 54 r.DelAttr("deps") 55 deps := make([]string, 0, len(imports)) 56 for _, imp := range imports { 57 l, err := resolveProto(pc, ix, r, imp, from) 58 if err == skipImportError { 59 continue 60 } else if err != nil { 61 log.Print(err) 62 } else { 63 l = l.Rel(from.Repo, from.Pkg) 64 deps = append(deps, l.String()) 65 } 66 } 67 if len(deps) > 0 { 68 r.SetAttr("deps", deps) 69 } 70 } 71 72 var ( 73 skipImportError = errors.New("std import") 74 notFoundError = errors.New("not found") 75 ) 76 77 func resolveProto(pc *ProtoConfig, ix *resolve.RuleIndex, r *rule.Rule, imp string, from label.Label) (label.Label, error) { 78 if !strings.HasSuffix(imp, ".proto") { 79 return label.NoLabel, fmt.Errorf("can't import non-proto: %q", imp) 80 } 81 82 if l, ok := knownImports[imp]; ok && pc.Mode.ShouldUseKnownImports() { 83 if l.Equal(from) { 84 return label.NoLabel, skipImportError 85 } else { 86 return l, nil 87 } 88 } 89 90 if l, err := resolveWithIndex(ix, imp, from); err == nil || err == skipImportError { 91 return l, err 92 } else if err != notFoundError { 93 return label.NoLabel, err 94 } 95 96 rel := path.Dir(imp) 97 if rel == "." { 98 rel = "" 99 } 100 name := RuleName(rel) 101 return label.New("", rel, name), nil 102 } 103 104 func resolveWithIndex(ix *resolve.RuleIndex, imp string, from label.Label) (label.Label, error) { 105 matches := ix.FindRulesByImport(resolve.ImportSpec{Lang: "proto", Imp: imp}, "proto") 106 if len(matches) == 0 { 107 return label.NoLabel, notFoundError 108 } 109 if len(matches) > 1 { 110 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) 111 } 112 if matches[0].IsSelfImport(from) { 113 return label.NoLabel, skipImportError 114 } 115 return matches[0].Label, nil 116 }