zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
46.58k stars 2.64k forks source link

Setting tab_size above 16 causes editor to crash #14809

Open lillianrubyrose opened 1 month ago

lillianrubyrose commented 1 month ago

Check for existing issues

Describe the bug / provide steps to reproduce it

Set "tab_size" to anything above 16.

Environment

Zed: v0.145.0 (Zed Preview) OS: Linux Wayland arch unknown Memory: 30.5 GiB Architecture: x86_64

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

Zed.log

2024-07-19T09:13:33.120790545-04:00 [ERROR] {
  "thread": "",
  "payload": "byte index 69 is out of bounds of `                `",
  "location_data": {
    "file": "crates/editor/src/display_map/tab_map.rs",
    "line": 543
  },
  "backtrace": [],
  "app_version": "0.145.0",
  "release_channel": "Zed Preview",
  "os_name": "Linux Wayland",
  "os_version": "arch unknown",
  "architecture": "x86_64",
  "panicked_on": 1721394813120,
  "installation_id": "bd388363-4955-4421-9138-5b2ee8a76d6e",
  "session_id": "42f8b191-08fa-4e0b-b454-9614009643f4"
}
jadijadi commented 1 month ago

Tried on Mac OSX & Debian x64 Wayland and were not able to reproduce.

Configuring tab_size to 25 in settings does not crashes the Zed & changes the tab size to 25 correctly.

Zed Version: 0.146.0 (main)

lillianrubyrose commented 1 month ago

Tried on Mac OSX & Debian x64 Wayland and were not able to reproduce.

Configuring tab_size to 25 in settings does not crashes the Zed & changes the tab size to 25 correctly.

Zed Version: 0.146.0 (main)

After a little more playing around with it the crash only occurs when the file has tab characters in it. It wasn't clear from the original issue, my fault for not playing with it further, sorry!

It does work correctly if a file is formatted with space characters. (The Zed settings.json for example)

Easiest repro I can think of: Set hard_tabs = true in a Rust project's rustfmt.toml then cargo fmt --all and open a formatted file. Always causes the crash for me.

Zed: v0.147.0 (Zed Dev 777ddefa73c03e169414cba1146eaa9ed21eeb48)

jadijadi commented 1 month ago

Thanks @lilyrrose for the detailed report, got it and I think I know where the root cause is. working on it.

jadijadi commented 1 month ago

The problem is in src/display_map/tab_map.rs where we have on line 542:

                        return Some(Chunk {
                            text: &SPACES[..len as usize],
                            is_tab: true,
                            ..self.chunk.clone()
                        });

This SPACE is defined on line 458:

// Handles a tab width <= 16
const SPACES: &str = "                ";
  1. setting text to something like text: &" ".repeat(len as usize) looks great but I did not figure out how to do it without referencing a temporary value. Any suggestions?
  2. Another method is increasing 16 to something like 32 and mentioning this limitation in docs and checking tab_size for that.

The solution 2 is not ideal but will prevent the current crash.

Any suggestions?