chore(gazelle): Rename GAZELLE_VERBOSE env var; use idiomatic go; add comments (#2428)
Address PR comments from #2420:
+ Add and update comments
+ idiomatic go: return early
+ rename and document env var
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1a1c17d..9cd6b82 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -87,7 +87,7 @@
{#v0-0-0-added}
### Added
* (gazelle): Parser failures will now be logged to the terminal. Additional
- details can be logged by setting `GAZELLE_VERBOSE=1`.
+ details can be logged by setting `RULES_PYTHON_GAZELLE_VERBOSE=1`.
* (toolchains) allow users to select which variant of the support host toolchain
they would like to use through
`RULES_PYTHON_REPO_TOOLCHAIN_{VERSION}_{OS}_{ARCH}` env variable setting. For
diff --git a/docs/environment-variables.md b/docs/environment-variables.md
index 906281d..12c1bcf 100644
--- a/docs/environment-variables.md
+++ b/docs/environment-variables.md
@@ -56,3 +56,9 @@
When `1`, debug information about coverage behavior is printed to stderr.
:::
+
+
+:::{envvar} RULES_PYTHON_GAZELLE_VERBOSE
+
+When `1`, debug information from gazelle is printed to stderr.
+:::
diff --git a/gazelle/python/file_parser.go b/gazelle/python/file_parser.go
index 9101621..a1f47f4 100644
--- a/gazelle/python/file_parser.go
+++ b/gazelle/python/file_parser.go
@@ -69,18 +69,29 @@
}
root := tree.RootNode()
- if root.HasError() {
- log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path)
+ if !root.HasError() {
+ return root, nil
+ }
- 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())
- }
- }
+ log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path)
+
+ // Note: we intentionally do not return an error even when root.HasError because the parse
+ // failure may be in some part of the code that Gazelle doesn't care about.
+ verbose, envExists := os.LookupEnv("RULES_PYTHON_GAZELLE_VERBOSE")
+ if !envExists || verbose != "1" {
+ return root, nil
+ }
+
+ for i := 0; i < int(root.ChildCount()); i++ {
+ child := root.Child(i)
+ if child.IsError() {
+ // Example logs:
+ // gazelle: Parse error at {Row:1 Column:0}:
+ // def search_one_more_level[T]():
+ log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
+ // Log the internal tree-sitter representation of what was parsed. Eg:
+ // gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
+ log.Printf("The above was parsed as: %v", child.String())
}
}