Skip to content
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

[bug]: There are cases where the error 'Invalid window id 1001' occurs when restoring a saved session. #430

Open
1 task done
tadashi-aikawa opened this issue Jan 1, 2025 · 3 comments · May be fixed by #431
Open
1 task done
Assignees
Labels
bug Something isn't working in progress When an issue is being working on

Comments

@tadashi-aikawa
Copy link

Description

In certain use cases, such as restoring a session, an error seems to occur when the state of the saved session indicates that a scratch window created by no-neck-pain was visually absent.

image

From the source code, I confirmed that the issue can be avoided with the following fix.

local bufnr = vim.api.nvim_win_get_buf(win)

    -- my workaround
    if not vim.api.nvim_win_is_valid(win) then
        return
    end

    local bufnr = vim.api.nvim_win_get_buf(win)

However, I am not sure if this is the proper solution. If this approach is appropriate and a PR is needed, please feel free to let me know.

Thank you for maintaining such an excellent plugin!

Neovim version

0.10.x

Steps to reproduce

# Create a sandbox environment
mkdir -p ~/.config/no-neck-pain
cat > ~/.config/no-neck-pain/init.lua << 'EOF'
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
  local out = vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  {
    "shortcuts/no-neck-pain.nvim",
    lazy = false,
    opts = {
      width = 140,
      autocmds = {
        enableOnVimEnter = true,
        enableOnTabEnter = true,
      },
      buffers = {
        colors = { background = "tokyonight-moon" },
      },
    },
  }
})
EOF
alias svim="NVIM_APPNAME=no-neck-pain nvim"

# Install lazy.nvim & no-neck-pain
svim
:q

# Create a sample file
echo "aaa" > aaa.txt

# Open files and make a session
svim
:e aaa.txt
:vnew aaa.txt
:mksession! mk.vim
:qa!

# Restore session
svim
:source mk.vim

Relevant log output

Error executing vim.schedule lua callback: .../nvim/lazy/no-neck-pain.nvim/lua/no-neck-pain/colors.lua:133: Invalid window id: 1001
stack traceback:
	[C]: in function 'nvim_win_get_buf'
	.../nvim/lazy/no-neck-pain.nvim/lua/no-neck-pain/colors.lua:133: in function 'init'
	...hare/nvim/lazy/no-neck-pain.nvim/lua/no-neck-pain/ui.lua:163: in function 'create_side_buffers'
	...re/nvim/lazy/no-neck-pain.nvim/lua/no-neck-pain/main.lua:108: in function 'callback'
	...vim/lazy/no-neck-pain.nvim/lua/no-neck-pain/util/api.lua:115: in function <...vim/lazy/no-neck-pain.nvim/lua/no-neck-pain/util/api.lua:113>

Self-service

  • I'd be willing to fix this bug myself.
@tadashi-aikawa tadashi-aikawa added the bug Something isn't working label Jan 1, 2025
@shortcuts
Copy link
Owner

shortcuts commented Jan 1, 2025

Hey, thanks for using the plugin and reporting the issue!!!

From the source code, I confirmed that the issue can be avoided with the following fix.

Indeed I think most nvim_win_get_buf or similar calls should be preceded by a valid check.

For that specific issue, is it enough to make the plugin work properly after your addition? I'd assume we should create a new window because the follow up logic might try to target a wrong window too

Feel free to go ahead with the implementation! Would be great if you could add a test case to tests/test_colors.lua as well, but if you can't I can add it to your pr

@shortcuts shortcuts added the in progress When an issue is being working on label Jan 1, 2025
@tadashi-aikawa
Copy link
Author

@shortcuts
Thank you for your reply!

For that specific issue, is it enough to make the plugin work properly after your addition? I'd assume we should create a new window because the follow

Unfortunately, I found that the workaround I suggested earlier results in an error under certain conditions.

Upon reviewing the code again, I realized it might be better to address the root cause, which seems to be that the first argument of colors.init is the invalid window id. Specifically, I moved this call inside the preceding if-then block.

colors.init(state.get_side_id(state, side), side)

            if
                wins[side].padding > _G.NoNeckPain.config.minSideBufferWidth
                and not state.is_side_enabled_and_valid(state, side)
            then
                vim.cmd(wins[side].cmd)

                state.set_side_id(state, vim.api.nvim_get_current_win(), side)

                if _G.NoNeckPain.config.buffers.set_names then
                    local exist = vim.fn.bufnr("no-neck-pain-" .. side)

                    if exist ~= -1 then
                        vim.api.nvim_buf_delete(exist, { force = true })
                    end

                    vim.api.nvim_buf_set_name(0, "no-neck-pain-" .. side)
                end

                if _G.NoNeckPain.config.buffers[side].scratchPad.enabled then
                    state.set_scratchPad(state, true)
                    ui.init_scratchPad(side, state.get_side_id(state, side))
                else
                    ui.init_side_options(side, state.get_side_id(state, side))
                end
+
+             colors.init(state.get_side_id(state, side), side)
            end

-           colors.init(state.get_side_id(state, side), side)

If state.is_side_enabled_and_valid is true, colors.init will not be called, but in such cases, it assumes that the highlight settings have already been applied.

After testing for a few minutes, no errors have occurred. I’ll continue using it for a while, and if it works without issues, I’ll create a PR. If you have any concerns or suggestions regarding this approach, please let me know 🙏

tadashi-aikawa added a commit to tadashi-aikawa/no-neck-pain.nvim that referenced this issue Jan 5, 2025
@tadashi-aikawa
Copy link
Author

@shortcuts
I created a pull request #431 as the development has been going well over the past few days. I appreciate your review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in progress When an issue is being working on
Projects
None yet
2 participants