Repository: jesseduffield/lazygit (language: go)

Issue:
Lazygit crashes when searching in keybinding menu.
**Describe the bug**
Lazygit crashes when searching in keybinding menu.

**To Reproduce**
Steps to reproduce the behavior:

1. Go to 'local branch'
2. press '?' enter keybindings menu.
3. press '/' enter filter mode.
4. press 'fa'
5. software crashes.

```
panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListRenderer).insertNonModelItems(0x140005dbb78?, {0x14000f2e048, 0x1, 0x1400096c290?}, 0x2, 0x0, {0x140007133b0?, 0x102e2d594?, 0x140000a7cc0?}, {0x1400063e3f0, ...})
	github.com/jesseduffield/lazygit/pkg/gui/context/list_renderer.go:118 +0x31c
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListRenderer).renderLines(0x14000127890, 0xffffffffffffffff, 0xffffffffffffffff)
	github.com/jesseduffield/lazygit/pkg/gui/context/list_renderer.go:90 +0x1a4
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).HandleRender(0x14000127880)
	github.com/jesseduffield/lazygit/pkg/gui/context/list_context_trait.go:96 +0x44
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).postRefreshUpdate(0x14000122f08, {0x1032db5b8, 0x1400000ea98})
	github.com/jesseduffield/lazygit/pkg/gui/view_helpers.go:139 +0x98
github.com/jesseduffield/lazygit/pkg/gui.(*guiCommon).PostRefreshUpdate(0x103917720?, {0x1032db5b8?, 0x1400000ea98?})
	github.com/jesseduffield/lazygit/pkg/gui/gui_common.go:33 +0x28
github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers.(*SearchHelper).OnPromptContentChanged(0x1400007c920, {0x1400063e270, 0x2})
	github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers/search_helper.go:222 +0x228
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).searchEditor(0x14000122f08, 0x140003bf400, 0x53e8?, 0x140?, 0x21c0?)
	github.com/jesseduffield/lazygit/pkg/gui/editors.go:98 +0x74
github.com/jesseduffield/gocui.EditorFunc.Edit(0x140005f5610?, 0x2?, 0x0?, 0x0?, 0x75c8?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/edit.go:23 +0x34
github.com/jesseduffield/gocui.(*Gui).execKeybindings(0x14000104c40, 0x140003bf400, 0x140005f5590)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/gui.go:1438 +0x408
github.com/jesseduffield/gocui.(*Gui).onKey(0x140000ba628?, 0x140005f5510?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/gui.go:1281 +0xc8
github.com/jesseduffield/gocui.(*Gui).handleEvent(0x140000ba600?, 0x140005f5578?)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/gui.go:800 +0x54
github.com/jesseduffield/gocui.(*Gui).processEvent(0x14000104c40)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/gui.go:754 +0x178
github.com/jesseduffield/gocui.(*Gui).MainLoop(0x14000104c40)
	github.com/jesseduffield/gocui@v0.3.1-0.20240309085756-86e0d5a312de/gui.go:741 +0xac
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).Run(0x14000122f08, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:705 +0x52c
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError.func1()
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:711 +0x40
github.com/jesseduffield/lazygit/pkg/utils.SafeWithError(0x140002a0b70?)
	github.com/jesseduffield/lazygit/pkg/utils/utils.go:117 +0x60
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError(0x14000122f08, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/gui/gui.go:710 +0xac
github.com/jesseduffield/lazygit/pkg/app.(*App).Run(...)
	github.com/jesseduffield/lazygit/pkg/app/app.go:266
github.com/jesseduffield/lazygit/pkg/app.Run({0x1032d57d0?, 0x140002c2be0?}, 0x1400013d5c0, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	github.com/jesseduffield/lazygit/pkg/app/app.go:49 +0x9c
github.com/jesseduffield/lazygit/pkg/app.Start(0x14000051ef8, {0x0, 0x0})
	github.com/jesseduffield/lazygit/pkg/app/entry_point.go:150 +0x9ac
main.main()
	github.com/jesseduffield/lazygit/main.go:23 +0x98
```

