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

chore(gazelle): Rename GAZELLE_VERBOSE env var; use idiomatic go; add comments #2428

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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