Skip to content

Commit

Permalink
[bazel] Add --config=shell for easier debugging (pytorch#79350)
Browse files Browse the repository at this point in the history
Bazel quality of life improvement.

This change adds a new option `--config=shell` which allows you to drop-in the shell right before the `bazel run` command is executed. For example you will have ability to examine bazel sandbox this way, run things under gdb instead of a normal run and so on.

Example usage:

```
bazel run --config=shell //:init_test
```
Pull Request resolved: pytorch#79350
Approved by: https://github.com/dagitses
  • Loading branch information
vors authored and pytorchmergebot committed Jun 25, 2022
1 parent 02093da commit 972a209
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tools/bazel_tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sh_binary(
name = "shellwrap",
srcs = ["shellwrap.sh"],
visibility = ["//visibility:public"],
)
58 changes: 58 additions & 0 deletions tools/bazel_tools/shellwrap.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 972a209

Please sign in to comment.