feat: trivial debug tracing information #36
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #28 #9
One of the best things about having a predictable module structure is that we can trivially construct location information. We can then use this to report unambiguously where we are in an Atom during evaluation.
This makes debugging trivial, since if you fail anywhere in your code the debug trace (when enabled) will print the last location visited inside the module system before the error occurred.
Implementation
It was first attempted to use
builtins.addErrorContext
to print location information only in an error trace. However, this function only reports the context if the failure occurs immediately in the given value. If the error occurs later in a nested structure, then the call toaddErrorContext
is essentially useless and error information is never reported. Even if that weren't the case, we still wouldn't have control of where the message appears, and if there was a ton of library code in use, it may be too far up the trace to even be helpful.So instead, in order to keep the implementation relatively simple while still being thorough, we simply use
builtins.trace
. We only trace where we are whendebug
is set totrue
, but when we do so, every module entry, and every module member entry is traced, with information about the atom you are in, version information (in case you have multiple versions of an atom), as well as the full path to the module or member.This means that if an error occurs anywhere in the module system, the last trace message will be the full path to the module or member it occurred in. Making tracking down error locations in your code trivial compared to existing methods.
To make the power of this clear, here is the cli trace of a contrived error:
given this trace, and understanding atom's structure, we can trivially derive that shell is a module member, and we immediately know exactly which file contains the error, regardless of whether the nix backtrace is helpful or not.
First Draft
This implementation serves as a draft to prove the value of the concept, but the format and details are up for ammendments.