@@ -57,7 +57,18 @@ func (*Resolver) Name() string { return languageName }
5757func (py * Resolver ) Imports (c * config.Config , r * rule.Rule , f * rule.File ) []resolve.ImportSpec {
5858 cfgs := c .Exts [languageName ].(pythonconfig.Configs )
5959 cfg := cfgs [f .Pkg ]
60+
6061 srcs := r .AttrStrings ("srcs" )
62+ if srcs != nil {
63+ return importsSrcLibrary (cfg , srcs , f )
64+ } else if isProtoLibrary (r ) {
65+ return importsProtoLibrary (cfg , r , f )
66+ }
67+
68+ return nil
69+ }
70+
71+ func importsSrcLibrary (cfg * pythonconfig.Config , srcs []string , f * rule.File ) []resolve.ImportSpec {
6172 provides := make ([]resolve.ImportSpec , 0 , len (srcs )+ 1 )
6273 for _ , src := range srcs {
6374 ext := filepath .Ext (src )
@@ -114,6 +125,42 @@ func importSpecFromSrc(pythonProjectRoot, bzlPkg, src string) resolve.ImportSpec
114125 }
115126}
116127
128+ func isProtoLibrary (r * rule.Rule ) bool {
129+ return r .Kind () == pyProtoLibraryKind
130+ }
131+
132+ func importsProtoLibrary (cfg * pythonconfig.Config , r * rule.Rule , f * rule.File ) []resolve.ImportSpec {
133+ specs := []resolve.ImportSpec {}
134+
135+ // First, determine the root module and emit an import for that,
136+ // i.e. for //foo:foo_py_pb2, we'd get foo.foo_pb2
137+ protoRuleAttr := r .PrivateAttr (protoKey )
138+ protoRelAttr := r .PrivateAttr (protoRelKey )
139+ if protoRuleAttr == nil || protoRelAttr == nil {
140+ return specs
141+ }
142+
143+ protoRule := protoRuleAttr .(string )
144+ generatedPbFileName := strings .TrimSuffix (protoRule , "_proto" ) + "_pb2.py"
145+ protoRel := protoRelAttr .(string )
146+
147+ specs = append (specs , importSpecFromSrc (cfg .PythonProjectRoot (), protoRel , generatedPbFileName ))
148+
149+ // TODO: use parsed proto FileInfo to enumerate importable constants, like messages,
150+ // and emit ImportSpec for them
151+ // protoPkg := r.PrivateAttr(proto.PackageKey).(proto.Package)
152+ // for _, protoFileInfo := range protoPkg.Files {
153+ // for _, svc := range protoFileInfo.Services {
154+ // specs = append(specs, resolve.ImportSpec{
155+ // Lang: languageName,
156+ // Imp: fmt.Sprintf("%s.%s", rootPath, svc),
157+ // })
158+ // }
159+ // Repeat for Messages, Enums
160+
161+ return specs
162+ }
163+
117164// Embeds returns a list of labels of rules that the given rule embeds. If
118165// a rule is embedded by another importable rule of the same language, only
119166// the embedding rule will be indexed. The embedding rule will inherit
@@ -210,9 +257,9 @@ func (py *Resolver) Resolve(
210257 baseParts = pkgParts [:len (pkgParts )- (relativeDepth - 1 )]
211258 }
212259 // Build absolute module path
213- absParts := append ([]string {}, baseParts ... ) // base path
214- absParts = append (absParts , fromParts ... ) // subpath from 'from'
215- absParts = append (absParts , imported ) // actual imported symbol
260+ absParts := append ([]string {}, baseParts ... ) // base path
261+ absParts = append (absParts , fromParts ... ) // subpath from 'from'
262+ absParts = append (absParts , imported ) // actual imported symbol
216263
217264 moduleName = strings .Join (absParts , "." )
218265 }
@@ -282,6 +329,9 @@ func (py *Resolver) Resolve(
282329 // Check if the imported module is part of the standard library.
283330 if isStdModule (Module {Name : moduleName }) {
284331 continue MODULES_LOOP
332+ } else if r .Kind () == pyProtoLibraryKind {
333+ // For py_proto_library, fall back to guessing the label based on the proto_library rule name.
334+ matches = py .resolveProtoFallback (cfg )
285335 } else if cfg .ValidateImportStatements () {
286336 err := fmt .Errorf (
287337 "%[1]q, line %[2]d: %[3]q is an invalid dependency: possible solutions:\n " +
@@ -372,6 +422,11 @@ func (py *Resolver) Resolve(
372422 }
373423}
374424
425+ func (* Resolver ) resolveProtoFallback (c * pythonconfig.Config ) []resolve.FindResult {
426+ // TODO
427+ return []resolve.FindResult {}
428+ }
429+
375430// addResolvedDeps adds the pre-resolved dependencies from the rule's private attributes
376431// to the provided deps set.
377432func addResolvedDeps (
0 commit comments