bun icon indicating copy to clipboard operation
bun copied to clipboard

Shell execution continues after failed command

Open wollowizard opened this issue 9 months ago • 1 comments

What version of Bun is running?

1.1.8+89d25807f

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

in Bun shell, I believe there's an issue with some commands, where even if they fail, they allow execution of subsequent instructions. Example:

import {$} from "bun";
try {
    await $`cd non-existent-directory && echo " This should not be printed?"`
}catch (e) {
    console.log("error");
}

the output is: cd: not a directory: non-existent-directory This should not be printed?

However this does not happen with all commands, for example

import {$} from "bun";
try {
    await $`cat non-existent-directory && echo " This should not be printed?"`
}catch (e) {
    console.log("error");
}

prints:

cat: non-existent-directory: No such file or directory
error

Finally, if the instructions are on 2 lines, then the previous return code is always ignored (which is also the case in bash):

import {$} from "bun";
try {
    await $`cat non-existent-directory 
    echo " This should not be printed?"`
}catch (e) {
    console.log("error");
}

but it would be nice to have a behavior similar to set -euo pipefail (maybe by default or at least opt-in).

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

wollowizard avatar May 15 '24 07:05 wollowizard

This looks like a bug where cd is returning the wrong exit code, otherwise && symbol works:

bun -e 'await Bun.$`ls lskdjfksjdflksdf && echo cant see this`.throws(false)'

>> ls: lskdjfksjdflksdf: No such file or directory

As for set -euo pipefail we should definitely add this

zackradisic avatar May 15 '24 10:05 zackradisic