-
Notifications
You must be signed in to change notification settings - Fork 21
feat(conflictingmarkers): Make error messages more precise #156
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(conflictingmarkers): Make error messages more precise #156
Conversation
Updates the `conflictingmarkers` analyzer to include the struct name in the error message, changing the output from: "field <fieldName> has conflicting markers..." to: "field <structName>.<fieldName> has conflicting markers..." This provides more precise context, making it easier for developers to locate the source of the error, especially in complex types. The struct name is reliably determined by inspecting the AST stack during analysis.
d060b21 to
c117432
Compare
|
/assign @JoelSpeed |
| // Find the struct name from the stack | ||
| for i := len(stack) - 2; i >= 0; i-- { | ||
| if typeSpec, ok := stack[i].(*ast.TypeSpec); ok { | ||
| if _, ok := typeSpec.Type.(*ast.StructType); ok { | ||
| structName = typeSpec.Name.Name | ||
| break | ||
| } | ||
| } | ||
| } |
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.
If we find that this is a valuable addition to the messages output by the linting checks, this should probably be a sweeping change across all the linting checks.
What do you think about turning this into a generic utility that can be used by existing and new linting checks that should be providing this additional granularity?
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.
Agreed, this seems like something we should agree to change globally (e.g. we already have something similar in conditions, but different) and maybe even make a standard utility that looks up the name from the pass rather than passing it all through
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.
I added a generic util func using pass and field to get the structname.
| } | ||
|
|
||
| func checkConflict(pass *analysis.Pass, field *ast.Field, markers markers.MarkerSet, conflictSet ConflictSet) { | ||
| func checkConflict(pass *analysis.Pass, field *ast.Field, structName string, markers markers.MarkerSet, conflictSet ConflictSet) { |
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.
Will this handle conflicting tags when applied to a tyoe definition (e.g. a string alias)?
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.
Yes it will.
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.
This seems like an improvement to me.
Adds a new utility function, `GetStructNameForField`, to determine the parent struct name for a given field by inspecting the analysis pass. Refactors the `conflictingmarkers` analyzer to use this new utility, simplifying the analyzer and improving the detail in its reports.
Adds a new test case for the `GetStructNameForField` utility function. This test uses the `analysistest` package to run a dedicated analyzer over a test data file, ensuring the function correctly identifies the parent struct for a given field.
b8463cf to
b09f061
Compare
|
/approve Thanks for the update @yongruilin, I'll create an issue so that we follow up and make this consistent among all the analyzers in the future |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: JoelSpeed, yongruilin The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Updates the
conflictingmarkersanalyzer to include the struct name in the error message, changing the output from:"field has conflicting markers..."
to:
"field . has conflicting markers..."
This provides more precise context, making it easier for developers to locate the source of the error, especially in complex types. The struct name is reliably determined by inspecting the AST stack during analysis.
This is for addressing the exception confusion: kubernetes/kubernetes#134229 (comment)