packer.nvim icon indicating copy to clipboard operation
packer.nvim copied to clipboard

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

Open Janfel opened this issue 3 years ago • 1 comments

  • nvim --version: 0.7.0
  • git --version: 2.36.1
  • Operating system/version: Manjaro 21.2.6 Qonos
  • Terminal name/version: konsole 22.04.1

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 avatar Jun 16 '22 23:06 Janfel

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

Janfel avatar Jun 17 '22 08:06 Janfel