Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b28db69

Browse filesBrowse files
refactor: Add GAZELLE_VERBOSE env and log parser failures (bazel-contrib#2420)
While investigating bazel-contrib#2396 and why bazel-contrib#2413 doesn't appear to be working for us, I realized that one of the things I was making heavy use of was additional parser logging that I had added. This adds some of that logging. I also throw in some documentation because I found it helpful. Users can (attempt to) get additional parse failure information by setting the `GAZELLE_VERBOSE` environment variable to `1`. Eg: ```console $ GAZELLE_VERBOSE=1 bazel run //:gazelle ``` Here are some example logs: ```console $ GAZELLE_VERBOSE=1 bazel run //:gazelle INFO: Invocation ID: a4e026d8-17df-426c-b1cc-d3980690dd53 ... INFO: Running command line: bazel-bin/gazelle INFO: Streaming build results to: https://btx.cloud.google.com/invocations/a4e026d8-17df-426c-b1cc-d3980690dd53 gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect. gazelle: Parse error at {Row:1 Column:0}: def search_one_more_level[T](): gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list))) gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention. $ $ bazel run //:gazelle INFO: Invocation ID: 726c9fd6-f566-4c30-95ef-c4781ad155de ... INFO: Running command line: bazel-bin/gazelle INFO: Streaming build results to: https://btx.cloud.google.com/invocations/726c9fd6-f566-4c30-95ef-c4781ad155de gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect. gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention. ``` --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com> Co-authored-by: Richard Levasseur <rlevasseur@google.com>
1 parent 15e9b4a commit b28db69
Copy full SHA for b28db69

File tree

2 files changed

+34
-4
lines changed
Filter options

2 files changed

+34
-4
lines changed

‎CHANGELOG.md

Copy file name to clipboardExpand all lines: CHANGELOG.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ Unreleased changes template.
6060

6161
{#v0-0-0-added}
6262
### Added
63-
* Nothing added.
63+
* (gazelle): Parser failures will now be logged to the terminal. Additional
64+
details can be logged by setting `GAZELLE_VERBOSE=1`.
6465

6566
{#v0-0-0-removed}
6667
### Removed

‎gazelle/python/file_parser.go

Copy file name to clipboardExpand all lines: gazelle/python/file_parser.go
+32-3Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package python
1717
import (
1818
"context"
1919
"fmt"
20+
"log"
2021
"os"
2122
"path/filepath"
2223
"strings"
@@ -55,7 +56,10 @@ func NewFileParser() *FileParser {
5556
return &FileParser{}
5657
}
5758

58-
func ParseCode(code []byte) (*sitter.Node, error) {
59+
// ParseCode instantiates a new tree-sitter Parser and parses the python code, returning
60+
// the tree-sitter RootNode.
61+
// It prints a warning if parsing fails.
62+
func ParseCode(code []byte, path string) (*sitter.Node, error) {
5963
parser := sitter.NewParser()
6064
parser.SetLanguage(python.GetLanguage())
6165

@@ -64,9 +68,27 @@ func ParseCode(code []byte) (*sitter.Node, error) {
6468
return nil, err
6569
}
6670

67-
return tree.RootNode(), nil
71+
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+
}
84+
}
85+
}
86+
87+
return root, nil
6888
}
6989

90+
// parseMain returns true if the python file has an `if __name__ == "__main__":` block,
91+
// which is a common idiom for python scripts/binaries.
7092
func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
7193
for i := 0; i < int(node.ChildCount()); i++ {
7294
if err := ctx.Err(); err != nil {
@@ -94,6 +116,8 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
94116
return false
95117
}
96118

119+
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
120+
// representing if the parse was OK or not.
97121
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
98122
switch node.Type() {
99123
case sitterNodeTypeDottedName:
@@ -112,6 +136,9 @@ func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
112136
return module{}, false
113137
}
114138

139+
// parseImportStatements parses a node for import statements, returning true if the node is
140+
// an import statement. It updates FileParser.output.Modules with the `module` that the
141+
// import represents.
115142
func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
116143
if node.Type() == sitterNodeTypeImportStatement {
117144
for j := 1; j < int(node.ChildCount()); j++ {
@@ -146,6 +173,8 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
146173
return true
147174
}
148175

176+
// parseComments parses a node for comments, returning true if the node is a comment.
177+
// It updates FileParser.output.Comments with the parsed comment.
149178
func (p *FileParser) parseComments(node *sitter.Node) bool {
150179
if node.Type() == sitterNodeTypeComment {
151180
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
@@ -180,7 +209,7 @@ func (p *FileParser) parse(ctx context.Context, node *sitter.Node) {
180209
}
181210

182211
func (p *FileParser) Parse(ctx context.Context) (*ParserOutput, error) {
183-
rootNode, err := ParseCode(p.code)
212+
rootNode, err := ParseCode(p.code, p.relFilepath)
184213
if err != nil {
185214
return nil, err
186215
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.