Replies: 2 comments 4 replies
-
I've tried to do something like this in the past, and got mixed results. It may have just been me writing a buggy implementation, or it may be that Bash just makes these things really hard. I personally would be open to prototyping a library function that implements stack trace functionality and trying it out in a few of my projects, and see how it goes. In any case, as you mention, this can't be abstracted away from the programmer so that they avoid needing to think about the details. For this reason I'm not entirely sure this belongs in the Bashly project itself. A stand-alone third-party library makes sense to me. That would force developers to actually look at some documentation and understand things first before they use it. |
Beta Was this translation helpful? Give feedback.
-
I like it.
As for the difference in output format - I did not see a huge difference, and for me simpler is almost always better. Since you two seem to have some experience with this bash feature, I am open to try any implementation that you think can work, and can be added as a library. If the only obstacle in our way is the ability for a library to "inject" the |
Beta Was this translation helpful? Give feedback.
-
Idea
One more step towards making bash programming more in line with "how progamming is expected to look like"...
When an error occurs in a bash program, little information is displayed to aid debugging. What most bash programmers usually do as the first debugging investigation is to run the program with
set -x
, which is too verbose.One trick I learned recently is to use the
caller
builtin to get useful information about the call stack. And then create a trap to give me a stack trace.You can check
help caller
for the official description. I found this explanation particularly useful.In order to use
caller
to generate an useful stacktrace, some "hacking" is needed. Here I share an implementation:Here's the output:
Notes
set -E
This is needed so
trap ... ERR
can be inherited by the functions.caller
outputThe default output is
$line $func $file
, but in other programming languages we usually see something starting with$file:$line ...
.If we don't tweak the output, the
stacktrace
function would be much simpler like this:In our example it would generate this output:
Which is kinda confusing IMO.
To be fair, the implementation I've posted is probably too verbose, as the bashly-generated code is always in the same file. I posted that just for illustration.
Considerations
Although displaying a stack trace is a basic thing in many interpreted languages, the way to have it in bash requires our own implementation, and for this implementation to be actually useful we need to enable flags (
set -Ee
) and set a trap, which can break existing code.The possibility to have it via library functions looks safer, as it requires the programmer to be aware of what he is doing (future maintainers of the codebase can be confused, though).
Beta Was this translation helpful? Give feedback.
All reactions