helix-editor / helix

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

Panic when exiting search #5632

Open par5er opened 1 year ago

par5er commented 1 year ago

Summary

Editor panics when exiting search

Reproduction Steps

I tried this: 1. `hx .` 2. Open a small file 3. Make a vertical split and open a larger file in the second pane 4. Search for something in the larger file and hit ENTER 5. Search for something again, and this time switch to the first pane without closing the search dialog 6. Hit ESCAPE and the program crashes I expected this to happen: The search dialog to close Instead, this happened: https://asciinema.org/a/eLc59nJruhSfKI2qW7whCK5ig ### Helix log
~/.cache/helix/helix.log ``` please provide a copy of `~/.cache/helix/helix.log` here if possible, you may need to redact some of the lines ```
### Platform Linux (Debian 11) ### Terminal Emulator st ### Helix Version helix 22.12 (7ac72a39)
archseer commented 1 year ago
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace    
the-mikedavis commented 1 year ago

I can reproduce this. On a debug build the backtrace is:

thread 'main' panicked at 'assertion failed: char_idx <= slice.len_chars()', helix-core/src/graphemes.rs:37:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
   2: core::panicking::panic
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:48:5
   3: helix_core::graphemes::nth_prev_grapheme_boundary
             at ./helix-core/src/graphemes.rs:37:5
   4: helix_core::graphemes::prev_grapheme_boundary
             at ./helix-core/src/graphemes.rs:79:5
   5: helix_core::graphemes::ensure_grapheme_boundary_prev
             at ./helix-core/src/graphemes.rs:194:9
   6: helix_core::selection::Range::grapheme_aligned
             at ./helix-core/src/selection.rs:271:17
   7: helix_core::selection::Selection::ensure_invariants::{{closure}}
             at ./helix-core/src/selection.rs:586:28
   8: helix_core::selection::Selection::transform
             at ./helix-core/src/selection.rs:574:22
   9: helix_core::selection::Selection::ensure_invariants
             at ./helix-core/src/selection.rs:586:9
  10: helix_view::document::Document::set_selection
             at ./helix-view/src/document.rs:742:30
  11: helix_term::ui::regex_prompt::{{closure}}
             at ./helix-term/src/ui/mod.rs:105:29
  12: <alloc::boxed::Box<F,A> as core::ops::function::FnMut<Args>>::call_mut
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/boxed.rs:1868:9
  13: <helix_term::ui::prompt::Prompt as helix_term::compositor::Component>::handle_event
             at ./helix-term/src/ui/prompt.rs:566:21
  14: helix_term::compositor::Compositor::handle_event
             at ./helix-term/src/compositor.rs:143:19
  15: helix_term::application::Application::handle_terminal_events::{{closure}}
             at ./helix-term/src/application.rs:632:22
  16: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/future/mod.rs:91:19
  17: helix_term::application::Application::event_loop_until_idle::{{closure}}
             at ./helix-term/src/application.rs:347:55
  18: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/future/mod.rs:91:19
  19: helix_term::application::Application::event_loop::{{closure}}
             at ./helix-term/src/application.rs:326:57
  20: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/future/mod.rs:91:19
  21: helix_term::application::Application::run::{{closure}}
             at ./helix-term/src/application.rs:1073:38
  22: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/future/mod.rs:91:19
  23: hx::main_impl::{{closure}}
             at ./helix-term/src/main.rs:156:53
  24: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/future/mod.rs:91:19
  25: tokio::runtime::park::CachedParkThread::block_on::{{closure}}
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/park.rs:283:63
  26: tokio::runtime::coop::with_budget
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/coop.rs:102:5
  27: tokio::runtime::coop::budget
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/coop.rs:68:5
  28: tokio::runtime::park::CachedParkThread::block_on
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/park.rs:283:31
  29: tokio::runtime::context::BlockingRegionGuard::block_on
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/context.rs:315:13
  30: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/scheduler/multi_thread/mod.rs:66:9
  31: tokio::runtime::runtime::Runtime::block_on
             at /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.24.1/src/runtime/runtime.rs:284:45
  32: hx::main_impl
             at ./helix-term/src/main.rs:158:5
  33: hx::main
             at ./helix-term/src/main.rs:38:21
  34: core::ops::function::FnOnce::call_once
             at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

When switching windows I think we want to cancel out of the prompt first. It looks like we're trying to jump to the next search match and if the split's file is shorter than the search file, we get a panic.

pascalkuthe commented 1 year ago

I can also reproduce. The same crash also happens for other prompts like s or k.

I think the correct fix for this would be to call Composistor::remove(..) (with a new id returned by prompt.id()) in Editor::_refresh. The problem is that we can't do that because the compositor is currently in helix-term so a fix is also blocked on #5555/#5581.