bubbletea icon indicating copy to clipboard operation
bubbletea copied to clipboard

could not decode rune

Open muesli opened this issue 2 years ago • 5 comments

Discussed in https://github.com/charmbracelet/bubbletea/discussions/664

Originally posted by mschneider82 December 5, 2022 i am using the mouse input and always getting a program exit after a while because of "could not decode rune"

I would expect it continues instead of failing on a invalid utf 8 rune.

		r, width := utf8.DecodeRune(b[i:])
		if r == utf8.RuneError {
			return nil, errors.New("could not decode rune")
		}
```</div>

muesli avatar Feb 16 '23 11:02 muesli

Note: the reason we return an error here, is that this is a bit of a panic situation, as all further decodes would likely fail to.

On the bright side, I believe this is already fixed by @knz and I will soon get to merge that fix.

muesli avatar Feb 16 '23 11:02 muesli

Hi,

I encountered the same problem, here is a minimal reproduction example:

package main

import (
	"log"

	tea "github.com/charmbracelet/bubbletea"
)

type model struct{}

func (model) Init() tea.Cmd {
	return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	return m, nil
}

func (model) View() string {
	return "hello world"
}

func main() {
	p := tea.NewProgram(model{})
	log.Fatal(p.Run())
}

When running the program, if you paste below text to the terminal, the program will exit with "could not decode rune" error:

我要你扮演一个侏儒。你会为我提供可以在任何地方进行的活动和爱好的有趣、独特的想法。例如,我可能会向您询问有趣的院子设计建议或在天气不佳时在室内消磨时间的创造性方法。此外,如有必要,您可以建议与我的要求相符的其他相关活动或项目。我的第一个请求是“我正在寻找我所在地区的新户外活动"

Actually this bug can be reproduced in any bubbletea program, if any Chinese text longer than 256 characters is pasted, the program will exit.

j178 avatar Mar 24 '23 07:03 j178

Yep, this is a known issue.

@muesli my PR to handle long inputs should help alleviate this.

knz avatar Mar 24 '23 08:03 knz

I encountered a similar problem where I first thought Bubblezone was causing the problem (https://github.com/lrstanley/bubblezone/issues/17). After further investigation it seems Bubbleteas mouse handling with tea.WithMouseAllMotion() is the culprit.

If tea.WithMouseAllMotion() is enabled the whole terminal freezes after a bit of mouse movement. Also when I minimize the console window and restore it, it consistently freezes. I tried this with iterm2 and the default macos terminal. The both won't output the could not decode rune and just be frozen and locked. I have to manually kill them. @lrstanley confirmed that Alacritty (via WSL) shows similar behaviour, but in contrast to iterm2 / macos terminal does output the could not decode rune error.

Reproducible example: https://github.com/lrstanley/bubblezone/issues/17#issuecomment-1502451940

@knz I tried to use your PR with the help of a replace in the go.mod

replace github.com/charmbracelet/bubbletea v0.23.2 => github.com/knz/bubbletea v0.0.0-20230107191255-7b1c10438d90

but even with your fix(key): support very long buffered input the program still freezes.

EDIT: https://github.com/charmbracelet/bubbletea/pull/594 seems to fix the problem 👀

BigJk avatar Apr 11 '23 05:04 BigJk

Since https://github.com/charmbracelet/bubbletea/pull/570 gets merged, I think this issue is resolved now.

j178 avatar Jan 02 '24 10:01 j178