wbthomason / packer.nvim

A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config
MIT License
7.84k stars 265 forks source link

"if" in requires #29

Closed p00f closed 4 years ago

p00f commented 4 years ago

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?

p00f commented 4 years ago

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

p00f commented 4 years ago

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)

wbthomason commented 4 years ago

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.

p00f commented 4 years ago

Thanks, I'll try this

p00f commented 4 years ago

{'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)

p00f commented 4 years ago

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'
wbthomason commented 4 years ago

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).

p00f commented 4 years ago

Yep, still there

wbthomason commented 4 years ago

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

p00f commented 4 years ago

yes

p00f commented 4 years ago

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

p00f commented 4 years ago

Nope doesnt, so sorry :P

wbthomason commented 4 years ago

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?

p00f commented 4 years ago

specific commit of packer you're using

commit 64d717bcfd437466de8cf168882a118bad274c52 (HEAD -> master, origin/master, origin/HEAD)
Author: Wil Thomason <wil.thomason@gmail.com>
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

wbthomason commented 4 years ago

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.

p00f commented 4 years ago

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

p00f commented 4 years ago

https://del.dog/grorapetuh

wbthomason commented 4 years ago

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 }
  2. Exit and restart Neovim
  3. Run PackerCompile
  4. 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.

p00f commented 4 years ago

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)

p00f commented 4 years ago

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
p00f commented 4 years ago

packer.nvim 's git log -1 :

commit 6ec7d148cf827755f6d1fbd13b68e87c00d5ea95 (grafted, HEAD -> master, origin/master, origin/HEAD)
Author: Wil Thomason <wbthomason@users.noreply.github.com>
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'
wbthomason commented 4 years ago

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...

wbthomason commented 4 years ago

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!)

p00f commented 4 years ago

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

p00f commented 4 years ago

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

wbthomason commented 4 years ago

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.

p00f commented 4 years ago

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
wbthomason commented 4 years ago

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

p00f commented 4 years ago

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

p00f commented 4 years ago

(fresh clone)

wbthomason commented 4 years ago

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...

p00f commented 4 years ago

yep same error

wbthomason commented 4 years ago

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?

p00f commented 4 years ago

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

p00f commented 4 years ago

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

wbthomason commented 4 years ago

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!

p00f commented 4 years ago

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