Skip to content

Commit

Permalink
Allow using local Bazel binaries. (#93)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
philwo authored Oct 11, 2019
1 parent 12d292f commit fd66bc3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ go_library(
importpath = "github.com/bazelbuild/bazelisk",
visibility = ["//visibility:private"],
x_defs = {"BazeliskVersion": "{STABLE_VERSION}"},
deps = ["@com_github_hashicorp_go_version//:go_default_library"],
deps = [
"@com_github_hashicorp_go_version//:go_default_library",
"@com_github_mitchellh_go_homedir//:go_default_library",
],
)

go_binary(
Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ go_repository(
sum = "h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=",
version = "v1.1.0",
)

go_repository(
name = "com_github_mitchellh_go_homedir",
importpath = "github.com/mitchellh/go-homedir",
sum = "h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=",
version = "v1.1.0",
)
49 changes: 32 additions & 17 deletions bazelisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"time"

version "github.com/hashicorp/go-version"
homedir "github.com/mitchellh/go-homedir"
)

const (
Expand All @@ -61,7 +62,7 @@ func findWorkspaceRoot(root string) string {
return findWorkspaceRoot(parentDirectory)
}

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

bazelForkAndVersion, err := getBazelForkAndVersion()
bazelVersionString, err := getBazelVersion()
if err != nil {
log.Fatalf("could not get Bazel fork and version: %v", err)
log.Fatalf("could not get Bazel version: %v", err)
}

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

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

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

bazelPath, err := downloadBazel(bazelFork, resolvedBazelVersion, isCommit, bazelDirectory)
if err != nil {
log.Fatalf("could not download Bazel: %v", err)
resolvedBazelVersion, isCommit, err = resolveVersionLabel(bazeliskHome, bazelFork, bazelVersion)
if err != nil {
log.Fatalf("could not resolve the version '%s' to an actual version number: %v", bazelVersion, err)
}

bazelDirectory := filepath.Join(bazeliskHome, "bin", bazelFork)
err = os.MkdirAll(bazelDirectory, 0755)
if err != nil {
log.Fatalf("could not create directory %s: %v", bazelDirectory, err)
}

bazelPath, err = downloadBazel(bazelFork, resolvedBazelVersion, isCommit, bazelDirectory)
if err != nil {
log.Fatalf("could not download Bazel: %v", err)
}
}

args := os.Args[1:]
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/bazelbuild/bazelisk

require github.com/hashicorp/go-version v1.1.0
require (
github.com/hashicorp/go-version v1.1.0
github.com/mitchellh/go-homedir v1.1.0
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down

0 comments on commit fd66bc3

Please sign in to comment.