afnanenayet / diffsitter

A tree-sitter based AST difftool to get meaningful semantic diffs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Out of Bounds error while diffing two Terraform files

bgshacklett opened this issue · comments

Describe the bug
Out of Bounds error while diffing two Terraform files

To Reproduce
Steps to reproduce the behavior:

  1. Configure diffsitter to treat .tf files as hcl.
{
  "grammar": {
    "file-associations": {
      "tf": "hcl"
    }
  }
}
  1. Create two Terraform files:
cat <<EOF > test-a.tf
locals {
}
EOF
cat <<EOF > test-b.tf
locals {
  foo = "foo"
}
EOF
  1. Diff the two files:
❯ diffsitter test-a.tf test-b.tf
  1. An error is generated:
thread 'main' panicked at 'byte index 14 is out of bounds of `  foo = "foo"`', src/formatting.rs:440:43

Expected behavior
The diff should be printed without any error.

Log output/screenshots

❯ RUST_BACKTRACE=full diffsitter --debug test-a.tf test-b.tf
 2022-11-16T20:11:09.402Z DEBUG diffsitter > Checking if test-a.tf can be parsed
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Deduced language "hcl" from extension "tf" provided from user mappings
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Using tree-sitter parser for language hcl
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Succeeded loading grammar for hcl
 2022-11-16T20:11:09.402Z DEBUG diffsitter        > Checking if test-b.tf can be parsed
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Deduced language "hcl" from extension "tf" provided from user mappings
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Using tree-sitter parser for language hcl
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Succeeded loading grammar for hcl
 2022-11-16T20:11:09.402Z DEBUG diffsitter        > Extensions for both input files are supported
 2022-11-16T20:11:09.402Z DEBUG diffsitter        > Reading test-a.tf to string
 2022-11-16T20:11:09.402Z INFO  diffsitter        > Will deduce filetype from file extension
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Deduced language "hcl" from extension "tf" provided from user mappings
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Using tree-sitter parser for language hcl
 2022-11-16T20:11:09.402Z INFO  diffsitter::parse > Succeeded loading grammar for hcl
 2022-11-16T20:11:09.402Z DEBUG diffsitter::parse > Constructed parser
 2022-11-16T20:11:09.402Z DEBUG diffsitter::parse > Parsed AST
 2022-11-16T20:11:09.402Z INFO  TimerFinished     > parse::parse_file(), Elapsed=225.133µs
 2022-11-16T20:11:09.403Z DEBUG diffsitter        > Reading test-b.tf to string
 2022-11-16T20:11:09.403Z INFO  diffsitter        > Will deduce filetype from file extension
 2022-11-16T20:11:09.403Z INFO  diffsitter::parse > Deduced language "hcl" from extension "tf" provided from user mappings
 2022-11-16T20:11:09.403Z INFO  diffsitter::parse > Using tree-sitter parser for language hcl
 2022-11-16T20:11:09.403Z INFO  diffsitter::parse > Succeeded loading grammar for hcl
 2022-11-16T20:11:09.403Z DEBUG diffsitter::parse > Constructed parser
 2022-11-16T20:11:09.403Z DEBUG diffsitter::parse > Parsed AST
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > parse::parse_file(), Elapsed=73.527µs
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > ast::from_ts_tree(), Elapsed=18.268µs
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > ast::process(), Elapsed=37.945µs
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > ast::from_ts_tree(), Elapsed=3.375µs
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > ast::process(), Elapsed=9.209µs
 2022-11-16T20:11:09.403Z INFO  TimerFinished     > diff::compute_edit_script(), Elapsed=4.517µs
 2022-11-16T20:11:09.403Z INFO  diffsitter::formatting > Detected terminal width: 104 columns
 2022-11-16T20:11:09.403Z INFO  diffsitter::formatting > Using stack style horizontal for title
 2022-11-16T20:11:09.403Z DEBUG diffsitter::formatting > Printing remaining old hunks
 2022-11-16T20:11:09.403Z DEBUG diffsitter::formatting > Printing remaining new hunks
 2022-11-16T20:11:09.403Z DEBUG diffsitter::formatting > Printing hunk (lines 1 - 1)
 2022-11-16T20:11:09.403Z DEBUG diffsitter::formatting > Title string has length of 3
 2022-11-16T20:11:09.403Z DEBUG diffsitter::formatting > Printing line 1
thread 'main' panicked at 'byte index 14 is out of bounds of `  foo = "foo"`', src/formatting.rs:440:43
stack backtrace:
   0:        0x10026b306 - __mh_execute_header
   1:        0x10021093b - __mh_execute_header
   2:        0x1002672e0 - __mh_execute_header
   3:        0x10026ebbb - __mh_execute_header
   4:        0x10026f428 - __mh_execute_header
   5:        0x10026ef24 - __mh_execute_header
   6:        0x10026ee99 - __mh_execute_header
   7:        0x10026ee55 - __mh_execute_header
   8:        0x10036da03 - __mh_execute_header
   9:        0x10036deec - __mh_execute_header
  10:        0x100185de8 - __mh_execute_header
  11:        0x10019468b - __mh_execute_header
  12:        0x1001a9096 - __mh_execute_header
  13:        0x1001ab227 - __mh_execute_header
  14:     0x7ff809791310 - <unknown>
 2022-11-16T20:11:09.404Z INFO  TimerFinished          > formatting::print(), Elapsed=727.907µs
test-a.tf -> test-b.tf
======================

1:
--
+   foo = "foo"%

Platform:
OS: macOS 13 (Ventura)

Possibly related to #468?

Thanks for the report, this is an interesting little bug. The Go one appears to because I assumed that there would never be a node that's just a newline, unclear if this one is the same

The bugfix for #468 is in the latest release. Could you let me know if this fixes your issue here?

It looks like that did it!