diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a1c17d8b8..9cd6b82759 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 906281d56f..12c1bcf0c2 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -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. +::: diff --git a/gazelle/python/file_parser.go b/gazelle/python/file_parser.go index 9101621639..a1f47f400c 100644 --- a/gazelle/python/file_parser.go +++ b/gazelle/python/file_parser.go @@ -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()) } }