Packer.nvim: "if" in requires

Created on 9 Aug 2020  ยท  37Comments  ยท  Source: wbthomason/packer.nvim

Currently I have

  use {
    'nvim-lua/completion-nvim',
    opt = true,
    requires = {{'hrsh7th/vim-vsnip', opt = true}, {'hrsh7th/vim-vsnip-integ', opt = true}, {'ncm2/float-preview.nvim', opt = true}, {'aca/completion-tabnine', opt = true}}
  }

I want to require ncm2/float-preview.nvim only if a particular global variable g:xyz is not set. How would I do that?

All 37 comments

Also, i don't have to mark opt=true manually if I have ft={'xyz'} right?

I have this but it doesnt work (ncm2/float-preview.nvim gets added anyway, irrespective of g:gonvim_running)
(g:gonvim_running is either undefined or 1, depending on me using the goneovim GUI)

reqcomp = {{'hrsh7th/vim-vsnip'}, {'hrsh7th/vim-vsnip-integ'}, {'aca/completion-tabnine'}}
if( vim.g.gonvim_running ~= 1 ) then
  table.insert(reqcomp, {'ncm2/float-preview.nvim'})
end

local packages = {
>other plugins here<
  {'nvim-lua/completion-nvim',
    requires = reqcomp
  },
>other plugins here<
}
return packages

(See this for my setup)

Assuming that by "gets added anyway" you mean "is loaded", that's because you have float-preview.nvim specified as a start plugin, not an opt plugin (see the docs on the built-in packages feature in Neovim for the definitions of those kinds of plugins; basically, start plugins are always sourced at startup, whereas opt plugins must be manually loaded).

Because it uses native packages rather than the vim-plug method of appending to runtimepath, plugin specification with packer is a bit different than with vim-plug. In particular, naming a plugin means it gets installed; configuring loading happens by specifying something like opt, setup, ft, cond, etc. in its specification.

This is actually a great example use case for the cond configuration keyword. cond in general is used to define a test to determine if an opt plugin should be loaded. So, you could instead write the following:

use {
  'nvim-lua/completion-nvim',
  requires = {
    'hrsh7th/vim-vsnip', 'hrsh7th/vim-vsnip-integ', 'aca/completion-tabnine',
    {'ncm2/float-preview.nvim', cond = function() return vim.g.gonvim_running ~= 1 end}
  }
}

The important things to note here are:

  1. For dependencies in a requires table, you can use any form of plugin specification you can at the top level (i.e. you can just say 'aca/completion-tabnine' instead of needing {'aca/completion-tabnine'}, etc.)
  2. The cond keyword lets you define a (table of) no-argument functions and/or valid Lua code strings that will be evaluated to determine if the corresponding plugin should be loaded. cond implies opt.

You could use something more explicit/closer to what you originally had, but you'd need to (1) specify float-preview as opt and (2) in your conditional, use vim.cmd [[ packadd float-preview.nvim ]] to manually load the plugin. Personally, I think cond is easier/cleaner.

I hope this helps! Make sure you pull the latest packer before trying cond; I just pushed a few bugfixes related to that feature.

Thanks, I'll try this

{'ncm2/float-preview.nvim', cond = function() return vim.g.gonvim_running ~= 1 end}

This line gives E5108: Error executing lua .../nvim/pack/packer/opt/packer.nvim/lua/packer/compile.lua:216: bad argument #1 to 'ipairs' (table expected, got function)

Surrounding the RHS of cond with {} gives

Error detected while processing /home/chinmay/.config/nvim/plugin/packer_compiled.vim:
line  189:
E5107: Error loading lua [string ":lua"]:182: unexpected symbol near 'do'

Did you update packer.nvim like I mentioned? Those are the bugs I fixed. Try running :PackerUpdate (assuming you have packer managing itself; otherwise just do git pull in the packer repo clone dir).

Yep, still there

Have you rerun :PackerCompile since making sure packer was updated?

yes

ok, after updating and surround RHS of cond = with {}, it works

Nope doesnt, so sorry :P

Sorry, I can't reproduce this; using the latest packer.nvim master I get no errors with identical config.

Could you please tell me the specific commit of packer you're using, and send the contents of ~/.config/nvim/plugin/packer_load.vim?

