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

Capture backtraces when codegen goes awry #128

Merged
merged 1 commit into from
Nov 7, 2022
Merged

Capture backtraces when codegen goes awry #128

merged 1 commit into from
Nov 7, 2022

Conversation

rsheeter
Copy link
Collaborator

@rsheeter rsheeter commented Nov 7, 2022

Feel free to merge if acceptable. Helps with #82, though spots where we propagate an error with ? still get missed. Baby steps.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to merge this as an improvement, but I think longer term we might want to move away from syn::Error, and create our own error type that generates a backtrace when it is created; this would have an impl of From<syn::Error>, and in that impl we would grab the backtrace, which would then work anywhere there is ?.

Comment on lines +35 to +39
// This is the one step where we can't readily intercept the error with logged_syn_error
let mut items: Items = syn::parse_str(code_str).map_err(|e| {
debug!("{}", Backtrace::capture());
e
})?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is only going to capture a backtrace at this point, which might not be what we want? In particular it won't tell us anything about what where in our parse impls we failed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we fail parsing entirely, e.g. I go add DUCK to some_input.rs, then we hit this w/o hitting any of our parse stuff. if I deliberately break something, say change u16 to u16z somewhere, we get a trace to the right place in our code.

}
}

pub(crate) fn logged_syn_error<T: Display>(span: Span, message: T) -> syn::Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is an improvement, but as we're only doing this with errors that we create explicitly (as opposed to errors that occur during another parse method, for instance when we do let name = input.parse::<syn::Ident>()?).

And unfortunately, errors of the other type (generated in syn, and merely propagated by us) are probably the majority of our errors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried introducing some basic errors, e.g. a type that didn't exist, I did get a trace. In any case, IIUC using our own error that auto-captures traces should entirely resolve this.

font-codegen/src/lib.rs Show resolved Hide resolved
@rsheeter
Copy link
Collaborator Author

rsheeter commented Nov 7, 2022

create our own error type that generates a backtrace when it is created

That does sound much nicer.

EDIT: I tried this and it seemed to work out worse because in impl Parse for Something I have to return a Result<T, syn::Error>. If I create that error in a way that makes a backtrace I get the expected trace.

This does leave us with the problem that we miss backtracing places where we use ?, I'm not sure wha tthe best way to tackle that is. I think I'll merge as-is for now, but without closing the capture backtrace issue, as what's here does make life easier despite being imperfect.

@rsheeter rsheeter merged commit 1a86822 into main Nov 7, 2022
@rsheeter rsheeter deleted the dbg branch November 7, 2022 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants