Skip to content

Commit 972a209

Browse files
vorspytorchmergebot
authored andcommitted
[bazel] Add --config=shell for easier debugging (pytorch#79350)
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
1 parent 02093da commit 972a209

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ build --copt=-isystem --copt=bazel-out/k8-fastbuild-cpu-only/bin
3131
# rules_cuda configuration
3232
build:cpu-only --@rules_cuda//cuda:enable_cuda=False
3333

34+
# Definition of --config=shell
35+
# interactive shell immediately before execution
36+
build:shell --run_under="//tools/bazel_tools:shellwrap"
37+
3438
# Disable all warnings for external repositories. We don't care about
3539
# their warnings.
3640
build --per_file_copt=^external/@-w

tools/bazel_tools/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sh_binary(
2+
name = "shellwrap",
3+
srcs = ["shellwrap.sh"],
4+
visibility = ["//visibility:public"],
5+
)

tools/bazel_tools/shellwrap.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
# This script is helpful in entering an interactive shell from a bazel build
4+
# before running a given bazel executable.
5+
# This can provide a quick way to explore the sandbox directory and filesystem.
6+
# Typical use is with
7+
#
8+
# bazel run --run_under=//tools/bazel:shell_wrapper //:target
9+
# OR
10+
# bazel run --config=shell //:target
11+
12+
shell='/bin/bash'
13+
rcfile='/tmp/pytorch_bazel_tools_shellwrap'
14+
while [[ $# -gt 0 ]] ; do
15+
case "$1" in
16+
--shell_bin_path)
17+
# path for the shell executable
18+
shell="$2"
19+
shift 2
20+
;;
21+
--rcfile)
22+
# path for the file used to write the environment
23+
rcfile="$2"
24+
shift 2
25+
;;
26+
*)
27+
# remaining arguments are part of the command for execution
28+
break
29+
;;
30+
esac
31+
done
32+
33+
if ! tty -s; then
34+
echo 'A tty is not available.'
35+
echo "Use \`bazel run\`, not \`bazel test\`."
36+
exit 1
37+
fi
38+
39+
NOCOLOR='\033[0m'
40+
YELLOW='\033[1;33m'
41+
42+
# store the environment in a file
43+
export PYTORCH_SHELL_COMMAND=$*
44+
echo "alias run=\"$*\"" > "$rcfile"
45+
echo "PS1='\s-\v\$ '" >> "$rcfile"
46+
47+
echo =====
48+
# print the execution command (command is yellow)
49+
echo -e "alias run=${YELLOW}$PYTORCH_SHELL_COMMAND${NOCOLOR}"
50+
echo =====
51+
52+
echo "Entering interactive shell at the execution root:"
53+
54+
# quote escape all the arguments to use as a single input string
55+
cmd="'$shell' --noprofile --rcfile '$rcfile'"
56+
57+
# run the command in a script psuedo terminal and dump to null
58+
/usr/bin/script -c "$cmd" -q /dev/null

0 commit comments

Comments
 (0)