We want to integrate a third-party Lua module (say) foo into our CoronaSDK application.
Main code of this module module is in foo/init.lua, to be required as require 'foo'.
We placed this module into corona/foo:
corona/foo$ ls
init.lua
In corona/main.lua we have:
package.path = package.path .. ";"
.. package.path:gsub("?.lua", "?/init.lua")
require 'foo' works in the simulator. However, it does not work on the device:
Lua Runtime Error: lua_pcall failed with status: 2, error message is:
module 'foo' not found:resource (foo.lua) does not exist in
archive
no field package.preload['foo']
no file '/var/mobile/Applications/0B609A43-54E8-40E5-9D44-F3C93CC4031E/Appname.app/foo.lua'
no file '/var/mobile/Applications/0B609A43-54E8-40E5-9D44-F3C93CC4031E/Appname.app/foo.lua'
no file '/var/mobile/Applications/0B609A43-54E8-40E5-9D44-F3C93CC4031E/Appname.app/foo/init.lua'
no file '/var/mobile/Applications/0B609A43-54E8-40E5-9D44-F3C93CC4031E/Appname.app/foo/init.lua'
no file './foo.so'
Strangely enough, require 'foo.init' does work.
Any clues?
Is your filename
foo.lua or Foo.lua?
it works on simulator for both cases, but in devices, the capital matters.
I had this nightmare (I wonder why my SVN client changed the capital for one of my files... I spent over an hour to find out what went wrong because my module couldn't be found in the device suddenly)
Try this
package.path = package.path .. ";"
.. package.path:gsub("?.lua", "?.init.lua")
path names for lua files called in packages should be done like this:
dir.luafile.lua
rather than dir/luafile.lua
hope that helps
Related
I decided to spring-clean and update my nvim config files/plugins, and thought I’d make proper use of the after/plug folder.
While setting up LSP (with mason, mason-lspconfig, and lspconfig), I wanted to move all the lsp language server settings out from after/plugin/lsp/init.lua to their own files (now in after/plugin/lsp/settings).
The problem is I don’t seem to be able to require them into the init.lua file.
Things I’ve tried to no avail:
require(‘after/plugin/lsp/settings/sumneko_lua.lua’)
require(vim.fn.stdpath("config") .. "/after/plugin/lsp/settings/sumneko_lua”)
require(vim.fn.expand('%:h').. ‘/settings/sumneko_lua’)
The attempt using expand works when I resource the file in nvim; but causes an error when starting nvim.
I understand that all the files in after/plugin are automagically sourced at startup. So if I had a file shared.lua:
local M = {}
function M.greet()
vim.notify("Hello!”)
end
return M
in the same folder as after/plugin/lsp/init.lua, how can I get access to the greet() function from init.lua?
Any pointers would be greatly appreciated.
It turned out to be quite a simple solution in the end: I simply updated the paths to be searched in init.lua
-- nvim/init.lua
-- Allow require to look in after/plugin folder
local home_dir = os.getenv("HOME”)
package.path = home_dir .. "/.config/nvim/after/plugin/?.lua;" .. package.path
And then I can require any file inside the after/plugin folder
e.g - require(‘lsp/settings/sumneko’) or require(‘lsp/shared’).greet()
I'm writing a tool to parse lua plugins created by other users. The only guarentee about the plugin is that it has a data.lua file in a known directory. Inside there users are free to do anything they wish. This particular plugin using require to load a file and that file loads another file. Both are relative paths but the second is relative to the location of the first file.
data.lua
foo/bar.lua
foo/baz.lua
data.lua:
require("foo.bar")
foo/bar.lua:
require("baz")
When I try to execute data.lua I get an error when foo/bar.lua tries to require "baz". None of the paths it tries are ./foo/.
Any idea how I can fix this? I could find any documentation specifically about this case, it seemed like I need to hard code /foo/ into the path but I don't know it ahead of time. This seems like something that should be automatic is there a setting I'm missing or am I running the wrong version of lua? I'm using NLua 4.0
Thanks
I tested this script using node-lua and it fixes the issue for me!
https://gist.github.com/hoelzro/1299679
Relavent code:
local oldrequire = require
function require(modname)
local regular_loader = package.loaders[2]
local loader = function(inner)
if string.match(modname, '(.*)%.') then
return regular_loader(string.match(modname, '(.*)%.') .. '.' .. inner)
end
end
table.insert(package.loaders, 1, loader)
local retval = oldrequire(modname)
table.remove(package.loaders, 1)
return retval
end
To get this to work with Lua 5.2 change all uses of package.loaders to package.searchers.
Also if you want to override the global require function you need this snippet as well:
_G.require = require
You can alter the search behavior of require by changing the package.path variable.
Resources on package.path:
https://www.lua.org/manual/5.3/manual.html#pdf-package.path
http://lua-users.org/wiki/PackagePath
Example adding foo folder to search locations:
package.path = package.path .. ';./foo/?.lua'
the ? character will be where the string passed to require is placed.
Alternatively you can add a default file to load, to the package.path:
package.path = package.path .. ';./nested_require.lua'
Then define the behavior you would like within this file. You can use the global variable _REQUIREDNAME to reference the value passed to the require function.
The documentation for this method can be found here at the bottom of the page: https://www.lua.org/pil/8.1.html
Here's a screenshot:
https://drive.google.com/open?id=0B4Dqo44FM648VTVKVzBKSERldGc
I would like to have fun contributing to the development of an open source game but this problem is preventing me from doing this.
My LuaRocks install is configured for Lua 5.3 and is set to use MinGW's build tools. My OS is Windows 10 64 bit.
My Lua 5.3.2 binaries were provided by this website (lua-5.3.2_Win64_bin.zip).
If you have an idea for a possible cause please post it.
You have to resolve environment variables manually:
os.getenv (varname)
Returns the value of the process environment variable varname, or nil
if the variable is not defined.
So, in your case you need to obtain the %APPDATA% path first:
os.getenv("APPDATA")
Returns: C:\Users\USERNAME\AppData\Roaming
When searching for packages to load, Lua uses a pre-defined list of folders to search in. This list is defined in package.path which is a semicolon-delimited (;) string. To add a new folder, you must append it's path to package.path:
package.path = package.path .. ";" .. NEWFOLDER .. "\\?.lua"
Finally, your complete solution is this:
package.path = package.path .. ";" .. os.getenv("APPDATA") .. "\\luarocks\\share\\lua\\5.3\\?.lua"
local s = require("socket")
"Lua does not expand environment variable references in package.path, so %APPDATA% won't work. You need the real path. The LuaRocks installation script should have said that: "Note that the %APPDATA% element in the paths above is user specific and it MUST be replaced by its actual value." - siffiejoe
I need to call a LUA function, defined in another my .lua file; from another. So, what I want is the classic C/C++ include approach.
I tried with the following:
(file funcs.lua)
function myfunc(arg1, arg2)
..dosomething
end
and
(file main.lua)
package.path = package.path .. ";/path/to/libs/?.lua"
require "funcs"
myfunc(1, 2)
The require works good, but at execution I get this error:
attempt to call global 'myfunc' (a nil value)
How come?
Thanks in advance,
Thank you all for the comments; I'm running LUA under OpenResty/Nginx.
I solved by exporting directly the function(s), I don't know if this is the preferred method, but I noticed that is used by lots of newer LUA modules.
For example, I changed the code as follows:
file (funcs.lua)
local A = {}
function A.myfunc(arg1, arg2)
..dosomething
end
return A
(file main.lua)
package.path = package.path .. ";/path/to/libs/?.lua"
funcs = require "funcs"
funcs.myfunc(1, 2)
This works good and it's nice to have every function to be manually exported, in a sort of OOP-style.
I'm struggling to figure out how to structure and then use internal dependencies in a Lua library I'm writing.
I've defined my library like this:
./alib.lua
./alib/adependency.lua
And the code:
-- File: ./alib.lua
local ad = require "alib.adependency"
module( "alib")
return {}
-- File: ./alib/adependency.lua
module( "adependency" )
return {}
This works a treat:
$ lua alib.lua
<no output>
Now let's "add" this library into another app:
./anapp.lua
./lib/alib.lua
./lib/alib/adependency.lua
And the new code:
-- File: ./anapp.lua
local alib = require "lib.alib"
local print = print
module( "anapp")
print "Hello"
Try to run it:
$ lua anapp.lua
lua: ./lib/alib.lua:2: module 'alib.adependency' not found:
no field package.preload['alib.adependency']
no file './alib/adependency.lua'
no file '/usr/local/share/lua/5.1/alib/adependency.lua'
no file '/usr/local/share/lua/5.1/alib/adependency/init.lua'
no file '/usr/local/lib/lua/5.1/alib/adependency.lua'
no file '/usr/local/lib/lua/5.1/alib/adependency/init.lua'
no file '/usr/share/lua/5.1/alib/adependency.lua'
no file '/usr/share/lua/5.1/alib/adependency/init.lua'
no file './alib/adependency.so'
no file '/usr/local/lib/lua/5.1/alib/adependency.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/alib/adependency.so'
no file '/usr/lib/lua/5.1/alib/adependency.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './alib.so'
no file '/usr/local/lib/lua/5.1/alib.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/alib.so'
no file '/usr/lib/lua/5.1/alib.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'require'
./lib/alib.lua:2: in main chunk
[C]: in function 'require'
anapp.lua:2: in main chunk
[C]: ?
Oh dear. Now I make a manual edit inside the library:
-- File: ./lib/alib.lua
-- local ad = require "alib.adependency" -- Doesn't work
local ad = require "lib.alib.adependency" -- Works
module( "alib")
return {}
And it works:
$ lua anapp.lua
Hello
It seems that Lua's require() relates paths relative to the ultimate script being run by Lua, not the script in which the require() is called.
Surely you don't have to manually fix the internal paths require()d inside of a Lua library every time you add one to your project... And I don't see how this can work from a unit test perspective either. What am I doing wrong?
I think the cleanest solution is to leave your library as it was, and then make whomever is using your library responsible for configuring the package.path correctly. In your setup that means the application should add the lib folder to the path:
package.path = './lib/?.lua;' .. package.path
See also related questions like Is there a better way to require file from relative path in lua as well as the manual for more about the path.