From 19ee00242e37c09c04f4f19ba8de5a884427dd13 Mon Sep 17 00:00:00 2001 From: Alberto Cavalcante <54247214+albertocavalcante@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:00:11 -0300 Subject: [PATCH] `.bazeliskrc`: Add Support for Env Var and Tilde Expansion (#587) * core: removed unused vars 'fileConfig' and 'fileConfigOnce' * extract bazeliskHome definition to separate function and add support to handle env variables and tilde expansion in .bazeliskrc --- core/core.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/core/core.go b/core/core.go index 8aad6877..52618cb3 100644 --- a/core/core.go +++ b/core/core.go @@ -20,7 +20,6 @@ import ( "runtime" "sort" "strings" - "sync" "syscall" "github.com/bazelbuild/bazelisk/config" @@ -42,9 +41,6 @@ const ( var ( // BazeliskVersion is filled in via x_defs when building a release. BazeliskVersion = "development" - - fileConfig map[string]string - fileConfigOnce sync.Once ) // ArgsFunc is a function that receives a resolved Bazel version and returns the arguments to invoke @@ -97,17 +93,12 @@ func RunBazeliskWithArgsFuncAndConfig(argsFunc ArgsFunc, repos *Repositories, co func RunBazeliskWithArgsFuncAndConfigAndOut(argsFunc ArgsFunc, repos *Repositories, config config.Config, out io.Writer) (int, error) { httputil.UserAgent = getUserAgent(config) - bazeliskHome := config.Get("BAZELISK_HOME") - if len(bazeliskHome) == 0 { - userCacheDir, err := os.UserCacheDir() - if err != nil { - return -1, fmt.Errorf("could not get the user's cache directory: %v", err) - } - - bazeliskHome = filepath.Join(userCacheDir, "bazelisk") + bazeliskHome, err := getBazeliskHome(config) + if err != nil { + return -1, fmt.Errorf("could not determine Bazelisk home directory: %v", err) } - err := os.MkdirAll(bazeliskHome, 0755) + err = os.MkdirAll(bazeliskHome, 0755) if err != nil { return -1, fmt.Errorf("could not create directory %s: %v", bazeliskHome, err) } @@ -220,6 +211,32 @@ func getBazelCommand(args []string) (string, error) { return "", fmt.Errorf("could not find a valid Bazel command in %q. Please run `bazel help` if you need help on how to use Bazel", strings.Join(args, " ")) } +// getBazeliskHome returns the path to the Bazelisk home directory. +func getBazeliskHome(config config.Config) (string, error) { + bazeliskHome := config.Get("BAZELISK_HOME") + if len(bazeliskHome) == 0 { + userCacheDir, err := os.UserCacheDir() + if err != nil { + return "", fmt.Errorf("could not get the user's cache directory: %v", err) + } + + bazeliskHome = filepath.Join(userCacheDir, "bazelisk") + } else { + // If a custom BAZELISK_HOME is set, handle tilde and var expansion + // before creating the Bazelisk home directory. + var err error + + bazeliskHome, err = homedir.Expand(bazeliskHome) + if err != nil { + return "", fmt.Errorf("could not expand home directory in path: %v", err) + } + + bazeliskHome = os.ExpandEnv(bazeliskHome) + } + + return bazeliskHome, nil +} + func getUserAgent(config config.Config) string { agent := config.Get("BAZELISK_USER_AGENT") if len(agent) > 0 {