Propagate command-not-found errors when invoking wrapped commands

Before this change, if a package manager was not installed, safe-chain still
sets the function and when invoked, the wrapper will invoke safe-chain, which
will exit with error code 127 when it fails to invoke the wrapped command. As an
example (with a shell prompt that shows $? when non-zero):

```
$ type -f pip
bash: type: pip: not found
1$ pip
127$
```

With this patch, the wrapper first checks for the existence of the wrapped
command (ignoring functions), and if no such command exists, it instructs the
shell to invoke it anyway. This results in the shell failing to find the
command, and reporting an error as if the wrapper function wasn't there:

```
$ source init-posix.sh
$ type -f pip
bash: type: pip: not found
1$ pip
Command 'pip' not found, but can be installed with:
sudo apt install python3-pip
127$
```
This commit is contained in:
Uriel Corfa 2026-01-07 17:18:48 +01:00 committed by Sander Declerck
parent 20cc62d6e1
commit 607b4ee87d
No known key found for this signature in database

View file

@ -76,6 +76,14 @@ function printSafeChainWarning() {
function wrapSafeChainCommand() { function wrapSafeChainCommand() {
local original_cmd="$1" local original_cmd="$1"
if ! type -f "${original_cmd}" > /dev/null 2>&1; then
# If the original command is not available, don't try to wrap it: invoke it
# transparently, so the shell can report errors as if this wrapper didn't
# exist.
command $@
return $?
fi
if command -v safe-chain > /dev/null 2>&1; then if command -v safe-chain > /dev/null 2>&1; then
# If the aikido command is available, just run it with the provided arguments # If the aikido command is available, just run it with the provided arguments
safe-chain "$@" safe-chain "$@"