Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trivial debug tracing information #36

Merged
merged 2 commits into from
Sep 26, 2024
Merged

feat: trivial debug tracing information #36

merged 2 commits into from
Sep 26, 2024

Conversation

nrdxp
Copy link
Contributor

@nrdxp nrdxp commented Aug 28, 2024

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 to addErrorContext 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 when debug is set to true, 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:

❯ nix-shell
trace: in dev-0.1.0
trace: in dev-0.1.0 at foo
trace: in dev-0.1.0 at foo.bar
trace: in dev-0.1.0 at foo.bar.shell
error:
<nix backtrace>

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.

@nrdxp nrdxp linked an issue Aug 28, 2024 that may be closed by this pull request
@nrdxp nrdxp added the tracing logging, tracing, or error reporting label Aug 28, 2024
@nrdxp nrdxp linked an issue Sep 3, 2024 that may be closed by this pull request
@nrdxp nrdxp mentioned this pull request Sep 3, 2024
One of the best things about having a predictable module structure is
that we can trivially construct error 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.
Instead of having to keep track of a debug boolean, just use nix's
builtin functionality to only show builtins.traceVerbose calls when
--trace-verbose is passed on the nix cli.
@nrdxp
Copy link
Contributor Author

nrdxp commented Sep 26, 2024

Gonna go ahead and merge this after switching to trace-verbose, just so we have it in master, we can interate on it in follow up PRs

@nrdxp nrdxp merged commit f2f7314 into master Sep 26, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracing logging, tracing, or error reporting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Robust Error Context & Reporting Report error location in tree
1 participant