Skip to content

Commit 4a55ef4

Browse files
authored
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
1 parent b9b0948 commit 4a55ef4

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Other changes:
8787
{#v0-0-0-added}
8888
### Added
8989
* (gazelle): Parser failures will now be logged to the terminal. Additional
90-
details can be logged by setting `GAZELLE_VERBOSE=1`.
90+
details can be logged by setting `RULES_PYTHON_GAZELLE_VERBOSE=1`.
9191
* (toolchains) allow users to select which variant of the support host toolchain
9292
they would like to use through
9393
`RULES_PYTHON_REPO_TOOLCHAIN_{VERSION}_{OS}_{ARCH}` env variable setting. For

docs/environment-variables.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ stderr.
5656

5757
When `1`, debug information about coverage behavior is printed to stderr.
5858
:::
59+
60+
61+
:::{envvar} RULES_PYTHON_GAZELLE_VERBOSE
62+
63+
When `1`, debug information from gazelle is printed to stderr.
64+
:::

gazelle/python/file_parser.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,29 @@ func ParseCode(code []byte, path string) (*sitter.Node, error) {
6969
}
7070

7171
root := tree.RootNode()
72-
if root.HasError() {
73-
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path)
74-
75-
verbose, envExists := os.LookupEnv("GAZELLE_VERBOSE")
76-
if envExists && verbose == "1" {
77-
for i := 0; i < int(root.ChildCount()); i++ {
78-
child := root.Child(i)
79-
if child.IsError() {
80-
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
81-
log.Printf("The above was parsed as: %v", child.String())
82-
}
83-
}
72+
if !root.HasError() {
73+
return root, nil
74+
}
75+
76+
log.Printf("WARNING: failed to parse %q. The resulting BUILD target may be incorrect.", path)
77+
78+
// Note: we intentionally do not return an error even when root.HasError because the parse
79+
// failure may be in some part of the code that Gazelle doesn't care about.
80+
verbose, envExists := os.LookupEnv("RULES_PYTHON_GAZELLE_VERBOSE")
81+
if !envExists || verbose != "1" {
82+
return root, nil
83+
}
84+
85+
for i := 0; i < int(root.ChildCount()); i++ {
86+
child := root.Child(i)
87+
if child.IsError() {
88+
// Example logs:
89+
// gazelle: Parse error at {Row:1 Column:0}:
90+
// def search_one_more_level[T]():
91+
log.Printf("Parse error at %+v:\n%+v", child.StartPoint(), child.Content(code))
92+
// Log the internal tree-sitter representation of what was parsed. Eg:
93+
// gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
94+
log.Printf("The above was parsed as: %v", child.String())
8495
}
8596
}
8697

0 commit comments

Comments
 (0)