Skip to content

Commit

Permalink
chore(gazelle): Rename GAZELLE_VERBOSE env var; use idiomatic go; add…
Browse files Browse the repository at this point in the history
… comments (#2428)

Address PR comments from #2420:

+ Add and update comments
+ idiomatic go: return early
+ rename and document env var
  • Loading branch information
dougthor42 authored Nov 20, 2024
1 parent b9b0948 commit 4a55ef4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Other changes:
{#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
Expand Down
6 changes: 6 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ stderr.

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.
:::
35 changes: 23 additions & 12 deletions gazelle/python/file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,29 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
}

root := tree.RootNode()
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)

// 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())
}
}

Expand Down

0 comments on commit 4a55ef4

Please sign in to comment.