@@ -35,6 +35,7 @@ import (
3535 "time"
3636
3737 version "github.com/hashicorp/go-version"
38+ homedir "github.com/mitchellh/go-homedir"
3839)
3940
4041const (
@@ -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 :]
0 commit comments