helix-editor / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
33.85k stars 2.51k forks source link

Helix crashed on "Undo change" (u) [undo] #5733

Closed souserge closed 1 year ago

souserge commented 1 year ago

Summary

I had an opened JS file in helix. I accidentally pasted some text from the system clipboard several times by making a three-finger gesture swiping from right to left (the pasting action is probably due to me using tmux). I pressed 'u' to undo it and it said "Already at oldest change". After pressing Escape, I could undo the change but after pressing it a few times, Helix crashed with the error message presented in the Helix log. I may have tried writing the file with :w (don't remember exactly).

Additionally, I have the JavaScript language server running, and auto-format using prettier.

Reproduction Steps

I tried this: 1. `tmux` 2. `hx file.js` 3. paste something form the system's clipboard. 4. press `u` 5. press `` 6. press `u` a few times 7. press `:w` I expected this to happen: Changes are undone and the file is saved with formatting Instead, this happened: The editor has crashed. ### Helix log No related logs were recorded but the program crashed with the following error outputed to the terminal: ``` thread 'main' panicked at 'assertion failed: original_doc.len_chars() == self.len', helix-core/src/transaction.rs:265:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` ### Platform Linux (Fedora 37) ### Terminal Emulator alacritty 0.10.1 ### Helix Version helix 22.12
pascalkuthe commented 1 year ago

A bit of a longshot but the problem maybe fixed by #5711 if your file contains any non-ascii chars. Could you try again with that PR? Otherwise please post your file with which you experienced the crash so we can reproduce and test the problem ourselfs.

gabydd commented 1 year ago

https://github.com/helix-editor/helix/issues/1410 has the same panic, it seems to have to do with paste and then undo

pascalkuthe commented 1 year ago

1410 has the same panic, it seems to have to do with paste and then undo

Any kind of incorrect transaction applied to a document can cause crashes like these. A ton of different bugs can cause incorrect transactions to be produced so the panic message doesn't tell us much about the cause. Since #1410 was reported most of these issues have been fixed (many by @the-mikedavis). So this is likely a different bug. It sounds like it's LSP related hence I suggested #5711 since I had similar crashes while testing that (before I finally fixed them all)

the-mikedavis commented 1 year ago

I can't reproduce this with just those steps: can you reliably reproduce this with some exact clipboard contents? If you can reproduce could you capture the full backtrace by setting RUST_BACKTRACE=1?

Also: do you have a language server installed for javascript and have you configured it to format on save?

pascalkuthe commented 1 year ago

Also: do you have a language server installed for javascript and have you configured it to format on save?

I think he does, from the issue description:

Additionally, I have the JavaScript language server running, and auto-format using prettier.

Which reminded me of #4791 (hence my guess that #5711 may fix this)

archseer commented 1 year ago

I had a similar crash lately, it happened when I was pasting via middle click (and I might have been in insert mode?) then undo. It was a Go file in my case

souserge commented 1 year ago

@pascalkuthe @the-mikedavis I'm not sure if I reproduced the same bug I had last time, since now the panic output looks different. Nevertheless, the circumstances are very similar so it's probably caused by the same issue.

Some observations

Environment:

Steps to replicate

(I also recorded it using asciinema).

  1. create a JS file (e.g. testHelixbug.js) with the following contents:
    
    // Make sure the mouse isn't disabled (editor.mouse in config.toml)
    // Steps to reproduce:
    //   * Click on the last line (marked with tilde)
    //   * Press: <middle-click> <middle-click> <middle-click> u <middle-click> u

function test() { return "Hello, world!"; }


2. Copy the following text to your system clipboard: `helix editor`
3. Open the file: `$ RUST_BACKTRACE=full hx testHelixBug.js`
4. Click somewhere on the last line (should be marked with a tilda) with a mouse. You don't need to actually click it as long as you have the cursor there.
5. Press: `<middle-click> <middle-click> <middle-click> u <middle-click> u`
6. Observe the crash (`Position` and `len` might differ):
<pre>
thread 'main' panicked at 'Position 307 is out of range for changeset len 302!', helix-core/src/transaction.rs:397:13
stack backtrace:
   0:     0x558872968465 - &lt;unknown&gt;
   1:     0x5588720489ae - &lt;unknown&gt;
   2:     0x558872962105 - &lt;unknown&gt;
   3:     0x558872969de3 - &lt;unknown&gt;
   4:     0x558872969aef - &lt;unknown&gt;
   5:     0x55887296a4af - &lt;unknown&gt;
   6:     0x55887296a314 - &lt;unknown&gt;
   7:     0x55887296896c - &lt;unknown&gt;
   8:     0x55887296a062 - &lt;unknown&gt;
   9:     0x558871fb5e43 - &lt;unknown&gt;
  10:     0x558872158572 - &lt;unknown&gt;
  11:     0x558872712a8f - &lt;unknown&gt;
  12:     0x558872714034 - &lt;unknown&gt;
  13:     0x558872608159 - &lt;unknown&gt;
  14:     0x55887248e646 - &lt;unknown&gt;
  15:     0x55887248e56e - &lt;unknown&gt;
  16:     0x558872490e5b - &lt;unknown&gt;
  17:     0x5588725c7845 - &lt;unknown&gt;
  18:     0x5588727afcc7 - &lt;unknown&gt;
  19:     0x5588727d058f - &lt;unknown&gt;
  20:     0x558872809a79 - &lt;unknown&gt;
  21:     0x55887282aa70 - &lt;unknown&gt;
  22:     0x5588727d1301 - &lt;unknown&gt;
  23:     0x5588728110f3 - &lt;unknown&gt;
  24:     0x5588728215f9 - &lt;unknown&gt;
  25:     0x55887295d6ba - &lt;unknown&gt;
  26:     0x5588727d1408 - &lt;unknown&gt;
  27:     0x7f5558d05510 - __libc_start_call_main
  28:     0x7f5558d055c9 - __libc_start_main@GLIBC_2.2.5
  29:     0x558871ff21d5 - &lt;unknown&gt;
  30:                0x0 - &lt;unknown&gt;
</pre>
pascalkuthe commented 1 year ago

Thank you so much for following up on this! I can reproduce this easily on the latest master and am looking into a fix. Detail reproduction cases like this are greatly appreciated. Not LSP related tough as this also occurs with :language text

pascalkuthe commented 1 year ago

turns out all paste commands don't append to history when called as a typable commands (which is a well known cause of crashes in combination with undo). Middle mouse just call :clipboard-paste-before, the same crash can also be reproduced by just using the typable command directly. #5790 fixes the problem

scimas commented 9 months ago

I have managed to get a panic very similar to this issue.

thread 'main' panicked at D:\a\helix\helix\helix-core\src\transaction.rs:461:9:
Positions [(8682, After)] are out of range for changeset len 8674!
note: run with `RUST...

Happened on an undo after a clipboard-paste-replace. I didn't open a new issue because I don't have reproduction steps.