Skip to content

Commit da0893a

Browse files
committed
fix(cli): explicitly close tree-sitter cursors and ast (#8243)
Fix #818 See also: smacker/go-tree-sitter#183 smacker/go-tree-sitter#182 --- ### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: yes Ensure `configure` tree-sitter resources are explicitly cleaned up. ### Test plan - Covered by existing test cases GitOrigin-RevId: 35c3509f084f0d7c2e01a9d11faa2519462dc46b
1 parent 4b97684 commit da0893a

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

gazelle/common/treesitter/parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type AST interface {
5656
// TODO: delete
5757
QueryStrings(query TreeQuery, returnVar string) []string
5858
RootNode() *sitter.Node
59+
60+
// Release all resources related to this AST.
61+
// The AST is most likely no longer usable after this call.
62+
Close()
5963
}
6064
type treeAst struct {
6165
lang LanguageGrammar
@@ -67,6 +71,12 @@ type treeAst struct {
6771

6872
var _ AST = (*treeAst)(nil)
6973

74+
func (tree *treeAst) Close() {
75+
tree.sitterTree.Close()
76+
tree.sitterTree = nil
77+
tree.sourceCode = nil
78+
}
79+
7080
func (tree *treeAst) String() string {
7181
return fmt.Sprintf("treeAst{\n lang: %q,\n filePath: %q,\n AST:\n %v\n}", tree.lang, tree.filePath, tree.sitterTree.RootNode().String())
7282
}

gazelle/common/treesitter/queries.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (tree *treeAst) QueryStrings(query TreeQuery, returnVar string) []string {
3737

3838
// Execute the query.
3939
qc := sitter.NewQueryCursor()
40+
defer qc.Close()
4041
qc.Exec(sitterQuery.q, rootNode)
4142

4243
// Collect string from the query results.
@@ -83,6 +84,7 @@ func (tree *treeAst) Query(query TreeQuery) <-chan ASTQueryResult {
8384
// Execute the query.
8485
go func() {
8586
qc := sitter.NewQueryCursor()
87+
defer qc.Close()
8688
qc.Exec(q.q, rootNode)
8789

8890
for {
@@ -167,6 +169,7 @@ func (tree *treeAst) QueryErrors() []error {
167169

168170
// Execute the import query
169171
qc := sitter.NewQueryCursor()
172+
defer qc.Close()
170173
qc.Exec(query.q, node)
171174

172175
// Collect import statements from the query results

gazelle/js/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func ParseSource(filePath string, sourceCode []byte) (ParseResult, []error) {
5858
}
5959

6060
if tree != nil {
61+
defer tree.Close()
6162
rootNode := tree.RootNode()
6263
rootNodeChildCount := int(rootNode.NamedChildCount())
6364

gazelle/kotlin/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (p *treeSitterParser) Parse(filePath string, sourceCode []byte) (*ParseResu
4545
}
4646

4747
if tree != nil {
48+
defer tree.Close()
4849
rootNode := tree.RootNode()
4950

5051
// Extract imports from the root nodes

0 commit comments

Comments
 (0)