Skip to content

Commit f635a19

Browse files
committed
fix: Stacktrace prints full file path. Closes #5
As opposed to relative paths
1 parent 566a21e commit f635a19

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/src/public/bash-core.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,32 @@ core.err_exists() {
274274
core.stacktrace_print() {
275275
printf '%s\n' 'Stacktrace:'
276276

277+
local old_cd="$PWD" cd_failed='no'
277278
local i=
278279
for ((i=0; i<${#FUNCNAME[@]}-1; ++i)); do
279-
printf '%s\n' " in ${FUNCNAME[$i]} (${BASH_SOURCE[$i]}:${BASH_LINENO[$i-1]})"
280+
local file="${BASH_SOURCE[$i]}"
281+
282+
# If the 'cd' has previous failed, then do not attempt to 'cd' as the current
283+
# directory is not in '$old_cd' (so the 'cd' will almost certainly fail)
284+
if [ "$cd_failed" = 'no' ]; then
285+
# shellcheck disable=SC1007
286+
if CDPATH= cd -- "${file%/*}"; then
287+
file="$PWD/${file##*/}"
288+
else
289+
cd_failed='yes'
290+
fi
291+
fi
292+
293+
printf '%s\n' " in ${FUNCNAME[$i]} ($file:${BASH_LINENO[$i-1]})"
294+
295+
if ! CDPATH= cd -- "$old_cd"; then
296+
cd_failed='yes'
297+
fi
280298
done; unset -v i
299+
300+
if [ "$cd_failed" = 'yes' ]; then
301+
printf '%s\n' "Error: bash-error: A 'cd' failed, so the stacktrace may include relative paths"
302+
fi
281303
} >&2
282304

283305
# @description Determine if color should be printed

0 commit comments

Comments
 (0)