Skip to content

Commit

Permalink
gopackagesdriver: fix version check with Bazel development versions (#…
Browse files Browse the repository at this point in the history
…3893)

Fixes #3889
  • Loading branch information
jayconrod committed Mar 17, 2024
1 parent 364c118 commit 219b7b6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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

0 comments on commit 219b7b6

Please sign in to comment.