dimo414 / bash-cache

Transparent caching layer for bash functions; particularly useful for functions invoked as part of your prompt.
GNU General Public License v3.0
73 stars 4 forks source link

:? expansions break caching #22

Open dimo414 opened 4 years ago

dimo414 commented 4 years ago
$ 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.