Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Difference between luafile and lua require #1

Closed
clason opened this issue Aug 10, 2020 · 2 comments
Closed

Difference between luafile and lua require #1

clason opened this issue Aug 10, 2020 · 2 comments

Comments

@clason
Copy link

clason commented Aug 10, 2020

First of all, wonderful write-up! It really fills a hole in the current documentation (and pretty much closes my neovim issue about the lack of such a high-level overview: neovim/neovim#12369)

One question I still have is about the practical difference between :lua require'foo' and :luafile PATH_TO_FOO.LUA. Is it just about the explicit path for the latter and the machinery that prevents loading twice for the former? (Assuming that foo.lua is a single lua script, nothing fancy involving modules.)

@nanotee
Copy link
Owner

nanotee commented Aug 11, 2020

Thanks! I had your issue in mind when writing this, it's part of what prompted me to start the guide

My understanding of the differences if you're not doing anything with modules is basically the same as yours:

  • :luafile will execute a script regardless of whether it's been executed before
  • :luafile, being an ex command, takes a path that's either absolute or relative to the working directory of the current window
  • :lua require('foo') prevents the script from being parsed and executed a second time (unless you're setting package.loaded['foo'] to nil or false)
  • :lua require('foo') uses package.path to search for modules

I'm far from being a Lua expert though, so there might be other things to take into account.

It'd be good to add a section explaining these differences, although I would expect :luafile to become less useful once Neovim supports loading init.lua directly. Not really sure where such a section would go, maybe after v:lua?

@clason
Copy link
Author

clason commented Aug 11, 2020

Thanks! I had your issue in mind when writing this, it's part of what prompted me to start the guide

I was wondering whether it was a coincidence that it addressed so many of my questions directly :)

It'd be good to add a section explaining these differences, although I would expect :luafile to become less useful once Neovim supports loading init.lua directly. Not really sure where such a section would go, maybe after v:lua?

I think it's sufficient to make a brief remark on this in the section on luafile -- that's where I expect people (like me) have the question "should I do this or lua require then? does it make a difference?"

(Not sure it'll become obsolete, although that depends on how exactly init.lua is handled. I use it for sourcing small lua-only plugin configurations, for which I'd use config/plugin files in vimscript -- if lua files are automatically sourced in that directory, I do indeed see this becoming less useful.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants