NeoVim nested require throws errors

329 views Asked by At

I was following this tutorial here to set up NeoVim:
https://www.youtube.com/watch?v=J9yqSdvAKXY

However, it doesn't work and throws a weird error that I don't understand and can't find anything on google for.

I know that this tutorial is for Linux, but I followed another tutorial for windows with the same results.

Restarting/-installing Neovim does not solve the issue.

Environment

I am working on Windows 11 (22H2) with NeoVim 0.9.2.

NeoVim does start (and Packer installed the plugins), but with an error in the lua scripts.

My structure inside the nvim folder is this:
(Why the icons are not working is another question...)
Folder Structure

Error

The error I am getting is:

E5113: Error while calling lua chunk: vim/_init_packages.lua:21: ...r1\AppData\Local\nvim/lua/core/plugin_config/gruvbox.lua:1: '=' expected
stack traceback:
        [C]: in function 'error'
        vim/_init_packages.lua:21: in function <vim/_init_packages.lua:15>
        [C]: in function 'require'
        ...user1\AppData\Local\nvim/lua/core/plugin_config/init.lua:2: in main chunk
        [C]: in function 'require'
        C:\Users\user1\AppData\Local\nvim\init.lua:6: in main chunk

If you scroll down to lua/core/plugin_config/init.lua, you will se that the require parts throw this error, but if I paste the contents of the config scripts (lua/core/plugin_config/*.lua) in lua/core/plugin_config/init.lua, it runs without errors.

Also, when I leave the gruvbox.lua, lualine.lua or nvim-tree.lua files empty, almost the same error occurs:

E5113: Error while calling lua chunk: vim/_init_packages.lua:21: ...r1\AppData\Local\nvim/lua/core/plugin_config/gruvbox.lua:1: '=' expected near '<eof>'
stack traceback:
        [C]: in function 'error'
        vim/_init_packages.lua:21: in function <vim/_init_packages.lua:15>
        [C]: in function 'require'
        ...user1\AppData\Local\nvim/lua/core/plugin_config/init.lua:2: in main chunk
        [C]: in function 'require'
        C:\Users\user1\AppData\Local\nvim\init.lua:6: in main chunk

Code-ZIP

https://drive.google.com/file/d/188hEJQOvcrS7G3zwBgOxKq91adIEpQli/view?usp=sharing

Code

init.lua

require("core.keymaps")
require("core.plugins")
require("core.plugin_config")

lua/core/keymaps.lua

vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

vim.opt.backspace = '2'
vim.opt.showcmd = true
vim.opt.laststatus = 2
vim.opt.autowrite = true
vim.opt.cursorline = true
vim.opt.autoread = true

vim.opt.tabstop = 2
vim.opt.shiftwidth = 2
vim.opt.shiftround = true
vim.opt.expandtab = true

vim.keymap.set('n', '<leader>h', ':nohlsearch<CR>')

lua/core/plugins.lua

local ensure_packer = function()
  local fn = vim.fn
  local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
  if fn.empty(fn.glob(install_path)) > 0 then
    fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
    vim.cmd [[packadd packer.nvim]]
    return true
  end
  return false
end

local packer_bootstrap = ensure_packer()

return require('packer').startup(function(use)
  use 'wbthomason/packer.nvim'
  use 'ellisonleao/gruvbox.nvim'
  use 'nvim-tree/nvim-tree.lua'
  use 'nvim-tree/nvim-web-devicons'
  use 'nvim-lualine/lualine.nvim'

  if packer_bootstrap then
    require('packer').sync()
  end
end)

lua/core/plugin_config/init.lua

-- Requiring the configs in files does not work
require("core.plugin_config.gruvbox")
require("core.plugin_config.lualine")
require("core.plugin_config.nvim-tree")

-- Pasting the config code here works
vim.o.termguicolors = true
vim.cmd [[ colorscheme gruvbox ]]


require('lualine').setup {
    options = {
    icons_enabled = true,
    theme = 'gruvbox',
    },
    sections = {
    lualine_a = {
        {
        'filename',
        path = 1,
        }
    }
    }
}


vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

require("nvim-tree").setup()

lua/core/plugin_config/gruvbox.lua

vim.o.termguicolors = true
vim.cmd [[ colorscheme gruvbox ]]

lua/core/plugin_config/lualine.lua

require('lualine').setup {
  options = {
    icons_enabled = true,
    theme = 'gruvbox',
  },
  sections = {
    lualine_a = {
      {
        'filename',
        path = 1,
      }
    }
  }
}

lua/core/plugin_config/nvim-tree.lua

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

require("nvim-tree").setup()
2

There are 2 answers

0
RD4 On BEST ANSWER

I tried using Lazy instead of Packer and everything works fine.

I have no clue why Packer failed but I wanted to switch to Lazy sooner or later anyways.

EDIT:
The icons didn't show because I wasn't using a nerd font.
See here: https://nerdfonts.com

0
koyaanisqatsi On

'I know that this tutorial is for Linux, but I followed another tutorial for windows with the same results.'

I am pretty sure your Errors in require depends on wrong use of Slashes and Backslashes.
I mean it looks you use Scripts for a Linux Machine on Windows and that will fail.

...r1\AppData\Local\nvim/lua/core/plugin_config/gruvbox.lua:1: '=' expected

The Windows Path without the filename ends: ...r1\AppData\Local\
...and the filename will be interpreted with Slashes as: "nvim/lua/core/plugin_config/gruvbox.lua"
So try to correct/replace every occurance of a Slash in a Path with: \\ (Escaped Backslash)
That will be interpreted by Lua to a single Backslash.

PS: The Dot (.) in require is OS Independend but harcoded Paths in the Script' that dont make a decision ("Am i on Linux or Windows?" - LuaJIT/FFI) will fail.