-
Notifications
You must be signed in to change notification settings - Fork 542
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
refactor: Add GAZELLE_VERBOSE env and log parser failures #2420
Conversation
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE") | ||
if envExists && verbose == "1" { |
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 people are happy with this idea, I will make a more generalized function that can be reused. Maybe even add verbosity levels.
I didn't see any option in the core gazelle
that allows for different verbosity levels, so if there is one please LMK.
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | ||
log.Printf("The above was parsed as: %v", child.String()) |
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.
Add comments demo'ing the logged message.
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.
Lol this was a TODO for me but now it's merged. I'll add this to the followup PR.
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 changed the title to refactor, since it isn't providing a feature, more just better diagnostics for debugging failures.
@@ -61,7 +61,8 @@ Unreleased changes template. | |||
|
|||
{#v0-0-0-added} | |||
### Added | |||
* Nothing yet. | |||
* (gazelle): Parser failures will now be logged to the terminal. Additional | |||
details can be logged by setting `GAZELLE_VERBOSE=1`. |
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.
You can add this to docs/environment-variables.md, too.
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.
TIL! ok I'll add that soon.
And change it to be prefixed with RULES_PYTHON_
to match the others.
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.
In general LGTM, but have questions if we should return err=nil
if root.HasError()
. If the Python file cannot be parsed, why should we continue?
if root.HasError() { | ||
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path) | ||
|
||
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE") | ||
if envExists && verbose == "1" { | ||
for i := 0; i < int(root.ChildCount()); i++ { | ||
child := root.Child(i) | ||
if child.IsError() { | ||
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | ||
log.Printf("The above was parsed as: %v", child.String()) | ||
} | ||
} | ||
} | ||
} |
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.
nit, it is much more idiomatic to do early returns:
if root.HasError() { | |
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path) | |
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE") | |
if envExists && verbose == "1" { | |
for i := 0; i < int(root.ChildCount()); i++ { | |
child := root.Child(i) | |
if child.IsError() { | |
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | |
log.Printf("The above was parsed as: %v", child.String()) | |
} | |
} | |
} | |
} | |
if !root.HasError() { | |
return root, nil | |
} | |
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path) | |
// TODO: if root.HasError, should we still return err=nil? | |
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE") | |
if !envExists || verbose != "1" { | |
return root, nil | |
} | |
for i := 0; i < int(root.ChildCount()); i++ { | |
child := root.Child(i) | |
if child.IsError() { | |
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | |
log.Printf("The above was parsed as: %v", child.String()) | |
} | |
} | |
return root, nil |
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! I'll update to that in a followup PR.
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 should return err=nil if root.HasError(). If the Python file cannot be parsed, why should we continue?
IMO we should continue on parse errors. It's possible that the parse error is unrelated to anything that gazelle cares about, and in that case the resulting target definition will be correct.
@@ -61,7 +61,8 @@ Unreleased changes template. | |||
|
|||
{#v0-0-0-added} | |||
### Added | |||
* Nothing yet. | |||
* (gazelle): Parser failures will now be logged to the terminal. Additional | |||
details can be logged by setting `GAZELLE_VERBOSE=1`. |
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.
TIL! ok I'll add that soon.
And change it to be prefixed with RULES_PYTHON_
to match the others.
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | ||
log.Printf("The above was parsed as: %v", child.String()) |
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.
Lol this was a TODO for me but now it's merged. I'll add this to the followup PR.
if root.HasError() { | ||
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path) | ||
|
||
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE") | ||
if envExists && verbose == "1" { | ||
for i := 0; i < int(root.ChildCount()); i++ { | ||
child := root.Child(i) | ||
if child.IsError() { | ||
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code)) | ||
log.Printf("The above was parsed as: %v", child.String()) | ||
} | ||
} | ||
} | ||
} |
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! I'll update to that in a followup PR.
While investigating #2396 and why #2413 doesn't appear to be working for us, I realized that one of the things I was making heavy use of was additional parser logging that I had added. This adds some of that logging. I also throw in some documentation because I found it helpful.
Users can (attempt to) get additional parse failure information by setting the
GAZELLE_VERBOSE
environment variable to1
. Eg:$ GAZELLE_VERBOSE=1 bazel run //:gazelle
Here are some example logs: