bun
bun copied to clipboard
Option for single-file executable not auto-closing on Windows
What is the problem this feature would solve?
Currently on Windows when you use bun build --compile index.ts
and double click the resulting exe
file a new terminal window will open and immediately close either when the program is done or when it throws an uncaught error. This can be quite problematic if the program throws an error since you wont be able to read it. As such it would be nice if there was an option to either at compile time or programmatically being able to configure the app to keep the terminal window open until the user confirms that it should terminate.
What is the feature you are proposing to solve the problem?
This would solve the application immediately closing when an uncaught error occurs, which would make it easier to debug since you could see the stack trace.
What alternatives have you considered?
For my own error handling I simply either use await setTimeout(() => {}, 10000);
or I use for await (const _line of console) { break; }
. But the problem with those solutions are that if something in the program throws an uncaught error then the solutions don't ever run and the terminal window closes immediately.
This problem is of course non-existent if you run the compiled exe
file from inside an already opened terminal, since then the terminal window won't close when the program is done. But one of the main benefits I found with the compiled executable is that I can easily create shell scripts that are instantly launchable from the explorer. Since the main benefit I see from the compiled executables is being able to run it from outside a terminal, running it from inside an open terminal isn't really a viable fix for my use-case.
I may have misunderstood your meaning, but as far as I know, binary files packaged in other languages also close after running. This is because your program is intended for terminal use rather than GUI. If you run it using PowerShell, you can see the logs after it finishes running because the output is recorded by PowerShell. However, when you double-click to run it directly, it doesn't "open a terminal and execute," but rather executes directly, which will automatically exit after the process ends.
You can easily solve this problem by adding:
$`pause`
At the end of your program.
Like this, you used the pause
command in Windows, which requires you to press any key for the program to terminate its execution.
As explained above I already have a solution for preventing it from closing when no uncaught errors occur, the issue is for uncaught errors it never reaches the line of code that tell it to "pause" at the end.
Also pause
did nothing for me, so my previous solutions are still my only partial solution.
Although on further experimenting seems I overlooked a rather obvious solution... Just wrapping everything inside a try ... catch
scope and using my existing solution.
Example:
import { $ } from "bun";
try {
console.log(`>> Downloading <<\n`);
await $`yt-dlp ...`;
console.log(`\n>> Done <<`);
console.log(`\n[press ENTER to exit]`);
// keeps terminal window open until user confirmation
for await (const _line of console) {
break;
}
} catch (err) {
console.log(err);
console.log(`\n[press ENTER to exit]`);
// keeps terminal window open until user confirmation
for await (const _line of console) {
break;
}
}
The above example is very minimal so it wouldn't be hard to just wrap the offending command in a try ... catch
scope or simply doing $.nothrow();
, but in my real code there are many other factors, but wrapping everything in a try ... catch
scope seems to work great, I think it would still be nice if there was an official option to keep the program open until user confirmation so that my solution wouldn't be required, but I can understand that this feature is quite small and can quite easily be addressed with the solution above.
So unless anymore discussions or suggestions around this feature take place I'm comfortable closing this issue.
you can run the executable in cmd.exe
you can run the executable in
cmd.exe
@dszymon did you miss the part where I already mentioned that I knew about that and that it wasn't a viable fix for my use-case?
From my issue above:
This problem is of course non-existent if you run the compiled
exe
file from inside an already opened terminal, since then the terminal window won't close when the program is done. But one of the main benefits I found with the compiled executable is that I can easily create shell scripts that are instantly launchable from the explorer. Since the main benefit I see from the compiled executables is being able to run it from outside a terminal, running it from inside an open terminal isn't really a viable fix for my use-case.
I'll go ahead a close this now, since there haven't really been any further discussions about it.