Repository: bazelbuild/bazelisk
Pull request #280 (2021-11-19 19:35:40)

Add support for patch versions of Bazel (e.g. `1.2.3-patch4`)
I have some patches I maintain in a fork of Bazel and generate binaries with `-patch` suffixes followed by a number (e.g. `4.2.1-patch1`) and would like to start using Bazelisk to download those binaries. This mostly works already except that Bazelisk does not accept the `-patch` patch versions. The change is pretty small so thought I'd open a pull requests to learn whether or not this patching pattern was a good idea or if maybe there's a better way. I also understand not everyone may be doing this so am open to other ideas and approaches for using patched Bazel versions.

UebelAndre: @fweikert Didn't realize there was a merge conflict here. That's been resolved and this PR is ready for a review if you can find the time 🙏 

Patch:
diff --git a/go.mod b/go.mod
index fc5437e..112afa0 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/bazelbuild/bazelisk
 go 1.15
 
 require (
-	github.com/bazelbuild/rules_go v0.28.0
+	github.com/bazelbuild/rules_go v0.29.0
 	github.com/hashicorp/go-version v1.3.0
 	github.com/mitchellh/go-homedir v1.1.0
 )
diff --git a/go.sum b/go.sum
index c8ff146..20b426c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/bazelbuild/rules_go v0.28.0 h1:fNtx0dJpG5ENGdMj3/GICoi/7z+ixB3IIW5rERTzOgM=
-github.com/bazelbuild/rules_go v0.28.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
+github.com/bazelbuild/rules_go v0.29.0 h1:SfxjyO/V68rVnzOHop92fB0gv/Aa75KNLAN0PMqXbIw=
+github.com/bazelbuild/rules_go v0.29.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
 github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
 github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
diff --git a/versions/versions.go b/versions/versions.go
index 8ea3a9b..0c4d5f9 100644
--- a/versions/versions.go
+++ b/versions/versions.go
@@ -18,6 +18,7 @@ const (
 
 var (
 	releasePattern       = regexp.MustCompile(`^(\d+)\.(x|\d+\.\d+)$`)
+	patchPattern         = regexp.MustCompile(`^(\d+\.\d+\.\d+)-([\w\d]+)$`)
 	candidatePattern     = regexp.MustCompile(`^(\d+\.\d+\.\d+)rc(\d+)$`)
 	rollingPattern       = regexp.MustCompile(`^\d+\.0\.0-pre\.\d{8}(\.\d+){1,2}$`)
 	latestReleasePattern = regexp.MustCompile(`^latest(?:-(?P<offset>\d+))?$`)
@@ -45,6 +46,8 @@ func Parse(fork, version string) (*Info, error) {
 			vi.IsRelative = true
 			vi.TrackRestriction = track
 		}
+	} else if patchPattern.MatchString(version) {
+		vi.IsRelease = true
 	} else if m := latestReleasePattern.FindStringSubmatch(version); m != nil {
 		vi.IsRelease = true
 		vi.IsRelative = true


Test patch:
diff --git a/bazelisk_version_test.go b/bazelisk_version_test.go
index 1a3e06a..b754f81 100644
--- a/bazelisk_version_test.go
+++ b/bazelisk_version_test.go
@@ -35,6 +35,42 @@ func TestMain(m *testing.M) {
 	os.Exit(code)
 }
 
+func TestResolveVersion(t *testing.T) {
+	s := setUp(t)
+	s.AddVersion("4.0.0", false, nil, nil)
+	s.Finish()
+
+	gcs := &repositories.GCSRepo{}
+	repos := core.CreateRepositories(nil, gcs, nil, nil, nil, false)
+	version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "4.0.0")
+
+	if err != nil {
+		t.Fatalf("Version resolution failed unexpectedly: %v", err)
+	}
+	expectedRC := "4.0.0"
+	if version != expectedRC {
+		t.Fatalf("Expected version %s, but got %s", expectedRC, version)
+	}
+}
+
+func TestResolvePatchVersion(t *testing.T) {
+	s := setUp(t)
+	s.AddVersion("4.0.0-patch1", false, nil, nil)
+	s.Finish()
+
+	gcs := &repositories.GCSRepo{}
+	repos := core.CreateRepositories(nil, gcs, nil, nil, nil, false)
+	version, _, err := repos.ResolveVersion(tmpDir, versions.BazelUpstream, "4.0.0-patch1")
+
+	if err != nil {
+		t.Fatalf("Version resolution failed unexpectedly: %v", err)
+	}
+	expectedRC := "4.0.0-patch1"
+	if version != expectedRC {
+		t.Fatalf("Expected version %s, but got %s", expectedRC, version)
+	}
+}
+
 func TestResolveLatestRcVersion(t *testing.T) {
 	s := setUp(t)
 	s.AddVersion("4.0.0", false, nil, nil)