Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set BAZELISK var to bazelisk executable path #612

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ If for any reason none of this works, you can also override the URL format altog
- `%%`: Literal `%` for escaping purposes.
- All other characters after `%` are reserved for future use and result in a processing error.

## Environment variables set by Bazelisk

Bazelisk prepends a directory to `PATH` that contains the downloaded Bazel binary.
This ensures that Bazel targets that invoke `bazel` will use the same Bazel binary as the outer invocation.

Bazelisk also sets the environment variable `BAZELISK` to its own path.
This can be useful for scripts that want to know if they are running under Bazelisk and can also be used to run specific Bazel versions from within a Bazel run, e.g. to generate version-specific test data.

## Ensuring that your developers use Bazelisk rather than Bazel

Bazel installers typically provide Bazel's [shell wrapper script] as the `bazel` on the PATH.
Expand Down
11 changes: 8 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
const (
bazelReal = "BAZEL_REAL"
skipWrapperEnv = "BAZELISK_SKIP_WRAPPER"
bazeliskEnv = "BAZELISK"
defaultWrapperDirectory = "./tools"
defaultWrapperName = "bazel"
maxDirLength = 255
Expand Down Expand Up @@ -214,11 +215,11 @@ func getBazelCommand(args []string) (string, error) {

// getBazeliskHome returns the path to the Bazelisk home directory.
func getBazeliskHome(config config.Config) (string, error) {
bazeliskHome := config.Get("BAZELISK_HOME_" + strings.ToUpper(runtime.GOOS))
bazeliskHome := config.Get("BAZELISK_HOME_" + strings.ToUpper(runtime.GOOS))
if len(bazeliskHome) == 0 {
bazeliskHome = config.Get("BAZELISK_HOME")
}

if len(bazeliskHome) == 0 {
userCacheDir, err := os.UserCacheDir()
if err != nil {
Expand Down Expand Up @@ -581,6 +582,10 @@ func makeBazelCmd(bazel string, args []string, out io.Writer, config config.Conf
if execPath != bazel {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", bazelReal, bazel))
}
selfPath, err := os.Executable()
if err != nil {
cmd.Env = append(cmd.Env, bazeliskEnv+"="+selfPath)
}
prependDirToPathList(cmd, filepath.Dir(execPath))
cmd.Stdin = os.Stdin
if out == nil {
Expand Down Expand Up @@ -798,7 +803,7 @@ func getBazelCommitsBetween(goodCommit string, badCommit string, config config.C
if err != nil {
return goodCommit, nil, fmt.Errorf("Error unmarshaling JSON: %v", err)
}

if len(compResp.Commits) == 0 {
break
}
Expand Down