-
Notifications
You must be signed in to change notification settings - Fork 9
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
Report location of code causing KSP exceptions when generating assisted factories #73
Report location of code causing KSP exceptions when generating assisted factories #73
Conversation
…assisted factories with a parameter of unknown type. This will wrap the original KotlinPoet exception adding some context about the name and location of the parameter that caused the code generation issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contextualTypeName is supposed to cover this case. Can we just improve that function to allow adding extra context if needed? Without a test case it's a little difficult to imagine the case this is solving
I'm trying to come up with some test for it, but it's difficult because I can't find a way to get access to the expected exception with the location values,
EDIT: ok, it seems like this happens because it was being run with a configuration that never called the code I modified. |
compiler/src/main/java/com/squareup/anvil/compiler/codegen/ksp/KspUtil.kt
Outdated
Show resolved
Hide resolved
compiler/src/main/java/com/squareup/anvil/compiler/codegen/ksp/KspUtil.kt
Outdated
Show resolved
Hide resolved
compiler/src/main/java/com/squareup/anvil/compiler/codegen/ksp/KspUtil.kt
Outdated
Show resolved
Hide resolved
- Resolve argument types only once. - Add extra info for property and function declarations too.
Please stop re-requesting review, I am reviewing. I am also on an airplane and will get to it when I get to it :). |
Ah sorry, the flow in my job is to make any needed changes and then requesting a new review to notify the reviewer it's ready, so I just followed it out of habit 😅 . It doesn't mean I want an immediate request, I just wanted to flag this was ready for you to look at it again when you get the time, my bad. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the warning? toString() calls on KSP declarations aren’t always super reliable so I’d rather use something more intentional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was:
Conditional branch result of type KSNode is implicitly cast to Any?
Conditional branch result of type KSType? is implicitly cast to Any?
The first branch of the if returned a KSNode
and the 2nd one a KSType?
. I had assumed the KSType
would also implement KSNode
, but it turns out it's a completely different type and it's not related. So I guess the options are:
- Since we only want to print its info, use the
toString()
strategy, as done in the changes. - Explicitly cast to
Any?
. - Change the implementation to something else altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a crack at it in 6828daa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! It looks way cleaner.
When having a code like:
If
Foo
hasn't been properly imported (i.e. in the middle of a refactor), KotlinPoet code generation will fail with a not really helpful error like:In the latest version, this has improved and it now displays at least:
But it still won't tell you where the code generation is failing, so you'd have to check every single usage of this class to try to understand where the error is.
I added some code that, when trying to get the underlying type for KotlinPoet fails, will wrap the original exception adding some context about the name and location of the parameter that caused the code generation issue, like this:
I'm not really sure about how this was handled, so feel free to suggest or replace the implementation for something better, or add it in other places where this issue may happen: I only know of this one, but there could be more.