Skip to content

bug: E5108 "Invalid buffer" whe toggle the render #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
sadtab opened this issue Dec 13, 2024 · 5 comments
Closed
2 tasks done

bug: E5108 "Invalid buffer" whe toggle the render #260

sadtab opened this issue Dec 13, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@sadtab
Copy link

sadtab commented Dec 13, 2024

Neovim version (nvim -v)

0.10.2

Neovim distribution

My own

Operating system

Ubuntu

Terminal emulator / GUI

Alacritty

Describe the bug

When several markdown files are opened and some of them are modified with or without writing , toggling the renderer can cause this error log :

   Error  04:11:20 PM msg_show.emsg E5108: Error executing lua: ...y/render-markdown.nvim/lua/render-markdown/core/util.lua:131: Invalid buffer id: 4
stack traceback:
	[C]: in function 'nvim_buf_get_name'
	...y/render-markdown.nvim/lua/render-markdown/core/util.lua:131: in function 'file_name'
	...zy/render-markdown.nvim/lua/render-markdown/core/log.lua:61: in function 'buf'
	...azy/render-markdown.nvim/lua/render-markdown/core/ui.lua:79: in function 'update'
	...azy/render-markdown.nvim/lua/render-markdown/manager.lua:47: in function 'set_all'
	...im/lazy/render-markdown.nvim/lua/render-markdown/api.lua:17: in function 'toggle'
	/home/sadegh/.config/nvim/lua/helpers.lua:47: in function </home/sadegh/.config/nvim/lua/helpers.lua:44>

I looked a little in the source code and it seems the autocmd at

