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

gopackagesdriver: fix version check with Bazel development versions #3893

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion go/tools/gopackagesdriver/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func NewBazel(ctx context.Context, bazelBin, workspaceRoot string, bazelStartupF
bazelBin: bazelBin,
workspaceRoot: workspaceRoot,
bazelStartupFlags: bazelStartupFlags,
version: bazelVersion{6, 0, 0}, // assumed until 'bazel info' output parsed
}
if err := b.fillInfo(ctx); err != nil {
return nil, fmt.Errorf("unable to query bazel info: %w", err)
Expand Down Expand Up @@ -198,3 +197,14 @@ func (a bazelVersion) compare(b bazelVersion) int {
}
return 0
}

// isAtLeast returns true if a.compare(b) >= 0 (that is, if a is greater than
// or equal to be) or if a is the zero value.
//
// Development versions of Bazel do not have valid version strings, not even a
// prerelease, so parseBazelVersion fails and returns the zero value. If we
// have such a version, we assume it's newer than whatever we're comparing
// it with.
func (a bazelVersion) isAtLeast(b bazelVersion) bool {
return a.compare(b) >= 0 || a == bazelVersion{}
}
2 changes: 1 addition & 1 deletion go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (b *BazelJSONBuilder) outputGroupsForMode(mode LoadMode) string {

func (b *BazelJSONBuilder) query(ctx context.Context, query string) ([]string, error) {
var bzlmodQueryFlags []string
if b.bazel.version.compare(bazelVersion{6, 4, 0}) >= 0 {
if b.bazel.version.isAtLeast(bazelVersion{6, 4, 0}) {
bzlmodQueryFlags = []string{"--consistent_labels"}
}
queryArgs := concatStringsArrays(bazelQueryFlags, bzlmodQueryFlags, []string{
Expand Down
2 changes: 1 addition & 1 deletion go/tools/gopackagesdriver/packageregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (pr *PackageRegistry) Match(labels []string) ([]string, []*FlatPackage) {

for _, label := range labels {
// When packagesdriver is ran from rules go, rulesGoRepositoryName will just be @
if pr.bazelVersion.compare(bazelVersion{6, 0, 0}) >= 0 &&
if pr.bazelVersion.isAtLeast(bazelVersion{6, 0, 0}) &&
!strings.HasPrefix(label, "@") {
// Canonical labels is only since Bazel 6.0.0
label = fmt.Sprintf("@%s", label)
Expand Down