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

refactor: Add GAZELLE_VERBOSE env and log parser failures #2420

Merged
merged 5 commits into from
Nov 18, 2024

Conversation

dougthor42
Copy link
Contributor

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 to 1. Eg:

$ GAZELLE_VERBOSE=1 bazel run //:gazelle

Here are some example logs:

$ GAZELLE_VERBOSE=1 bazel run //:gazelle
INFO: Invocation ID: a4e026d8-17df-426c-b1cc-d3980690dd53
...
INFO: Running command line: bazel-bin/gazelle
INFO: Streaming build results to: https://btx.cloud.google.com/invocations/a4e026d8-17df-426c-b1cc-d3980690dd53
gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect.
gazelle: Parse error at {Row:1 Column:0}:
def search_one_more_level[T]():
gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention.
$
$ bazel run //:gazelle
INFO: Invocation ID: 726c9fd6-f566-4c30-95ef-c4781ad155de
...
INFO: Running command line: bazel-bin/gazelle
INFO: Streaming build results to: https://btx.cloud.google.com/invocations/726c9fd6-f566-4c30-95ef-c4781ad155de
gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect.
gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention.

Comment on lines +75 to +76
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE")
if envExists && verbose == "1" {
Copy link
Contributor Author

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.

Comment on lines +80 to +81
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
log.Printf("The above was parsed as: %v", child.String())
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@dougthor42 dougthor42 marked this pull request as ready for review November 17, 2024 05:24
@rickeylev rickeylev changed the title feat(gazelle): Add GAZELLE_VERBOSE env and log parser failures refactor: Add GAZELLE_VERBOSE env and log parser failures Nov 18, 2024
Copy link
Collaborator

@rickeylev rickeylev left a 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.

gazelle/python/file_parser.go Outdated Show resolved Hide resolved
@@ -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`.
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

@aignas aignas left a 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?

Comment on lines +72 to +85
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())
}
}
}
}
Copy link
Collaborator

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:

Suggested change
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

Copy link
Contributor Author

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.

@rickeylev rickeylev added this pull request to the merge queue Nov 18, 2024
Merged via the queue into bazelbuild:main with commit b28db69 Nov 18, 2024
4 checks passed
Copy link
Contributor Author

@dougthor42 dougthor42 left a 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`.
Copy link
Contributor Author

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.

Comment on lines +80 to +81
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
log.Printf("The above was parsed as: %v", child.String())
Copy link
Contributor Author

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.

Comment on lines +72 to +85
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())
}
}
}
}
Copy link
Contributor Author

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.

@dougthor42 dougthor42 deleted the u/dthor/parser-logging branch November 19, 2024 18:05
github-merge-queue bot pushed a commit that referenced this pull request Nov 20, 2024
… comments (#2428)

Address PR comments from #2420:

+ Add and update comments
+ idiomatic go: return early
+ rename and document env var
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.

3 participants