uber / piranha

A tool for refactoring code related to feature flag APIs
Apache License 2.0
2.29k stars 198 forks source link

[Question] Error when running demo/find_replace_custom_cleanup_demos.py #707

Open sasidv opened 2 weeks ago

sasidv commented 2 weeks ago

Hi Team,

I'm trying to run the find_replace_custom_cleanup_demos.py file as the first step in getting started with Polyglot Piranha, but I encountered the following error when executing the python_demo() function.

thread '<unnamed>' panicked at src/utilities/tree_sitter_utilities.rs:191:3:
Could not fetch range or node for replace_node @assignment. Context: ["i", "list", "assignment"]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/Users/xxx/Documents/TreeSitter/TreeSitter/src/demo/find_replace_custom_cleanup_demos.py", line 80, in <module>
    python_demo()
  File "/Users/xxx/Documents/TreeSitter/TreeSitter/src/demo/find_replace_custom_cleanup_demos.py", line 67, in python_demo
    _ = execute_piranha(args)
pyo3_runtime.PanicException: Could not fetch range or node for replace_node @assignment. Context: ["i", "list", "assignment"]

However, I also noticed that when I remove the following entry from the only_lists.py file, the error disappears. Does anyone have any idea why this might be happening?

    deps3 = ['dependency2']
danieltrt commented 2 weeks ago

Ah, so one of the rules under piranha/demo/find_replace_custom_cleanup/python/configurations/rules.toml is a bit convoluted.

try to replace

# Deletes an assignment to an empty list. Example: `a = []`
#
# Note the multiple escapes (`\`) for the regex below
# `\[` (match the `[` character) -> `\\\\[`
# `\s*` (match zero or more empty spaces) -> `\\\\s*`
[[rules]]
name = "delete_empty_list_assignment"
query = """
(
(assignment
        left: (_) @i
        right: (list) @list
) @assignment
(#match? @list "\\\\[\\\\s*,?\\\\]")
)
"""
replace_node = "@assignment"
replace = ""
is_seed_rule = false

with

[[rules]]
name = "delete_empty_list_assignment"
query = """cs :[variable] = []"""
replace_node = "*"
replace = ""
is_seed_rule = false