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 264 forks source link

`packer_compiled.lua` breaks when packer is installed under another name #939

Open Janfel opened 2 years ago

Janfel commented 2 years ago

Steps to reproduce

  1. Install packer under another name using the as key.
  2. Restart neovim.

Actual behaviour

The line vim.api.nvim_command('packadd packer.nvim') in packer_compiled.lua throws an error because packer.nvim could not be found.

E5113: Error while calling lua chunk: /home/janfel/.config/nvim/plugin/packer_compiled.lua:8: Vim(packadd):E919: Directory not found in 'packpath': "pack/*/opt/packer.nvim"
stack traceback:
    [C]: in function 'nvim_command'
    /home/janfel/.config/nvim/plugin/packer_compiled.lua:8: in main chunk

Expected behaviour

Packer works regardless of the name it is installed as.

packer files

Irrelevant to this issue.

Janfel commented 2 years ago

I have found a workaround by using sed to rewrite packer_compiled.lua on PackerCompileDone.

function()
    local file = require("packer").config.compile_path
    if vim.fn.filereadable(file) ~= 1 then return end

    local sed = vim.fn.exepath("sed")
    if sed == "" then error("Could not find `sed`") end

    -- This skips the first line, but line number zero is a GNU extension.
    -- https://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file
    local old = [[vim\.api\.nvim_command('packadd packer\.nvim')]]
    local new = [[vim\.api\.nvim_command('packadd packer')]]

    local sedscript = "1,/"..old.."/ s/"..old.."/"..new.."/"
    local cmd = { sed, "--in-place", "--expression", sedscript , "--", file }

    return vim.fn.jobstart(cmd, { stdin = "null" })
end