aswinkarthik / csvdiff

A fast diff tool for comparing csv files
https://aswinkarthik.github.io/csvdiff/
MIT License
532 stars 57 forks source link

Panic while word-diffing #56

Open mdruiter opened 1 year ago

mdruiter commented 1 year ago

I get an exception, while comparing two simple files using csvdiff -o word-diff:

x,[-C-]{+A+}
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/aswinkarthik/csvdiff/cmd.(*Formatter).wordLevelDiffs(0xc0000f7ac0, 0xb1a638, 0x0, 0x0, 0xc000182000, 0x2, 0x2, 0xb1a638, 0x0, 0x0, ...)
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/formatter.go:242 +0x1338
github.com/aswinkarthik/csvdiff/cmd.(*Formatter).wordDiff(...)
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/formatter.go:216
github.com/aswinkarthik/csvdiff/cmd.(*Formatter).Format(0xc0000f7ac0, 0xb1a638, 0x0, 0x0, 0xc000182000, 0x2, 0x2, 0xb1a638, 0x0, 0x0, ...)
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/formatter.go:51 +0x504
github.com/aswinkarthik/csvdiff/cmd.runContext(0xc0000f0000, 0x884060, 0xc000010018, 0x884060, 0xc000010020, 0x0, 0x0)
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/root.go:108 +0x4c5
github.com/aswinkarthik/csvdiff/cmd.glob..func2(0xaf89c0, 0xc0000243c0, 0x2, 0x4, 0x0, 0x0)
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/root.go:87 +0x283
github.com/spf13/cobra.(*Command).execute(0xaf89c0, 0xc000012060, 0x4, 0x4, 0xaf89c0, 0xc000012060)
        /home/travis/gopath/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:826 +0x460
github.com/spf13/cobra.(*Command).ExecuteC(0xaf89c0, 0xc00005cda0, 0xc000059f20, 0x43cb8a)
        /home/travis/gopath/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:914 +0x2fb
github.com/spf13/cobra.(*Command).Execute(...)
        /home/travis/gopath/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:864
github.com/aswinkarthik/csvdiff/cmd.Execute()
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/cmd/root.go:115 +0x76
main.main()
        /home/travis/gopath/src/github.com/aswinkarthik/csvdiff/main.go:29 +0x59

Interestingly enough, this even happens with two equal files, including the difference found ([-C-]{+A+}). My example data, for both files:

x,A
x,B
x,C

I tested both DOS (CRLF) and UNIX (LF) line endings. I'm running version 1.4.0 in Ubuntu via WSL2.

datatraveller1 commented 11 months ago

You are using the first field (default: -p0) as primary key. The issue is that three times "x" isn't a unique primary key.

mdruiter commented 11 months ago

Ah! I still would not expect a crash. Plus finding a difference where there is none is not helpful as default behavior...

raiki61 commented 1 month ago

Ah! I still would not expect a crash. Plus finding a difference where there is none is not helpful as default behavior...

The same thing happened to me too. i agree with your opinion