twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

Diff output is incorrect for some files #2706

Closed tpoliaw closed 1 year ago

tpoliaw commented 1 year ago

Describe the bug

Running chezmoi diff ~/path/to/file shows a diff that does not apply to the file and/or highlights incorrect lines as being added or deleted.

From testing locally I can't see a consistent reason why some files fail yet.

To reproduce

I first came across this in a large i3 config file but the following was able to reproduce an example of it.

  1. Create a file ~/demo containing ten lines, eg one to ten.

    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
    ten
  2. Add it to chezmoi chezmoi add --template ~/demo

  3. In the chezmoi repo directory add {{ if true }} helloWorld {{ end}} as the second line to add a line containing helloWorld between one and two.

  4. Run chezmoi diff ~/demo

    diff --git a/demo b/demo
    index f00c965d8307308469e537302baa73048488f162..b5b97089d54f9f4d4b29f69fc33e9a1243e2a1e6 100644
    --- a/demo
    +++ b/demo
    @@ -1,3 +1,5 @@
    +one
    +one
    one
    two
    three

Expected behavior

I would expect the above diff to show

diff --git a/demo b/demo
index 3bb459b831ea471b9cd1cbb7c6d54a74251a711b..72b967b3dc880ca2b0cdd93cfcadc61dacf33763 100644
--- a/demo
+++ b/demo
@@ -1,4 +1,5 @@
 one
+ helloWorld
 two
 three
 four

Output of command with the --verbose flag

No change, the diff is the same and nothing else is output

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE warning version v2.29.3, built at 2023-01-19T23:30:23Z ok latest-version v2.29.3 ok os-arch linux/amd64 (Arch Linux) ok uname Linux 5.15.77-1-lts #1 SMP Thu, 03 Nov 2022 17:26:01 +0000 x86_64 GNU/Linux ok go-version go1.19.5 (gc) ok executable /usr/bin/chezmoi ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2023-01-22T16:09:52Z ok source-dir ~/.local/share/chezmoi is a git working tree (clean) ok suspicious-entries no suspicious entries ok working-tree ~/.local/share/chezmoi is a git working tree (clean) ok dest-dir ~ is a directory ok umask 022 ok cd-command found /bin/zsh ok cd-args /bin/zsh info diff-command not set ok edit-command found /usr/bin/nvim ok edit-args /usr/bin/nvim ok git-command found /usr/bin/git, version 2.39.1 ok merge-command found /usr/bin/vimdiff ok shell-command found /bin/zsh ok shell-args /bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.40 info pinentry-command not set info 1password-command op not found in $PATH ok bitwarden-command found /usr/bin/bw, version 2022.11.0 info gopass-command gopass not found in $PATH ok keepassxc-command found /usr/bin/keepassxc-cli, version 2.7.4 info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

It is difficult to pin down an instance that triggers the bug. In the example above for instance, adding the template line to somewhere other than the second line doesn't cause it. Similarly, changing the number of lines seems to stop the bug appearing. The original i3 config file that first showed the bug is a 246 line template generating a 235 line file (for this machine).

Running chezmoi apply -v shows the same diff but applies the correct change.

twpayne commented 1 year ago

Thanks for reporting this. I suspect that the bug is in the diff library that chezmoi uses, which I updated to the latest version recently.

Could you try with the old chezmoi 2.29.1 and report if the bug still occurs?

tpoliaw commented 1 year ago

Looks like that version is ok.

Incidentally, I kept trying to find a simpler example and I don't think it's related to templating. Adding the example above normally and then adding an empty line between one and two does the same thing.

twpayne commented 1 year ago

Yeah, I don't think this is related to templating in any way. It's the diff library, which has been shaky since version 1.2.0 onward.

tpoliaw commented 1 year ago

Ok, I'll use an external diff tool for now. Thanks for looking at it and for chezmoi in general. Have been very happy with it otherwise.

kdarkhan commented 1 year ago

I faced the same issue and there is indeed a bug in go-diff. I created this PR https://github.com/sergi/go-diff/pull/136 which should fix it. Hopefully the review won't take too long.

twpayne commented 1 year ago

Awesome, thank you @kdarkhan!

twpayne commented 1 year ago

I've released chezmoi v2.29.4 which reverts the diff library to a working version.