Skip to content

Commit

Permalink
Fix #61: Do not execute tools/bazel when BAZELISK_SKIP_WRAPPER is set. (
Browse files Browse the repository at this point in the history
#67)

* Fix #61: Do not execute tools/bazel when BAZELISK_SKIP_WRAPPER is set.

Bazelisk will set this environment variable when executing Bazel (or
a wrapper found in <workspace_root>/tools/bazel). This prevents Bazelisk
from executing itself in an infinite loop, in case the wrapper is
Bazelisk or somehow executes Bazelisk.

If you're using a wrapper in <workspace_root>/tools/bazel that downloads
Bazelisk and delegates to it, please set the BAZELISK_SKIP_WRAPPER
environment variable to ensure that Bazelisk directly executes Bazel.

* Skip wrapper delegation tests on Windows
  • Loading branch information
philwo authored Jun 4, 2019
1 parent e105567 commit c2912d8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions bazelisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ import (
)

const (
bazelReal = "BAZEL_REAL"
wrapperPath = "./tools/bazel"
bazelReal = "BAZEL_REAL"
skipWrapperEnv = "BAZELISK_SKIP_WRAPPER"
wrapperPath = "./tools/bazel"
)

var (
Expand Down Expand Up @@ -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
Expand Down
69 changes: 64 additions & 5 deletions bazelisk_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function bazelisk() {
else
echo "Could not find the bazelisk executable, listing files:"
find .
exit 1
fi
}

Expand Down Expand Up @@ -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
Expand All @@ -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
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
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/*

0 comments on commit c2912d8

Please sign in to comment.