**Version info:**
_Run `lazygit --version`
commit=, build date=, build source=homebrew, version=0.41.0, os=darwin, arch=arm64, git version=2.44.0

_Run `git --version`
git version 2.44.0

macOS 14.4.1




Discussion / hints:
Doesn't crash for me, I wonder what's different on your machine. Do you have any custom commands configured?
I tested it again. I have set some of the keybindings to `null`. When the search result hits the one with `null` keybindings, it crashes.

Fix patch:
diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go
index 9db09b74fb0..6d87d2bb774 100644
--- a/pkg/gui/context/menu_context.go
+++ b/pkg/gui/context/menu_context.go
@@ -74,9 +74,6 @@ func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem, columnAlignment
 // TODO: move into presentation package
 func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
 	menuItems := self.FilteredListViewModel.GetItems()
-	showKeys := lo.SomeBy(menuItems, func(item *types.MenuItem) bool {
-		return item.Key != nil
-	})
 
 	return lo.Map(menuItems, func(item *types.MenuItem, _ int) []string {
 		displayStrings := item.LabelColumns
@@ -84,12 +81,12 @@ func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
 			displayStrings[0] = style.FgDefault.SetStrikethrough().Sprint(displayStrings[0])
 		}
 
-		if !showKeys {
-			return displayStrings
+		keyLabel := ""
+		if item.Key != nil {
+			keyLabel = style.FgCyan.Sprint(keybindings.LabelFromKey(item.Key))
 		}
 
-		keyLabel := keybindings.LabelFromKey(item.Key)
-		displayStrings = utils.Prepend(displayStrings, style.FgCyan.Sprint(keyLabel))
+		displayStrings = utils.Prepend(displayStrings, keyLabel)
 		return displayStrings
 	})
 }


Test patch:
diff --git a/pkg/integration/tests/filter_and_search/filter_menu_with_no_keybindings.go b/pkg/integration/tests/filter_and_search/filter_menu_with_no_keybindings.go
new file mode 100644
index 00000000000..40aa8a3d15d
--- /dev/null
+++ b/pkg/integration/tests/filter_and_search/filter_menu_with_no_keybindings.go
@@ -0,0 +1,32 @@
+package filter_and_search
+
+import (
+	"github.com/jesseduffield/lazygit/pkg/config"
+	. "github.com/jesseduffield/lazygit/pkg/integration/components"
+)
+
+var FilterMenuWithNoKeybindings = NewIntegrationTest(NewIntegrationTestArgs{
+	Description:  "Filtering the keybindings menu so that only entries without keybinding are left",
+	ExtraCmdArgs: []string{},
+	Skip:         false,
+	SetupConfig: func(config *config.AppConfig) {
+		config.UserConfig.Keybinding.Universal.ToggleWhitespaceInDiffView = "<disabled>"
+	},
+	SetupRepo: func(shell *Shell) {
+	},
+	Run: func(t *TestDriver, keys config.KeybindingConfig) {
+		t.Views().Files().
+			IsFocused().
+			Press(keys.Universal.OptionMenu)
+
+		t.ExpectPopup().Menu().
+			Title(Equals("Keybindings")).
+			Filter("whitespace").
+			Lines(
+				// menu has filtered down to the one item that matches the
+				// filter, and it doesn't have a keybinding
+				Equals("--- Global ---"),
+				Equals("Toggle whitespace").IsSelected(),
+			)
+	},
+})
diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go
index 1dbfe95fb46..a7257fe91be 100644
--- a/pkg/integration/tests/test_list.go
+++ b/pkg/integration/tests/test_list.go
@@ -146,6 +146,7 @@ var tests = []*components.IntegrationTest{
 	filter_and_search.FilterFuzzy,
 	filter_and_search.FilterMenu,
 	filter_and_search.FilterMenuCancelFilterWithEscape,
+	filter_and_search.FilterMenuWithNoKeybindings,
 	filter_and_search.FilterRemoteBranches,
 	filter_and_search.FilterRemotes,
 	filter_and_search.FilterSearchHistory,