textinput: alt+d panics on the last rune of the value and deletes an extra word on the last rune of a word
Describe the bug
Pressing alt+d (the default DeleteWordForward binding, also alt+delete and ctrl+delete) with the cursor on the last rune of the value panics with an index out of range; with the cursor on the last rune of a word that is followed by another word, it deletes that next word as well. With the cursor anywhere earlier in a word it deletes the rest of that word only, as expected.
Setup
charm.land/bubbles/v2 v2.2.1 with charm.land/bubbletea/v2 v2.0.8, go1.27.1, macOS 26.6. The program below drives Update directly, so the terminal does not matter.
To Reproduce
Run the program under Source Code. It presses alt+d in three inputs, each with the cursor on b (position 1): inside a word, on the last rune of a word, and on the last rune of the value.
Source Code
package main
import (
"fmt"
"charm.land/bubbles/v2/textinput"
tea "charm.land/bubbletea/v2"
)
func altD(value string, cursor int) string {
m := textinput.New()
m.Focus()
m.SetValue(value)
m.SetCursor(cursor)
m, _ = m.Update(tea.KeyPressMsg{Code: 'd', Mod: tea.ModAlt})
return m.Value()
}
func main() {
fmt.Printf("%q\n", altD("abc d", 1)) // cursor on b, inside the word
fmt.Printf("%q\n", altD("ab c", 1)) // cursor on b, the last rune of the word
fmt.Printf("%q\n", altD("ab", 1)) // cursor on b, the last rune of the value
}Output:
"a d"
"a"
panic: runtime error: index out of range [2] with length 2
goroutine 1 [running]:
charm.land/bubbles/v2/textinput.(*Model).deleteWordForward(...)
charm.land/bubbles/[email protected]/textinput/textinput.go:475 +0x2c8
charm.land/bubbles/v2/textinput.Model.Update(...)
charm.land/bubbles/[email protected]/textinput/textinput.go:640 +0xd18
main.altD(...)
main.go:15 +0x11c
main.main()
main.go:22 +0xd4
exit status 2Expected behavior
"a d", "a c" and "a": in each case the rune under the cursor and the rest of its word are deleted and nothing else, and no key press panics. The second case deletes b c instead of b; the third panics.
Additional context
Tested on v2.2.1 and on current main (0a69b19b); the output is identical on both.
BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 96 other bugs in bubbles. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/bubbles. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.
Source: charmbracelet/bubbles