specific commit of packer you're using

commit 64d717bcfd437466de8cf168882a118bad274c52 (HEAD -> master, origin/master, origin/HEAD)
Author: Wil Thomason <[email protected]>
Date:   Mon Aug 10 12:57:27 2020 -0400

    Ensure handle_checkouts result always has an output field; hopefully fix #25

send the contents of ~/.config/nvim/plugin/packer_load.vim

I dont have such a file, my setup is like this

If you don't have a file like packer_load.vim, then you haven't run PackerCompile (which you clearly have or you wouldn't be getting these errors). You can specify an alternate location for the compiled loaders file in your setup, but (from the link) it doesn't seem that you have.

I have ~/.config/nvim/plugin/packer_compiled.vim, contents below:

Oh, derp, my bad. I forgot that the default path is different now.

That file has an empty conditional loads section. Again, sorry for all the hassle, but could you please do the following:

  1. Put the specification for float-preview to the following:
{'ncm2/float-preview.nvim', cond = function() return vim.g.gonvim_running ~= 1 end }
  1. Exit and restart Neovim
  2. Run PackerCompile
  3. Exit and restart Neovim

After 4, if you still see errors or don't get the expected conditional loading, please send the updated packer_compiled.vim and your plugin specification.

You definitely shouldn't see errors; if the conditional load doesn't work, it's possible that gonvim_running isn't being set before packer_compiled is sourced; we could work around that if necessary.

packer_compiled: https://del.dog/lunarfotam, error E5108: Error executing lua .../nvim/pack/packer/opt/packer.nvim/lua/packer/compile.lua:216: bad argument #1 to 'ipairs' (table expected, got function)

pacakges.lua :

local packages = {
  {'wbthomason/packer.nvim'},
  {'Akin909/nvim-bufferline.lua', config = 'require [[bufferline]].setup()'},
  {'bakpakin/fennel.vim', ft = {'fennel'}, opt = true},
  {'camspiers/lens.vim', requires = {'camspiers/animate.vim'}},
  {'dense-analysis/ale'},
  {'dracula/vim', as = dracula},
  {'editorconfig/editorconfig-vim'},
  {'guns/vim-sexp', ft = {'clj', 'fnl'}, requires = {'tpope/vim-sexp-mappings-for-regular-people'}},
  {'hardcoreplayers/dashboard-nvim'},
  {'iamcco/markdown-preview.nvim', run = 'cd app && yarn install', cmd = 'MarkdownPreview', ft = {'md', 'mkdn', 'vim-plug'}},
  {'jeffkreeftmeijer/vim-numbertoggle'},
  {'jiangmiao/auto-pairs'},
  {'justinmk/vim-gtfo'},
  {'justinmk/vim-sneak'},
  {'kassio/neoterm', cmd = {'Tnew'}},
  {'kyazdani42/nvim-tree.lua', requires = {'kyazdani42/nvim-web-devicons'}},
  {'liuchengxu/vim-clap',
  --  config = 'vim.cmd[[Clap install-binary!]]'
  },
  {'liuchengxu/vista.vim'},
  {'mattn/vim-sonictemplate'},
  {'mhinz/vim-signify'},
  {'nvim-lua/completion-nvim',
    requires = {
        'hrsh7th/vim-vsnip', 'hrsh7th/vim-vsnip-integ', {'aca/completion-tabnine', run = './install.sh'},
        {'ncm2/float-preview.nvim', cond = function() return vim.g.gonvim_running ~= 1 end }
      }
  },
  {'neovim/nvim-lsp',
    requires = {'nvim-lua/lsp-status.nvim', 'nvim-lua/diagnostic-nvim'},
    config = {'require [[nvim_lsp]].bashls.setup{}', 'require [[nvim_lsp]].jdtls.setup{}', 'require [[nvim_lsp]].vimls.setup{}'}
  },
  {'norcalli/nvim-colorizer.lua', config={'require [[colorizer]].setup()'}},
  {'nvim-treesitter/nvim-treesitter', config='require [[treesitter_setup]]' },
  {'Olical/aniseed', config='require [[aniseed.dotfiles]]'},
  {'Olical/conjure', ft = {'fnl', 'clj'}},
  {'pbrisbin/vim-mkdir'},
  {'psliwka/vim-smoothie'},
  {'tpope/vim-fugitive'},
  {'tpope/vim-surround'},
  {'tyru/caw.vim'},
  {'reedes/vim-pencil', ft = {'txt', 'md', 'mkdn', 'rst'}},
  {'rhysd/git-messenger.vim'},
--TODO  'skywind3000/vim-quickui',
  {'vigoux/LanguageTool.nvim',
--TODO    ft = {},
  },
--  {'vim-airline/vim-airline' requires = {}},
  {'hardcoreplayers/spaceline.vim', requires = {'airblade/vim-gitgutter'}},
  {'Yggdroot/indentLine', requires = {'lukas-reineke/indent-blankline.nvim'}},
  {'ryanoasis/vim-devicons'},
}
return packages

packer.nvim 's git log -1 :

commit 6ec7d148cf827755f6d1fbd13b68e87c00d5ea95 (grafted, HEAD -> master, origin/master, origin/HEAD)
Author: Wil Thomason <[email protected]>
Date:   Mon Aug 10 14:02:03 2020 -0400

    Add plugin alias feature to close #28 (#31)

    * Add plugin alias feature to close #28

    * Update docs to show usage of 'as'

I'm very confused. You seem to be on a commit after the fixes, but line 216 of compile.lua (i.e. where the error is being reported) doesn't call ipairs; that happens on line 218, and getting the error on 216 seems to imply that the version of packer.nvim you have is not up to date...

Wait, I might know what's happening here.

Is there any chance you have two clones of packer? Maybe one in ~/.local/share/site/nvim/pack/packer/start and one in ~/.local/share/site/nvim/pack/packer/opt? From #30 I see that you initially cloned into opt, but here I see you're using packer as a start plugin. If the opt version is out of date and being loaded first for some reason, that could explain this (and why #31 didn't work for you!)

It isn't there (I had deleted that directory earlier when I removed the manual opt=true from the config

Maybe I should nuke ~/.local/share/site/nvim/pack and cleanly install it again

Fascinating, by which I mean "very confusing".

Could you please show me the contents of ~/.local/share/site/nvim/pack/packer/opt/packer.nvim/lua/packer/compile.lua from lines 210-220?

Or nuking should work, but I'm super confused by the current state of things.

Could you please show me the contents of ~/.local/share/site/nvim/pack/packer/opt/packer.nvim/lua/packer/compile.lua from lines 210-220?

tree /home/chinmay/.local/share/nvim/site/pack/packer -L 2

/home/chinmay/.local/share/nvim/site/pack/packer
โ”œโ”€โ”€ opt
โ”‚ย ย  โ”œโ”€โ”€ conjure
โ”‚ย ย  โ”œโ”€โ”€ fennel.vim
โ”‚ย ย  โ”œโ”€โ”€ float-preview.nvim
โ”‚ย ย  โ”œโ”€โ”€ markdown-preview.nvim
โ”‚ย ย  โ”œโ”€โ”€ neoterm
โ”‚ย ย  โ”œโ”€โ”€ vim-pencil
โ”‚ย ย  โ””โ”€โ”€ vim-sexp
โ”œโ”€โ”€ start
โ”‚ย ย  โ”œโ”€โ”€ ale
โ”‚ย ย  โ”œโ”€โ”€ animate.vim
โ”‚ย ย  โ”œโ”€โ”€ aniseed
โ”‚ย ย  โ”œโ”€โ”€ auto-pairs
โ”‚ย ย  โ”œโ”€โ”€ caw.vim
โ”‚ย ย  โ”œโ”€โ”€ completion-nvim
โ”‚ย ย  โ”œโ”€โ”€ completion-tabnine
โ”‚ย ย  โ”œโ”€โ”€ dashboard-nvim
โ”‚ย ย  โ”œโ”€โ”€ diagnostic-nvim
โ”‚ย ย  โ”œโ”€โ”€ editorconfig-vim
โ”‚ย ย  โ”œโ”€โ”€ fennel.vim
โ”‚ย ย  โ”œโ”€โ”€ git-messenger.vim
โ”‚ย ย  โ”œโ”€โ”€ indent-blankline.nvim
โ”‚ย ย  โ”œโ”€โ”€ indentLine
โ”‚ย ย  โ”œโ”€โ”€ LanguageTool.nvim
โ”‚ย ย  โ”œโ”€โ”€ lens.vim
โ”‚ย ย  โ”œโ”€โ”€ lsp-status.nvim
โ”‚ย ย  โ”œโ”€โ”€ nvim-bufferline.lua
โ”‚ย ย  โ”œโ”€โ”€ nvim-colorizer.lua
โ”‚ย ย  โ”œโ”€โ”€ nvim-lsp
โ”‚ย ย  โ”œโ”€โ”€ nvim-tree.lua
โ”‚ย ย  โ”œโ”€โ”€ nvim-treesitter
โ”‚ย ย  โ”œโ”€โ”€ nvim-web-devicons
โ”‚ย ย  โ”œโ”€โ”€ packer.nvim
โ”‚ย ย  โ”œโ”€โ”€ spaceline.vim
โ”‚ย ย  โ”œโ”€โ”€ vim
โ”‚ย ย  โ”œโ”€โ”€ vim-clap
โ”‚ย ย  โ”œโ”€โ”€ vim-devicons
โ”‚ย ย  โ”œโ”€โ”€ vim-fugitive
โ”‚ย ย  โ”œโ”€โ”€ vim-gitgutter
โ”‚ย ย  โ”œโ”€โ”€ vim-gtfo
โ”‚ย ย  โ”œโ”€โ”€ vim-mkdir
โ”‚ย ย  โ”œโ”€โ”€ vim-numbertoggle
โ”‚ย ย  โ”œโ”€โ”€ vim-sexp-mappings-for-regular-people
โ”‚ย ย  โ”œโ”€โ”€ vim-signify
โ”‚ย ย  โ”œโ”€โ”€ vim-smoothie
โ”‚ย ย  โ”œโ”€โ”€ vim-sneak
โ”‚ย ย  โ”œโ”€โ”€ vim-sonictemplate
โ”‚ย ย  โ”œโ”€โ”€ vim-surround
โ”‚ย ย  โ”œโ”€โ”€ vim-vsnip
โ”‚ย ย  โ”œโ”€โ”€ vim-vsnip-integ
โ”‚ย ย  โ””โ”€โ”€ vista.vim
โ””โ”€โ”€ tree

51 directories, 1 file

Yes - sorry, I meant the contents of the file ~/.local/share/site/nvim/pack/packer/start/packer.nvim/lua/packer/compile.lua from lines 210-220

line 210 is blank
lines 211-220:

      if plugin.cond then
        loaders[name].only_sequence = false
        loaders[name].only_setup = false
        if type(plugin.cond) == 'string' or type(plugin.cond) == 'function' then
          plugin.cond = {plugin.cond}
        end

        for _, condition in ipairs(plugin.cond) do
          if type(condition) == 'function' then
            condition = 'loadstring(' .. vim.inspect(string.dump(condition, true)) .. ')()'

line 221: end

(fresh clone)

Ok, that's clearly right....
If you delete packer_compile.vim, restart Neovim, and re-run PackerCompile, you still get the same error? I will be even more confused if that's the case...

yep same error

Bizarre. This really seems to imply that you have a second copy of packer somewhere; from the snippet you sent me, it should be trivially impossible for plugin.cond to be of type function in the call to ipairs.

If you run :messages in Neovim after getting the error, does it show you the full path to the compile.lua file which has the error on line 216?

/home/chinmay/.config/nvim/pack/packer/opt/packer.nvim/lua/packer/compile.lua

nuked /home/chinmay/.config/nvim/pack and everything is fine, even as works

Ah! So, you did have a second copy of packer, which was causing things to break. It was just in ~/.config/nvim/pack/packer/, not in the more standard install location of ~/.local/share/nvim/site/packer/.

I'm glad things are working; sorry for all the hassle!

Now that i think of it, I don't even know how it came there. I set up a new user to reproduce, this doesn't happen

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jzelinskie picture jzelinskie  ยท  6Comments

rockerBOO picture rockerBOO  ยท  4Comments

akinsho picture akinsho  ยท  5Comments

ghost picture ghost  ยท  3Comments

p00f picture p00f  ยท  5Comments