nvim-neorocks / lz.n

πŸ¦₯ A dead simple lazy-loading Lua library for Neovim plugins.
GNU General Public License v2.0
102 stars 7 forks source link

error when applying colorscheme in `after` hook on `event` trigger #81

Closed jla2000 closed 2 months ago

jla2000 commented 2 months ago

I'm working on a plugin that persists colorschemes, and I found the following issue:

require("lz.n").load({
    {
        "onedark.nvim",
        colorscheme = "onedark",
    },
    {
        "xyz",
        event = "UIEnter",
        after = function()
            vim.cmd.colorscheme("onedark")
        end,
    },
})

This code does not work in this combination:

03:02:38 PM msg_show Failed to run 'after' hook for xyz: /home/jlafferton/.config/nvim/lua/plugins/colorschemes.lua:70: Vim:E5113: Error while calling lua chunk: ...vrz-vimplugin-onedark.nvim-2024-07-05/colors/onedark.lua:5: module 'onedark' not found:
^Ino field package.preload['onedark']
^Ino file '/nix/store/s2law12lgav6zqbmfz13jmqncdadmb23-lua5.1-plenary.nvim-scm-1/share/lua/5.1/onedark.lua'
^Ino file '/nix/store/s2law12lgav6zqbmfz13jmqncdadmb23-lua5.1-plenary.nvim-scm-1/share/lua/5.1/onedark/init.lua'
^Ino file '/nix/store/nq4y3hxm0g3vj8jzcc63f373gkfb2xp4-lua5.1-luassert-1.9.0-1/share/lua/5.1/onedark.lua'
^Ino file '/nix/store/nq4y3hxm0g3vj8jzcc63f373gkfb2xp4-lua5.1-luassert-1.9.0-1/share/lua/5.1/onedark/init.lua'
^Ino file '/nix/store/rq9m7zbz40j5p9inggy0786llc3ka27y-lua5.1-say-1.4.1-3/share/lua/5.1/onedark.lua'
^Ino file '/nix/store/rq9m7zbz40j5p9inggy0786llc3ka27y-lua5.1-say-1.4.1-3/share/lua/5.1/onedark/init.lua'
^Ino file '/nix/store/74x3q8qvr31hbdbk7zxckfvyd4q92d79-lua5.1-jsregexp-0.0.7-1/share/lua/5.1/onedark.lua'
^Ino file '/nix/store/xq88fv22ld2dnwzrfd79ji800vp2z065-luajit-2.1.1713773202-env/share/lua/5.1/onedark.lua'
^Ino file '/nix/store/xq88fv22ld2dnwzrfd79ji800vp2z065-luajit-2.1.1713773202-env/share/lua/5.1/onedark/init.lua'
^Ino file '/nix/store/74x3q8qvr31hbdbk7zxckfvyd4q92d79-lua5.1-jsregexp-0.0.7-1/lib/lua/5.1/onedark.so'
^Ino file '/nix/store/xq88fv22ld2dnwzrfd79ji800vp2z065-luajit-2.1.1713773202-env/lib/lua/5.1/onedark.so'
stack traceback:
^I[C]: in function 'require'
^I...vrz-vimplugin-onedark.nvim-2024-07-05/colors/onedark.lua:5: in main chunk
^I[C]: in function 'colorscheme'
^I/home/jlafferton/.config/nvim/lua/plugins/colorschemes.lua:70: in function </home/jlafferton/.config/nvim/lua/plugins/colorschemes.lua:69>
^I[C]: in function 'xpcall'
^I...ack/myNeovimPackages/start/lz-n.nvim/lua/lz/n/loader.lua:88: in function 'hook'
^I...ack/myNeovimPackages/start/lz-n.nvim/lua/lz/n/loader.lua:125: in function 'load'
^I...eovimPackages/start/lz-n.nvim/lua/lz/n/handler/event.lua:162: in function <...eovimPackages/start/lz-n.nvim/lua/lz/n/handler/event.lua:154>

Somehow it's not possible to load the colorscheme inside the after function, or also if the plugin xyz e.g. tries to load it in its own code.

When just calling vim.cmd.colorschem("onedark") without the xyz plugin, it will work correctly.

mrcjkb commented 2 months ago

hey πŸ‘‹

thanks for reporting. I'm travelling right now, but will have a look when I'm near my laptop again.

mrcjkb commented 2 months ago

Hey again.

I just tried to reproduce it (using rocks.nvim and onedarkpro.nvim, because onedark.nvim wasn't on luarocks yet) and was not able to reproduce the error with your snippet. It called the after script and set the colorscheme.

It looks like you're using Nix. Could you please provide a minimal reproducible config? Ideally, with a flake that I can build from.

jla2000 commented 2 months ago

Hey, thanks for your time :) I will try to set something up this weekend

jla2000 commented 2 months ago

Put the following into a default.nix:

{ pkgs ? import <nixpkgs> { } }:

pkgs.neovim.override {
  configure = {
    customRC = /* lua */ ''
      lua << EOF
      require("lz.n").load({
        {
          "onedark.nvim",
          colorscheme = "onedark",
        },
        {
          "xyz",
          event = "UIEnter",
          after = function()
            vim.cmd.colorscheme("onedark")
          end,
        }
      })
      EOF
    '';
    packages.all = with pkgs.vimPlugins; {
      start = [ lz-n ];
      opt = [ onedark-nvim ];
    };
  };
}

And then:

$ nix-build
$ ./result/bin/nvim

When just not having the "xyz" entry here, everything works as expected. But within the after script of this entry it will break

mrcjkb commented 2 months ago

lz.n relies on :h ColorSchemePre autocommands to lazy-load plugins on colorscheme events.

To debug your setup, I've created the follwing init.lua

local test = false
vim.api.nvim_create_autocmd("ColorSchemePre", {
    callback = function()
        test = true
    end,
})
vim.api.nvim_create_autocmd("UIEnter", {
    callback = function()
        pcall(vim.cmd.colorscheme, "onedark")
        if not test then
            error("ColorSchemePre was not invoked")
        end
    end,
})

And ran it with nvim -u NORC -u init.lua.

It fails, indicating that Neovim did not invoke a ColorSchemePre event when calling vim.cmd.colorscheme. I don't know if this is a Neovim bug or intended behaviour, but I will open a Neovim issue.

mrcjkb commented 2 months ago
mrcjkb commented 2 months ago

Closing, as there is nothing I can do to work around that in lz.n.

mrcjkb commented 2 months ago

Reopening (see comment on the Neovim issue). Will have to make sure nested=true doesn't break anything and add a test cases (when I'm near my laptop).

jla2000 commented 2 months ago

Thanks for taking the time and fixing it πŸ‘