diff --git a/.bazelrc b/.bazelrc index a37a042fdf7..e7efd3abdf5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -31,6 +31,10 @@ build --copt=-isystem --copt=bazel-out/k8-fastbuild-cpu-only/bin # rules_cuda configuration build:cpu-only --@rules_cuda//cuda:enable_cuda=False +# Definition of --config=shell +# interactive shell immediately before execution +build:shell --run_under="//tools/bazel_tools:shellwrap" + # Disable all warnings for external repositories. We don't care about # their warnings. build --per_file_copt=^external/@-w diff --git a/tools/bazel_tools/BUILD.bazel b/tools/bazel_tools/BUILD.bazel new file mode 100644 index 00000000000..f4c37fb7389 --- /dev/null +++ b/tools/bazel_tools/BUILD.bazel @@ -0,0 +1,5 @@ +sh_binary( + name = "shellwrap", + srcs = ["shellwrap.sh"], + visibility = ["//visibility:public"], +) diff --git a/tools/bazel_tools/shellwrap.sh b/tools/bazel_tools/shellwrap.sh new file mode 100755 index 00000000000..1ebab29a6a7 --- /dev/null +++ b/tools/bazel_tools/shellwrap.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# This script is helpful in entering an interactive shell from a bazel build +# before running a given bazel executable. +# This can provide a quick way to explore the sandbox directory and filesystem. +# Typical use is with +# +# bazel run --run_under=//tools/bazel:shell_wrapper //:target +# OR +# bazel run --config=shell //:target + +shell='/bin/bash' +rcfile='/tmp/pytorch_bazel_tools_shellwrap' +while [[ $# -gt 0 ]] ; do + case "$1" in + --shell_bin_path) + # path for the shell executable + shell="$2" + shift 2 + ;; + --rcfile) + # path for the file used to write the environment + rcfile="$2" + shift 2 + ;; + *) + # remaining arguments are part of the command for execution + break + ;; + esac +done + +if ! tty -s; then + echo 'A tty is not available.' + echo "Use \`bazel run\`, not \`bazel test\`." + exit 1 +fi + +NOCOLOR='\033[0m' +YELLOW='\033[1;33m' + +# store the environment in a file +export PYTORCH_SHELL_COMMAND=$* +echo "alias run=\"$*\"" > "$rcfile" +echo "PS1='\s-\v\$ '" >> "$rcfile" + +echo ===== +# print the execution command (command is yellow) +echo -e "alias run=${YELLOW}$PYTORCH_SHELL_COMMAND${NOCOLOR}" +echo ===== + +echo "Entering interactive shell at the execution root:" + +# quote escape all the arguments to use as a single input string +cmd="'$shell' --noprofile --rcfile '$rcfile'" + +# run the command in a script psuedo terminal and dump to null +/usr/bin/script -c "$cmd" -q /dev/null