bash-cache icon indicating copy to clipboard operation
bash-cache copied to clipboard

:? expansions break caching

Open dimo414 opened this issue 5 years ago • 0 comments

$ foo() { echo 'Checking $1'; : "${1:?}"; }

$ foo
Checking $1
-bash: 1: parameter null or not set

$ bc::cache foo

$ foo

$ echo $?
1

$ (set -x; client_type) |& tail -n 3
++ mktemp -d /tmp/bash-cache-0.8-274474/data/60/XXXXXXXXXX
+ cache=/tmp/bash-cache-0.8-274474/data/60/vZDnRVXVA1
+ bc::orig::client_type

The failing ${1:?} expansion causes the shell to exit (in an interactive session it doesn't exit, but it does immediately terminate processing and return to the prompt, as if exiting). This prevents the function from being cached properly and, notably, suppresses the error message from being written to stderr.

Invoking the cached function in a subshell (i.e. ( "bc::orig::${func}" "${args[@]}" ) > "${cache}/out" 2> "${cache}/err" in bc::_write_cache) is sufficient to work-around this problem, but adding an extra subshell to every cache write would be an expensive solution.

dimo414 avatar Sep 20 '20 01:09 dimo414