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.87k stars 267 forks source link

Some plugin names break packer_compiled.lua #1029

Open svvac opened 2 years ago

svvac commented 2 years ago

Using a plugin name that is a reserved word in lua breaks the packer_compiled.lua file.

use { 'tpope/vim-repeat', as = 'repeat' }
[packer.nvim] [ERROR 18:10:09] async.lua:20: Error in coroutine: ...ox/config/nvim/pack/sys/start/packer.nvim/lua/packer.lua:725: Vim(source):E5112: Error while creating lua chunk: ...e/dir/src/sandbox/config/nvim/plugin/packer_compiled.lua:217: unexpected symbol near 'repeat'

This appears to be due to using the builtin vim.inspect as a means to stringify a table as lua code, however its output is not guaranteed to be deserializable as valid lua code.

https://github.com/wbthomason/packer.nvim/blob/3a9f9801f683946b9f1047d8f4bf9946c29e927d/lua/packer/compile.lua#L178

vim.inspect({ ['if'] = true })
-- {
--   if = true
-- }
svvac commented 2 years ago

The following patch works around the issue by manually escaping the plugin names in the output object, but it's rather brittle, and still relies on vim.inspect for the plugin configs:

  -- Crude `vim.inspect` replacement that unconditionnaly escapes plugin names to avoid invalid keys such as lua keywords
  -- Assumes the plugin objects are properly serialized by vim.inspect
  local out = '{\n'
  for k, v in pairs(result) do
    -- indent:
    local objstr = table.concat(vim.split(vim.inspect(v), '\n'), '\n  ')
    local objkeystr = '  [' .. string.format('%q', k) .. '] = ' .. objstr .. ',\n'
    out = out .. objkeystr
  end
  out = out .. '}'
  return out