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

Crash in highlight.lua at line 106 (attempt to perform arithmetic on local 'n' (nil value)) with certain color configurations #260

Open
Kerim-Willem opened this issue Jan 16, 2025 · 7 comments

Comments

@Kerim-Willem
Copy link

Kerim-Willem commented Jan 16, 2025

I’m encountering two persistent errors in Neovim/LazyVim whenever I press s, likely due to having a fully transparent background in my setup.

  1. First error:
    Right after I start Neovim, pressing s produces the following message:

E5108: Error executing lua: ...kerim/.local/share/nvim/lazy/leap.nvim/lua/leap/init.lua:5:
loop or previous error loading module 'leap.main'
stack traceback:
[C]: in function 'require'
...kerim/.local/share/nvim/lazy/leap.nvim/lua/leap/init.lua:5: in function '__index'
...s/kerim/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:36: in function <...s/kerim/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:36>
...

  1. Second error:
    Once the first error clears, pressing s again leads to:

E5108: Error executing lua: ...kerim/.local/share/nvim/lazy/leap.nvim/lua/leap/init.lua:5:
loop or previous error loading module 'leap.main'
stack traceback:
[C]: in function 'require'
...kerim/.local/share/nvim/lazy/leap.nvim/lua/leap/init.lua:5: in function '__index'
...s/kerim/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:36: in function <...s/kerim/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:36>
...

This second error repeats every time I press s.

It appears that blend(color1, color2, weight) expects color1 and color2 to be decimal numbers, but in my configuration, nvim_get_hl() sometimes returns a hex string (e.g., "#ffffff") or nil. That causes math.floor(n / 65536) to fail.

Proposed Fix:
In highlight.lua (around line 106), blend() calls a helper like __3ergb(n). If n is either nil or a hex string, the code crashes. A straightforward workaround is to check for these cases and convert them to a fallback numeric value. For example:

This worked for me!
`
local function blend(color1, color2, weight)
local function __3ergb(n)
if not n then
-- fallback to black
return 0, 0, 0
elseif type(n) == "string" then
-- parse something like "#ffffff" into a number
local hex = n:gsub("^#", "")
local num = tonumber(hex, 16) or 0
local r = math.floor(num / 65536)
local g = math.floor((num / 256) % 256)
local b = num % 256
return r, g, b
else
-- n is already a decimal
local r = math.floor(n / 65536)
local g = math.floor((n / 256) % 256)
local b = n % 256
return r, g, b
end
end
local r1, g1, b1 = __3ergb(color1)
local r2, g2, b2 = __3ergb(color2)

local r = ((r1 * (1 - weight)) + (r2 * weight))
local g = ((g1 * (1 - weight)) + (g2 * weight))
local b = ((b1 * (1 - weight)) + (b2 * weight))

return string.format("#%02x%02x%02x", r, g, b)

end
`

With this patch, Leap no longer crashes and works like expected. I’d be happy to open a PR if this approach seems acceptable. I hope this helps anyone that got stuck like I did!

@tomasmiguez
Copy link

Until this is fixed on master, it can be avoided by pinning the version to the commit before the bug was introduced, this on packer can be done as:

  use {
    'ggandor/leap.nvim',
    commit = '5ae080b646021bbb6e1d8715b155b1e633e28166',
    config = function() require('leap').create_default_mappings() end
  }

@searleser97
Copy link

Same issue here, it seems it is affecting everyone

@fredrdz
Copy link

fredrdz commented Jan 17, 2025

Same is occurring on my end. I'm doing also locking to a previous commit to bypass. I was using a transparent background as well.

@ggandor
Copy link
Owner

ggandor commented Jan 17, 2025

Thanks for reporting, sorry for the slow response. I specifically checked this before commiting, in my config nvim_get_hl() consistently returns a number, even if the group was defined using hex strings... (both on 0.9 and nightly). For now, I skipped dimming when not getting a numeric value.

@ggandor
Copy link
Owner

ggandor commented Jan 17, 2025

All of you are using transparent backgrounds?

@Kerim-Willem I guess we can parse the strings if necessary then, but I'd like to get to the bottom of this first :)

@ynhhoJ
Copy link

ynhhoJ commented Jan 17, 2025

I'm using transparent background too and after update 50045fa i got:

E5108: Error executing lua: .../.local/share/nvim/lazy/leap.nvim/lua/leap/highlight.lua:106: attempt to perform arithmetic on local 'n' (a nil value)                                                                                                                                       
stack traceback:                                                                                                                                                                                                                                                                            
        .../.local/share/nvim/lazy/leap.nvim/lua/leap/highlight.lua:106: in function '__3ergb'                                                                                                                                                                                              
        .../.local/share/nvim/lazy/leap.nvim/lua/leap/highlight.lua:112: in function 'blend'                                                                                                                                                                                                
        .../.local/share/nvim/lazy/leap.nvim/lua/leap/highlight.lua:157: in function 'init-highlight'                                                                                                                                                                                       
        .../o3h3/.local/share/nvim/lazy/leap.nvim/lua/leap/main.lua:837: in function 'init'                                                                                                                                                                                                 
        .../o3h3/.local/share/nvim/lazy/leap.nvim/lua/leap/main.lua:908: in main chunk                                                                                                                                                                                                      
        [C]: in function 'require'                                                                                                                                                                                                                                                          
        .../o3h3/.local/share/nvim/lazy/leap.nvim/lua/leap/init.lua:5: in function '__index'                                                                                                                                                                                                
        ...ox/o3h3/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:6: in function <...ox/o3h3/.local/share/nvim/lazy/leap.nvim/plugin/init.lua:6>

My theme config:

-- lazy.nvim
return {
  "navarasu/onedark.nvim",
  -- lazy = false,
  -- priority = 1000,
  config = function()
    require("onedark").setup {
      transparent = true,

      highlights = {
        TabLine = { bg = "none" },
        TabLineFill = { bg = "none" },
        FloatBorder = { bg = "none", fg = "#707880" },
        NormalFloat = { bg = "none", fg = "#707880" },

        Pmenu = { bg = "none" },

        StatusLine = { bg = "none" },
      },
    }

    vim.api.nvim_create_autocmd("Colorscheme", {
      group = vim.api.nvim_create_augroup("config_custom_highlights", {}),
      callback = function()
        -- Removes background from barbar
        vim.api.nvim_set_hl(0, "BufferCurrentADDED", { bg = "none", fg = "#7EA662" })
        vim.api.nvim_set_hl(0, "BufferCurrentCHANGED", { bg = "none", fg = "#4FA6ED" })
        vim.api.nvim_set_hl(0, "BufferCurrentDELETED", { bg = "none", fg = "#E55561" })
      end,
    })

    vim.cmd "colorscheme onedark"
  end,
}

ggandor added a commit that referenced this issue Jan 17, 2025
@ynhhoJ
Copy link

ynhhoJ commented Jan 17, 2025

67d26a1 fixed error for me

UPD: Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants