Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to set LUA_PATH? #20

Closed
kabukky opened this issue Apr 22, 2015 · 4 comments
Closed

Best way to set LUA_PATH? #20

kabukky opened this issue Apr 22, 2015 · 4 comments
Labels

Comments

@kabukky
Copy link

kabukky commented Apr 22, 2015

Hi,
I love working with gopher-lua so far. The documentation is lacking tough.
What would be the best way to set the LUA_PATH? In order to load modules from subdirectories located relative to the executed .lua file, I need to set it to the directory of that .lua file.
I discovered that the Lua vm uses system env variables set via os.Setenv(). Is there a way to set / append to the LUA_PATH just for one vm (e.g. vm.Setenv())?

Thanks

@yuin
Copy link
Owner

yuin commented Apr 22, 2015

Hi.

Thanks for working with GopherLua.
You can do it in much the same way as the original Lua implementation.
package.path is the path used by require to search for a GopherLua loader.

The best way that you might want is to set package.path as follows:

local script_path = _getscriptdir() -- implement in your host golang program
package.path = script_path .. [[/subdir/?.lua;]] .. package.path

@kabukky
Copy link
Author

kabukky commented Apr 22, 2015

That seems sensible, thank you :)
But I'm getting this error when executing test.lua:

[G]: plugins line:1(column:1) near '�':   Invalid token

stack traceback:
    [G]: in anonymous function
    [G]: in require
    test.lua:2: in main chunk
    [G]: ?

I have a test.lua and a testmod.lua, both in the same directory.

Here's what my lua.test looks like (getCurrentDir() returns ";/absolute/path/to/script/dir"):

package.path = package.path .. getCurrentDir()
require "testmod"
print(testmod.foo())

Here's what my testmod.lua looks like:

local testmod = {}

function testmod()
    return "Hello World!"
end

return testmod

I'm new to Lua, so please excuse me if this is obvious. But from what I gather from the reference, this should load the module, right?

Thanks

@yuin
Copy link
Owner

yuin commented Apr 23, 2015

package.path = package.path .. getCurrentDir()

to

package.path = package.path .. getCurrentDir() .. [[/?.lua]]

Refer to Lua5.1 - package.loaders section for further information.

@kabukky
Copy link
Author

kabukky commented Apr 25, 2015

Thank you so much, I forgot about the .lua wildcard.
I'm using GopherLua to drive the plugin system of Journey now :)

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

No branches or pull requests

2 participants