@@ -52,9 +52,61 @@ debug "running $BAZEL_TOOL_TRAMPOLINE_TARGET"
5252debug " bazel invoke cwd: $( pwd) "
5353debug " target cwd : $BAZEL_TOOL_TRAMPOLINE_CWD "
5454
55+ # If BAZEL_TRAMPOLINE_PARALLEL=1 use a separate output_base so this tool can run
56+ # in parallel with the main bazel server without contending for the same lock.
57+ # Best for tools that need to run frequently and don't need a ton of files in
58+ # their output base. disk_cache will in general obviate the need to actually
59+ # rebuild anything.
60+ #
61+ # Determining the default output_base is tricky: `bazel info output_base` itself
62+ # takes the main server's lock, so if the main server is busy the query would
63+ # block (defeating the point). Strategy (keyed on cache existence):
64+ # - No cache yet: no choice but to block — call `bazel info output_base` and
65+ # seed the cache with the result.
66+ # - Cache exists: try `bazel --noblock_for_lock info output_base` to
67+ # opportunistically refresh the cache; on lock-held (exit 9) just use the
68+ # cached value.
69+ if [[ ${BAZEL_TRAMPOLINE_PARALLEL:- 0} -gt 0 ]]; then
70+ cache_dir=" $support_script_dir /../.cache"
71+ cache_file=" $cache_dir /output_base"
72+ mkdir -p " $cache_dir "
73+
74+ if [[ ! -f $cache_file ]]; then
75+ debug " output_base from: blocking bazel query (no cache yet)"
76+ default_output_base=" $( bazel info output_base) "
77+ echo " $default_output_base " > " $cache_file "
78+ else
79+ # Bazel exits 9 (LOCK_HELD_NOBLOCK_FOR_LOCK) when --noblock_for_lock
80+ # cannot acquire the server lock. Any other non-zero exit is a real
81+ # error and should surface rather than silently falling back to cache.
82+ rc=0
83+ fresh=$( bazel --noblock_for_lock info output_base 2> /dev/null) || rc=$?
84+ if (( rc == 0 )) ; then
85+ debug " output_base from: live bazel query (cache refreshed)"
86+ default_output_base=$fresh
87+ echo " $default_output_base " > " $cache_file "
88+ elif (( rc == 9 )) ; then
89+ default_output_base=" $( cat " $cache_file " ) "
90+ debug " output_base from: cache ($cache_file , lock held)"
91+ else
92+ echo " ERROR: bazel-tool-trampoline.sh: bazel info output_base failed (exit $rc )" >&2
93+ exit " $rc "
94+ fi
95+ fi
96+
97+ tool_output_base=" ${default_output_base} _rptool"
98+ output_base_flag=(--output_base=" $tool_output_base " )
99+
100+ debug " tool output_base : $tool_output_base "
101+ else
102+ output_base_flag=()
103+ debug " tool output_base : (default)"
104+ fi
105+
55106# These flags are to hide a bunch of Bazel's built-in output.
56107
57- exec bazel run " $BAZEL_TOOL_TRAMPOLINE_TARGET " \
108+ exec bazel " ${output_base_flag[@]} " \
109+ run " $BAZEL_TOOL_TRAMPOLINE_TARGET " \
58110 --run_under=//tools/bazel-tools/support:run \
59111 --noshow_progress \
60112 --ui_event_filters=,+error,+fail \
0 commit comments