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

Error with lazy loading via cmd options #316

Closed
garymh opened this issue Apr 23, 2021 · 12 comments
Closed

Error with lazy loading via cmd options #316

garymh opened this issue Apr 23, 2021 · 12 comments

Comments

@garymh
Copy link

garymh commented Apr 23, 2021

This still seems to be happening to me with the latest Packer 😞

While running :Switch, which should be lazy loaded, I get:

Error in packer_compiled: ...vim/site/pack/packer/opt/packer.nvim/lua/packer/load.lua:10: attempt to index local 'plugin' (a nil value)
Please check your config for correctness

Here is my packer loading code:

local packer = nil

local function init()
    if packer == nil then
        packer = require('packer')
        util = require("packer/util")
        packer.init({
            compile_path = util.join_paths(vim.fn.stdpath("config"),
                                           "vimscript", "packer_compiled.vim"),
            max_jobs = 10
        })
    end

    local use = packer.use
    packer.reset()

    use {"wbthomason/packer.nvim", opt = true}

    use {
        "AndrewRadev/switch.vim",
        cmd = {"Switch", "SwitchReverse"},
        config = function()
            vim.g.switch_custom_definitions =
                {
                    {"assert", "refute"}, {"if", "unless"}, {"[ ]", "[x]"},
                    {"yes", "no"}, {"let g:", "vim.g."}
                }
        end
    }
end

local plugins = setmetatable({}, {
    __index = function(_, key)
        init()
        return packer[key]
    end
})

return plugins

Originally posted by @garymh in #180 (comment)

@garymh
Copy link
Author

garymh commented Apr 23, 2021

init.lua for reference as well:

require 'plugins'

vim.api.nvim_command [[ augroup auto_compile ]]
vim.api.nvim_command [[ autocmd! ]]
vim.api.nvim_command [[ autocmd BufWritePost plugins.lua PackerCompile ]]
vim.api.nvim_command [[ augroup END ]]
vim.cmd [[source $HOME/.config/nvim/vimscript/packer_compiled.vim]]

@wbthomason
Copy link
Owner

@garymh Thanks for the report! I'm having trouble reproducing this - using your switch specification, the :Switch command works as expected. Can you please post your compiled file?

@garymh
Copy link
Author

garymh commented Apr 26, 2021

@wbthomason Thanks for reading it! 😄

🤔 Hmm. That's odd! It must be something screwy with my setup. Here's the compiled file:

" Automatically generated packer.nvim plugin loader code

if !has('nvim-0.5')
  echohl WarningMsg
  echom "Invalid Neovim version for packer.nvim!"
  echohl None
  finish
endif

packadd packer.nvim

try

lua << END
  local time
  local profile_info
  local should_profile = false
  if should_profile then
    local hrtime = vim.loop.hrtime
    profile_info = {}
    time = function(chunk, start)
      if start then
        profile_info[chunk] = hrtime()
      else
        profile_info[chunk] = (hrtime() - profile_info[chunk]) / 1e6
      end
    end
  else
    time = function(chunk, start) end
  end
  
