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.88k stars 266 forks source link

cmd is creating a duplicated, invalid command #225

Closed gbrlsnchs closed 3 years ago

gbrlsnchs commented 3 years ago

By following the markdown-preview.nvim example, I end up with an invalid :MarkdownPreview command which is available as soon as I start the editor. Then when the plugin kicks in (I only enable it for markdown filetypes) I end up with two :MarkdownPreview commands.

wbthomason commented 3 years ago

What makes the initial command invalid? The way cmd lazy-loading works is by creating a "fake" command with the same name at startup. Running this command deletes the fake definition and then loads the plugin.

Does the fake command cause an error, or not load the plugin? I'm also not sure what you mean by "two :MarkdownPreview commands" - usually, redefining a command name will remove old definitions for that command (or throw an error).

wbthomason commented 3 years ago

(there is an alternative way to implement cmd lazy-loading by using the CmdUndefined event. I've considered moving to that style, but haven't had problems with the current implementation, and so haven't done so)

gbrlsnchs commented 3 years ago

Just to make a little bit clearer, I have set markdown-preview.nvim to only load when I fire :MarkdownPreview. So I understand it's there as soon as I open the editor because lazy loading.

However, I have also set it up to only be loaded by Packer for certain filetypes (for example, markdown). markdown-preview.nvim itself needs to know for which filetypes it should set its commands, so it really felt double weird to have that command there without having a Markdown file open, but I understand this should be a debate for another issue.

The real issue here is that, when I open a Markdown file, I end up with two :MarkdownPreview commands. They seem to both work exactly the same, but it would be better if there was only one option. If I remove the cmd option from its configuration, only one command is loaded.

Here's a picture of my editor showing both commands: 1613855449

wbthomason commented 3 years ago

Fascinating, I didn't think that was possible. Could you please post your config for this plugin? I think I know what's going on - lazy-loaders represent a union of conditions, so if you open a Markdown file the plugin gets loaded but the original lazy-load command is still there - but I'd like to verify.

The fix will be making the lazy-load handler delete all loader shims on any shim activating, which shouldn't be too bad. EDIT: Wait, this should actually already be happening. Once you post your config, I'll trace through and see why it isn't.

gbrlsnchs commented 3 years ago

Sorry for taking some time to reply back with my config for the plugin!

I tend to split specs in different files, so there is this file specs/markdown_preview.lua:

local M = {'iamcco/markdown-preview.nvim'}

M.run = 'cd app && yarn install'

M.cmd = 'MarkdownPreview'

M.ft = {'markdown', 'plantuml'}

M.config = function()
    vim.cmd([[
function! g:Open_browser(url)
    silent exec 'silent !firefox -new-window ' . a:url
endfunction
    ]])

    vim.g.mkdp_filetypes = {'markdown', 'plantuml'}
    vim.g.mkdp_browserfunc = 'g:Open_browser'
end

M.cond = function()
    return vim.fn.executable('yarn') == 1
end

return M

And in plugins.lua:

return require('packer').startup(function()
    -- ...
    use(require('specs.markdown_preview'))
    -- ...
end)
ranjithshegde commented 3 years ago

I am going to watch this space! I have the exact same problem with the same plugin(markdown-preview), and a few other (vim-processing)

ranjithshegde commented 3 years ago

i can confirm that this issue is valid on many other plugins apart from markdown-preview

wbthomason commented 3 years ago

I'm still not sure why this happens, but #449 should incidentally also close this issue.

nanotee commented 3 years ago

markdown-preview.nvim creates buffer-local commands: https://github.com/iamcco/markdown-preview.nvim/blob/e5bfe9b89dc9c2fbd24ed0f0596c85fd0568b143/plugin/mkdp.vim#L103-L105

The issue is reproducible without packer in both Vim and Neovim by creating a global and a local command with the same name:

:command! Test echo 'globaltest'
:command! -buffer Test echo 'localtest'

I think this is an upstream bug