Skip to content

Commit

Permalink
fix(packagesdrv): resolve external/ go packages (#3332)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyDev authored Nov 6, 2022
1 parent 03a8b8e commit c9299f0
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"path/filepath"
"strings"
"regexp"
)

type BazelJSONBuilder struct {
Expand All @@ -32,13 +33,24 @@ const (

var _defaultKinds = []string{"go_library", "go_test", "go_binary"}

var externalRe = regexp.MustCompile(".*\\/external\\/([^\\/]+)(\\/(.*))?\\/([^\\/]+.go)")

func (b *BazelJSONBuilder) fileQuery(filename string) string {
label := filename

if filepath.IsAbs(filename) {
fp, _ := filepath.Rel(b.bazel.WorkspaceRoot(), filename)
filename = fp
label, _ = filepath.Rel(b.bazel.WorkspaceRoot(), filename)
}

if matches := externalRe.FindStringSubmatch(filename); len(matches) == 5 {
// if filepath is for a third party lib, we need to know, what external
// library this file is part of.
matches = append(matches[:2], matches[3:]...)
label = fmt.Sprintf("@%s//%s", matches[1], strings.Join(matches[2:], ":"))
}

kinds := append(_defaultKinds, additionalKinds...)
return fmt.Sprintf(`kind("%s", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), filename)
return fmt.Sprintf(`kind("%s", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
}

func (b *BazelJSONBuilder) packageQuery(importPath string) string {
Expand Down

0 comments on commit c9299f0

Please sign in to comment.