diff --git a/README.md b/README.md index 4f96ab7c..1fd8be9a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,13 @@ show you which flags can safely enabled, and which flags require a migration. You can set `BAZELISK_GITHUB_TOKEN` to set a GitHub access token to use for API requests to avoid rate limiting when on shared networks. +If `tools/bazel` exists in your workspace root and is executable, Bazelisk will run this file, +instead of the Bazel version it downloaded. It will set the environment variable `BAZEL_REAL` to +the path of the downloaded Bazel binary. This can be useful, if you have a wrapper script that e.g. +ensures that environment variables are set to known good values. This behavior can be disabled by +setting the environment variable `BAZELISK_SKIP_WRAPPER` to any value (except the empty string) +before launching Bazelisk. + ## Releases Binary and source releases are provided on our [Releases](https://github.com/bazelbuild/bazelisk/releases) page. diff --git a/bazelisk.go b/bazelisk.go index fe80e01a..6eb5caa9 100644 --- a/bazelisk.go +++ b/bazelisk.go @@ -38,8 +38,9 @@ import ( ) const ( - bazelReal = "BAZEL_REAL" - wrapperPath = "./tools/bazel" + bazelReal = "BAZEL_REAL" + skipWrapperEnv = "BAZELISK_SKIP_WRAPPER" + wrapperPath = "./tools/bazel" ) var ( @@ -421,26 +422,33 @@ func downloadBazel(version string, isCommit bool, directory string) (string, err return destinationPath, nil } -func delegateToWrapper(bazel string) string { +func maybeDelegateToWrapper(bazel string) string { + if os.Getenv(skipWrapperEnv) != "" { + return bazel + } + wd, err := os.Getwd() if err != nil { return bazel } + root := findWorkspaceRoot(wd) wrapper := filepath.Join(root, wrapperPath) if stat, err := os.Stat(wrapper); err != nil || stat.Mode().Perm()&0001 == 0 { return bazel } + return wrapper } func runBazel(bazel string, args []string) (int, error) { - execPath := delegateToWrapper(bazel) + execPath := maybeDelegateToWrapper(bazel) if execPath != bazel { os.Setenv(bazelReal, bazel) } cmd := exec.Command(execPath, args...) + cmd.Env = append(os.Environ(), skipWrapperEnv+"=true") cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/bazelisk_test.sh b/bazelisk_test.sh index a6c1e8de..00987120 100755 --- a/bazelisk_test.sh +++ b/bazelisk_test.sh @@ -61,6 +61,7 @@ function bazelisk() { else echo "Could not find the bazelisk executable, listing files:" find . + exit 1 fi } @@ -141,6 +142,49 @@ function test_bazel_last_rc() { (echo "FAIL: Expected to find 'Build label' in the output of 'bazelisk version'"; exit 1) } +function test_delegate_to_wrapper() { + setup + + mkdir tools + cat > tools/bazel <<'EOF' +#!/bin/sh +echo HELLO_WRAPPER +env | grep BAZELISK_SKIP_WRAPPER +EOF + chmod +x tools/bazel + + BAZELISK_HOME="$BAZELISK_HOME" \ + bazelisk version 2>&1 | tee log + + grep "HELLO_WRAPPER" log || \ + (echo "FAIL: Expected to find 'HELLO_WRAPPER' in the output of 'bazelisk version'"; exit 1) + + grep "BAZELISK_SKIP_WRAPPER=true" log || \ + (echo "FAIL: Expected to find 'BAZELISK_SKIP_WRAPPER=true' in the output of 'bazelisk version'"; exit 1) +} + +function test_skip_wrapper() { + setup + + mkdir tools + cat > tools/bazel <<'EOF' +#!/bin/sh +echo HELLO_WRAPPER +env | grep BAZELISK_SKIP_WRAPPER +EOF + chmod +x tools/bazel + + BAZELISK_HOME="$BAZELISK_HOME" \ + BAZELISK_SKIP_WRAPPER="true" \ + bazelisk version 2>&1 | tee log + + grep "HELLO_WRAPPER" log && \ + (echo "FAIL: Expected to not find 'HELLO_WRAPPER' in the output of 'bazelisk version'"; exit 1) + + grep "Build label:" log || \ + (echo "FAIL: Expected to find 'Build label' in the output of 'bazelisk version'"; exit 1) +} + echo "# test_bazel_version" test_bazel_version echo @@ -165,8 +209,23 @@ echo "# test_bazel_last_downstream_green" test_bazel_last_downstream_green echo -if [[$BAZELISK_VERSION == "GO"]]; then -echo "# test_bazel_last_rc" -test_bazel_last_rc -echo -fi \ No newline at end of file +if [[ $BAZELISK_VERSION == "GO" ]]; then + echo "# test_bazel_last_rc" + test_bazel_last_rc + echo + + case "$(uname -s)" in + MSYS*) + # The tests are currently not compatible with Windows. + ;; + *) + echo "# test_delegate_to_wrapper" + test_delegate_to_wrapper + echo + + echo "# test_skip_wrapper" + test_skip_wrapper + echo + ;; + esac +fi diff --git a/build.sh b/build.sh index a4b8f23c..30bae5f1 100755 --- a/build.sh +++ b/build.sh @@ -38,4 +38,5 @@ rm -f bazelisk # GOOS=windows GOARCH=amd64 go build -o bin/bazelisk-windows-amd64.exe ### Print some information about the generated binaries. +ls -lh bin/* file bin/*