vim.api.nvim_create_autocmd('FileType', {

is registered in all files and if somehow there is a temporary buffer that comes into existence and is wiped out right after (maybe another plugin is doing it) is queued in the plugin internals

I changed autocmd to

    vim.api.nvim_create_autocmd('FileType', {
        pattern = { 'markdown' },
        group = M.group,
        callback = function(args)
            M.attach(args.buf)
        end,
    })

And it seems to be working, I see this autocmd is intentionally registered for all the files but maybe there is a better way of doing it

Expected behavior

no invalid buffer error

Healthcheck output

render-markdown: require("render-markdown.health").check()

render-markdown.nvim [version] ~
- OK plugin 7.7.3
- OK neovim >= 0.10

render-markdown.nvim [configuration] ~
- OK valid

render-markdown.nvim [nvim-treesitter] ~
- OK installed
- OK markdown: parser installed
- OK markdown: highlight enabled
- OK markdown_inline: parser installed
- OK markdown_inline: highlight enabled

render-markdown.nvim [executables] ~
- OK none to check

render-markdown.nvim [conflicts] ~
- OK headlines: not installed
- OK obsidian: not installed

Plugin configuration

{
    "MeanderingProgrammer/render-markdown.nvim",
    dependencies = { "nvim-treesitter/nvim-treesitter", "echasnovski/mini.nvim" },
    opts = {
      file_types = { "markdown", "noice" },
      -- enabled = false, -- Disable by default
      code = {
        sign = false,
        width = "block",
        -- right_pad = 1,
        style = "full",
        language_pad = 0,
        language_name = true,
        position = "right",
      },
      dash = {
        -- Turn on / off thematic break rendering
        enabled = true,
        icon = "󰮸 ",
        width = 40, -- textwidth/2 because of 2 characters as icon
        highlight = "RenderMarkdownDash",
      },
      heading = {
        enabled = true,
        width = "block",
        sign = false,
        icons = {
          -- "󰎤  ",
          -- " 󰎩  ",
          -- "  󰎪  ",
          -- "   󰎮  ",
          -- "    󰎱  ",
          -- "      󰎵  ",
          "",
          "",
          "",
          "",
          "",
          "",
        },
        -- left_margin = 2,
      },
      bullet = {
        -- Turn on / off list bullet rendering
        enabled = true,
        right_pad = 1,
      },
      latex = {
        enabled = false,
        -- Executable used to convert latex formula to rendered unicode
        converter = "latex2text",
        -- Highlight for LaTeX blocks
        highlight = "RenderMarkdownMath",
        -- Amount of empty lines above LaTeX blocks
        top_pad = 0,
        -- Amount of empty lines below LaTeX blocks
        bottom_pad = 0,
      },
      win_options = {
        conceallevel = {
          default = 0,
          rendered = 3,
        },
      },
      render_modes = { "n", "c", "t", "i" },
    },
    ft = { "markdown", "norg", "rmd", "org", "noice" },
  },

Plugin error log

 Error 04:11:20 PM msg_show.emsg E5108: Error executing lua: ...y/render-markdown.nvim/lua/render-markdown/core/util.lua:131: Invalid buffer id: 4
stack traceback:
[C]: in function 'nvim_buf_get_name'
...y/render-markdown.nvim/lua/render-markdown/core/util.lua:131: in function 'file_name'
...zy/render-markdown.nvim/lua/render-markdown/core/log.lua:61: in function 'buf'
...azy/render-markdown.nvim/lua/render-markdown/core/ui.lua:79: in function 'update'
...azy/render-markdown.nvim/lua/render-markdown/manager.lua:47: in function 'set_all'
...im/lazy/render-markdown.nvim/lua/render-markdown/api.lua:17: in function 'toggle'
/home/sadegh/.config/nvim/lua/helpers.lua:47: in function </home/sadegh/.config/nvim/lua/helpers.lua:44>

Confirmations

  • I have updated this plugin to the latest version using my plugin manager
  • I have provided the text contained in all screenshots as raw text in this issue. This means if there is a screenshot below it is the copy pasted contents of the file in the screenshot. I understand that my issue will be closed if I have not.

Additional information

No response

@sadtab sadtab added the bug Something isn't working label Dec 13, 2024
@sadtab
Copy link
Author

sadtab commented Dec 13, 2024

The problem still exists even with my modification, it is way less frequent but I particularly see it in DiffView ing the MD files.

Note

In DiffView ing, the ft is still markdown

@sadtab
Copy link
Author

sadtab commented Dec 13, 2024

At some point, we need to add a if vim.api.nvim_buf_is_valid(buf) then in the logics

@sadtab
Copy link
Author

sadtab commented Dec 13, 2024

Experimenting with :

function M.set_all(enabled)
    -- Attempt to attach current buffer in case this is from a lazy load
    M.attach(util.current('buf'))
    state.enabled = enabled
    for _, buf in ipairs(buffers) do
        if vim.api.nvim_buf_is_valid(buf) then
            ui.update(buf, vim.fn.bufwinid(buf), 'UserCommand', true)
        end
    end
end

at

-- Attempt to attach current buffer in case this is from a lazy load

seems promising, it wont render DiffVeiw MD files at all though

MeanderingProgrammer added a commit that referenced this issue Dec 13, 2024
## Details

Issue: #260

When logging info at the buffer level we get the file name for the
buffer to make the logs more useful. However if the buffer is invalid
this call to get file name will throw an exception.

Wrap the call to get file name in a check that buffer is valid. Return
some default string if it is not. Other parts of the ui.update flow will
validate the buffer and window and prevent any actual changes to invalid
buffers.
@MeanderingProgrammer
Copy link
Owner

MeanderingProgrammer commented Dec 13, 2024

Thanks for letting me know!

This should resolve it: 0022a57

Error is getting triggered on this line here: https://github.com/MeanderingProgrammer/render-markdown.nvim/blob/main/lua/render-markdown/core/ui.lua#L79, since logging things at the buffer level calls file_name in the logger.

Once that no longer causes a problem the very next line: https://github.com/MeanderingProgrammer/render-markdown.nvim/blob/main/lua/render-markdown/core/ui.lua#L80, checks that the buffer and window are both valid and does an early return if either is not.

@sadtab
Copy link
Author

sadtab commented Dec 14, 2024

You're awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants