DavisVaughan / r-tree-sitter

https://davisvaughan.github.io/r-tree-sitter/
Other
31 stars 2 forks source link

Possible bug with node pointers? #26

Open mpadge opened 1 month ago

mpadge commented 1 month ago

Hi Davis, thanks so much for your help thus far! I've encountered an issue which you can hopefully reproduce via simple demo repo at https://github.com/mpadge/r-tree-sitter-test. The README there has all info. As a local reprex here, based on a random CRAN tarball:

path <- test::dl_tarball ("odbc_1.5.0.tar.gz")

bodies <- test::get_captures (path)
#> Error: Input must point to a valid tree sitter type.
library (test)
bodies <- test::get_captures (path)
#> Error: Input must point to a valid tree sitter type.

But then everything works if the full namespace of that package is loaded:

pkg_path <- "/<local>/<path>/<to>/r-tree-sitter-test"
devtools::load_all (pkg_path)
#> ℹ Loading test
bodies <- test::get_captures (path)
length (bodies)
#> [1] 36

Created on 2024-10-02 with reprex v2.1.1

The only function it's calling is this:

QUERY_FUNCTIONS <- r"(
  (binary_operator
      lhs: (identifier) @name
      operator: "<-"
      rhs: (function_definition) @fn
  )
)"
QUERY_FUNCTIONS <- treesitter::query (treesitter.r::language (), QUERY_FUNCTIONS)

#' Get treesitter captures
#'
#' @export
get_captures <- function (path) {
    path_r <- fs::path (path, "R")
    paths <- fs::dir_ls (path_r, regexp = "\\.(r|R)$")
    paths <- as.character (paths)

    parser <- treesitter::parser (treesitter.r::language ())

    lapply (paths, function (path) {
        text <- brio::read_file (path)

        tree <- treesitter::parser_parse (parser, text)
        node <- treesitter::tree_root_node (tree)

        treesitter::query_captures (QUERY_FUNCTIONS, node)
    })
}

I guess there's something happening here with tracking and resetting the node pointers? I'd really appreciate any insight you could give; thanks!


Oh, and this behaviour is only seen when the function is embedded within a package namespace, hence my construction of test repo to demonstrate. You won't see the same if you try to locally step through r-tree-sitter code. Everything works then too, which makes it quite tricky to debug.

mpadge commented 1 month ago

Writing that last statement made me realise it's just the treesitter function not capturing the calling environment. That makes fixing it easy for me. Your choices are:

(1) Just ignore this and close for now; or (2) Ensure that the calling env is sufficiently enclosed in the x param expected by the query functions.

It's probably not overly important, so okay for you to just close. Thanks anyway :+1: