Skip to content

Commit fd66bc3

Browse files
authored
Allow using local Bazel binaries. (bazelbuild#93)
Bazel versions (e.g. in USE_BAZEL_VERSION or .bazelversion) can now refer to absolute paths on the filesystem. As an added convenience, a tilde prefix is expanded to the user's home directory. Example: ```sh $ USE_BAZEL_VERSION="~/bin/bazel-1.0" bazelisk ``` This would tell Bazelisk to not download any binaries and instead just directly use the Bazel binary in `$HOME/bin/bazel-1.0`.
1 parent 12d292f commit fd66bc3

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ go_library(
4343
importpath = "github.com/bazelbuild/bazelisk",
4444
visibility = ["//visibility:private"],
4545
x_defs = {"BazeliskVersion": "{STABLE_VERSION}"},
46-
deps = ["@com_github_hashicorp_go_version//:go_default_library"],
46+
deps = [
47+
"@com_github_hashicorp_go_version//:go_default_library",
48+
"@com_github_mitchellh_go_homedir//:go_default_library",
49+
],
4750
)
4851

4952
go_binary(

WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ go_repository(
3434
sum = "h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=",
3535
version = "v1.1.0",
3636
)
37+
38+
go_repository(
39+
name = "com_github_mitchellh_go_homedir",
40+
importpath = "github.com/mitchellh/go-homedir",
41+
sum = "h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=",
42+
version = "v1.1.0",
43+
)

bazelisk.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"time"
3636

3737
version "github.com/hashicorp/go-version"
38+
homedir "github.com/mitchellh/go-homedir"
3839
)
3940

4041
const (
@@ -61,7 +62,7 @@ func findWorkspaceRoot(root string) string {
6162
return findWorkspaceRoot(parentDirectory)
6263
}
6364

64-
func getBazelForkAndVersion() (string, error) {
65+
func getBazelVersion() (string, error) {
6566
// Check in this order:
6667
// - env var "USE_BAZEL_VERSION" is set to a specific version.
6768
// - env var "USE_NIGHTLY_BAZEL" or "USE_BAZEL_NIGHTLY" is set -> latest
@@ -686,30 +687,44 @@ func main() {
686687
log.Fatalf("could not create directory %s: %v", bazeliskHome, err)
687688
}
688689

689-
bazelForkAndVersion, err := getBazelForkAndVersion()
690+
bazelVersionString, err := getBazelVersion()
690691
if err != nil {
691-
log.Fatalf("could not get Bazel fork and version: %v", err)
692+
log.Fatalf("could not get Bazel version: %v", err)
692693
}
693694

694-
bazelFork, bazelVersion, err := parseBazelForkAndVersion(bazelForkAndVersion)
695+
bazelPath, err := homedir.Expand(bazelVersionString)
695696
if err != nil {
696-
log.Fatalf("could not parse Bazel fork and version: %v", err)
697+
log.Fatalf("could not expand home directory in path: %v", err)
697698
}
698699

699-
resolvedBazelVersion, isCommit, err := resolveVersionLabel(bazeliskHome, bazelFork, bazelVersion)
700-
if err != nil {
701-
log.Fatalf("could not resolve the version '%s' to an actual version number: %v", bazelVersion, err)
702-
}
700+
// If the Bazel version is an absolute path to a Bazel binary in the filesystem, we can
701+
// use it directly. In that case, we don't know which exact version it is, though.
702+
resolvedBazelVersion := "unknown"
703+
isCommit := false
703704

704-
bazelDirectory := filepath.Join(bazeliskHome, "bin", bazelFork)
705-
err = os.MkdirAll(bazelDirectory, 0755)
706-
if err != nil {
707-
log.Fatalf("could not create directory %s: %v", bazelDirectory, err)
708-
}
705+
// If we aren't using a local Bazel binary, we'll have to parse the version string and
706+
// download the version that the user wants.
707+
if !filepath.IsAbs(bazelPath) {
708+
bazelFork, bazelVersion, err := parseBazelForkAndVersion(bazelVersionString)
709+
if err != nil {
710+
log.Fatalf("could not parse Bazel fork and version: %v", err)
711+
}
709712

710-
bazelPath, err := downloadBazel(bazelFork, resolvedBazelVersion, isCommit, bazelDirectory)
711-
if err != nil {
712-
log.Fatalf("could not download Bazel: %v", err)
713+
resolvedBazelVersion, isCommit, err = resolveVersionLabel(bazeliskHome, bazelFork, bazelVersion)
714+
if err != nil {
715+
log.Fatalf("could not resolve the version '%s' to an actual version number: %v", bazelVersion, err)
716+
}
717+
718+
bazelDirectory := filepath.Join(bazeliskHome, "bin", bazelFork)
719+
err = os.MkdirAll(bazelDirectory, 0755)
720+
if err != nil {
721+
log.Fatalf("could not create directory %s: %v", bazelDirectory, err)
722+
}
723+
724+
bazelPath, err = downloadBazel(bazelFork, resolvedBazelVersion, isCommit, bazelDirectory)
725+
if err != nil {
726+
log.Fatalf("could not download Bazel: %v", err)
727+
}
713728
}
714729

715730
args := os.Args[1:]

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module github.com/bazelbuild/bazelisk
22

3-
require github.com/hashicorp/go-version v1.1.0
3+
require (
4+
github.com/hashicorp/go-version v1.1.0
5+
github.com/mitchellh/go-homedir v1.1.0
6+
)
47

58
go 1.13

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu
77
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
88
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=
99
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
10+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
11+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1012
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
1113
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
1214
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

0 commit comments

Comments
 (0)