local function save_profiles(threshold)
  local sorted_times = {}
  for chunk_name, time_taken in pairs(profile_info) do
    sorted_times[#sorted_times + 1] = {chunk_name, time_taken}
  end
  table.sort(sorted_times, function(a, b) return a[2] > b[2] end)
  local results = {}
  for i, elem in ipairs(sorted_times) do
    if not threshold or threshold and elem[2] > threshold then
      results[i] = elem[1] .. ' took ' .. elem[2] .. 'ms'
    end
  end

  _G._packer = _G._packer or {}
  _G._packer.profile_output = results
end

time("Luarocks path setup", true)
local package_path_str = "/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua"
local install_cpath_pattern = "/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so"
if not string.find(package.path, package_path_str, 1, true) then
  package.path = package.path .. ';' .. package_path_str
end

if not string.find(package.cpath, install_cpath_pattern, 1, true) then
  package.cpath = package.cpath .. ';' .. install_cpath_pattern
end

time("Luarocks path setup", false)
time("try_loadstring definition", true)
local function try_loadstring(s, component, name)
  local success, result = pcall(loadstring(s))
  if not success then
    print('Error running ' .. component .. ' for ' .. name)
    error(result)
  end
  return result
end

time("try_loadstring definition", false)
time("Defining packer_plugins", true)
_G.packer_plugins = {
  ["packer.nvim"] = {
    loaded = false,
    needs_bufread = false,
    path = "/Users/gary/.local/share/nvim/site/pack/packer/opt/packer.nvim"
  },
  ["switch.vim"] = {
    commands = { "Switch", "SwitchReverse" },
    config = { "\27LJ\2\n§\2\0\0\3\0\n\0\0196\0\0\0009\0\1\0004\1\b\0005\2\3\0>\2\1\0015\2\4\0>\2\2\0015\2\5\0>\2\3\0015\2\6\0>\2\4\0015\2\a\0>\2\5\0015\2\b\0>\2\6\0015\2\t\0>\2\a\1=\1\2\0K\0\1\0\1\2\0\0005expect(\\([^. ]\\+\\))\\.to\\(_not\\|\\)': '\\1.should\\2\1\2\0\0006\\([^. ]\\+\\)\\.should\\(_not\\|\\)': 'expect(\\1)\\.to\\2\1\3\0\0\vlet g:\vvim.g.\1\3\0\0\byes\ano\1\3\0\0\b[ ]\b[x]\1\3\0\0\aif\vunless\1\3\0\0\vassert\vrefute\30switch_custom_definitions\6g\bvim\0" },
    loaded = false,
    needs_bufread = true,
    path = "/Users/gary/.local/share/nvim/site/pack/packer/opt/switch.vim"
  }
}

time("Defining packer_plugins", false)

-- Command lazy-loads
time("Defining lazy-load commands", true)
vim.cmd [[command! -nargs=* -range -bang -complete=file Switch lua require("packer.load")({'switch.vim'}, { cmd = "Switch", l1 = <line1>, l2 = <line2>, bang = <q-bang>, args = <q-args> }, _G.packer_plugins)]]
vim.cmd [[command! -nargs=* -range -bang -complete=file SwitchReverse lua require("packer.load")({'switch.vim'}, { cmd = "SwitchReverse", l1 = <line1>, l2 = <line2>, bang = <q-bang>, args = <q-args> }, _G.packer_plugins)]]
time("Defining lazy-load commands", false)

if should_profile then save_profiles() end

END

catch
  echohl ErrorMsg
  echom "Error in packer_compiled: " .. v:exception
  echom "Please check your config for correctness"
  echohl None
endtry

``

@garymh
Copy link
Author

garymh commented Apr 26, 2021

If it helps any, here it is without the lazy loaded commands:

" Automatically generated packer.nvim plugin loader code

if !has('nvim-0.5')
  echohl WarningMsg
  echom "Invalid Neovim version for packer.nvim!"
  echohl None
  finish
endif

packadd packer.nvim

try

lua << END
  local time
  local profile_info
  local should_profile = false
  if should_profile then
    local hrtime = vim.loop.hrtime
    profile_info = {}
    time = function(chunk, start)
      if start then
        profile_info[chunk] = hrtime()
      else
        profile_info[chunk] = (hrtime() - profile_info[chunk]) / 1e6
      end
    end
  else
    time = function(chunk, start) end
  end
  
local function save_profiles(threshold)
  local sorted_times = {}
  for chunk_name, time_taken in pairs(profile_info) do
    sorted_times[#sorted_times + 1] = {chunk_name, time_taken}
  end
  table.sort(sorted_times, function(a, b) return a[2] > b[2] end)
  local results = {}
  for i, elem in ipairs(sorted_times) do
    if not threshold or threshold and elem[2] > threshold then
      results[i] = elem[1] .. ' took ' .. elem[2] .. 'ms'
    end
  end

  _G._packer = _G._packer or {}
  _G._packer.profile_output = results
end

time("Luarocks path setup", true)
local package_path_str = "/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua"
local install_cpath_pattern = "/Users/gary/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so"
if not string.find(package.path, package_path_str, 1, true) then
  package.path = package.path .. ';' .. package_path_str
end

if not string.find(package.cpath, install_cpath_pattern, 1, true) then
  package.cpath = package.cpath .. ';' .. install_cpath_pattern
end

time("Luarocks path setup", false)
time("try_loadstring definition", true)
local function try_loadstring(s, component, name)
  local success, result = pcall(loadstring(s))
  if not success then
    print('Error running ' .. component .. ' for ' .. name)
    error(result)
  end
  return result
end

time("try_loadstring definition", false)
time("Defining packer_plugins", true)
_G.packer_plugins = {
  ["packer.nvim"] = {
    loaded = false,
    needs_bufread = false,
    path = "/Users/gary/.local/share/nvim/site/pack/packer/opt/packer.nvim"
  },
  ["switch.vim"] = {
    config = { "\27LJ\2\n§\2\0\0\3\0\n\0\0196\0\0\0009\0\1\0004\1\b\0005\2\3\0>\2\1\0015\2\4\0>\2\2\0015\2\5\0>\2\3\0015\2\6\0>\2\4\0015\2\a\0>\2\5\0015\2\b\0>\2\6\0015\2\t\0>\2\a\1=\1\2\0K\0\1\0\1\2\0\0005expect(\\([^. ]\\+\\))\\.to\\(_not\\|\\)': '\\1.should\\2\1\2\0\0006\\([^. ]\\+\\)\\.should\\(_not\\|\\)': 'expect(\\1)\\.to\\2\1\3\0\0\vlet g:\vvim.g.\1\3\0\0\byes\ano\1\3\0\0\b[ ]\b[x]\1\3\0\0\aif\vunless\1\3\0\0\vassert\vrefute\30switch_custom_definitions\6g\bvim\0" },
    loaded = true,
    path = "/Users/gary/.local/share/nvim/site/pack/packer/start/switch.vim"
  }
}

time("Defining packer_plugins", false)
-- Config for: switch.vim
time("Config for switch.vim", true)
try_loadstring("\27LJ\2\n§\2\0\0\3\0\n\0\0196\0\0\0009\0\1\0004\1\b\0005\2\3\0>\2\1\0015\2\4\0>\2\2\0015\2\5\0>\2\3\0015\2\6\0>\2\4\0015\2\a\0>\2\5\0015\2\b\0>\2\6\0015\2\t\0>\2\a\1=\1\2\0K\0\1\0\1\2\0\0005expect(\\([^. ]\\+\\))\\.to\\(_not\\|\\)': '\\1.should\\2\1\2\0\0006\\([^. ]\\+\\)\\.should\\(_not\\|\\)': 'expect(\\1)\\.to\\2\1\3\0\0\vlet g:\vvim.g.\1\3\0\0\byes\ano\1\3\0\0\b[ ]\b[x]\1\3\0\0\aif\vunless\1\3\0\0\vassert\vrefute\30switch_custom_definitions\6g\bvim\0", "config", "switch.vim")
time("Config for switch.vim", false)
if should_profile then save_profiles() end

END

catch
  echohl ErrorMsg
  echom "Error in packer_compiled: " .. v:exception
  echom "Please check your config for correctness"
  echohl None
endtry

@wbthomason
Copy link
Owner

Thanks! Just to confirm, the non-lazy-loaded version does not throw an error?

@garymh
Copy link
Author

garymh commented Apr 26, 2021

Correct!

@wbthomason
Copy link
Owner

Hmm. I'm still unable to reproduce this. Your compiled files look fine too...

I've pushed a change (in 87980fc) that might help with debugging, if you can give a try. From your error message, it looks like the plugin name we try to load is not present in the table of plugins (just to check, you don't modify _G.packer_plugins anywhere in your config, right?), so the change will check that an attempted load is present and print its name/exit with an error if it is not.

@garymh
Copy link
Author

garymh commented Apr 27, 2021

Sure! I got:

Error: attempted to load switch.vim which is not present in plugins table!
Error in packer_compiled: ...vim/site/pack/packer/opt/packer.nvim/lua/packer/load.lua:14: Error: attempte
d to load switch.vim which is not present in plugins table!
Please check your config for correctness

I haven't modified _G.packer_plugins at all. I don't think there are any other files being loaded besides some tiny random syntax ones (that just adjust vim options)

@garymh
Copy link
Author

garymh commented Apr 27, 2021

AH! I just realized what it was... I actually had a plugin/packer_compiled.vim file in my config folder in addition to the one I was manually loading. Removed the first file and it works fine! 🤦

@garymh
Copy link
Author

garymh commented Apr 27, 2021

It might be handy for debugging to put a check if it tries to load packer twice, though!

@garymh garymh closed this as completed Apr 27, 2021
@garymh
Copy link
Author

garymh commented Apr 27, 2021

Thanks again for the support, though! 😄

@wbthomason
Copy link
Owner

Ah, whoops. That'd do it! Thanks! Unfortunately naively detecting if loading packer twice is desired or not is tricky; the user may re-source their compiled file during normal use. However, we should probably add a check to make sure that it's the same file path.

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

2 participants