Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(packagesdrv): resolve third party go packages #3332

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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