Search code examples
luapackageluarocks

Luarocks: Creating a rock from package with multiple files


Using Luarocks, I've followed the tutorial on Creating a rock. However, I'm having difficulty creating a rock of a package that contains multiple files. What do I have to do differently from the tutorial when my package is spread out across multiple files?

Example - One File

Say I have the following file testrock.lua:

module('testrock')
function add(a, b)
    return a+b
end

and in testrock-scm-1.rockspec

package = "testrock"
version = "scm-1"

source = ...
description = ...
dependencies = ...

build = {
   type = "builtin",
   modules = {
       testrock = "testrock.lua"
   }
}

I then run luarocks make and it installs testrock just fine (I can go to another directory and run require 'testrock').

Example - Two Files

However, let's say I want to add another file foo.lua:

function testrock.sub(a, b)
    return a - b
end

I add the following to the end of testrock.lua:

require('foo')

and run luarocks make. make works, but when I then go to another directory and run `require 'testrock`` I get the following error:

/home/<username>/torch/install/share/lua/5.1/testrock.lua:7: attempt to call global 'require' (a nil value)

and so it's complaining about the require('foo') statement. Any advice?


Solution

  • The call to module('testrock') hides all global variables, including the global require function. You can either call require before the call to module, or create a local alias (local require = require) before the call to module, or use the package.seeall option (module('testrock', package.seeall)).

    Adding the foo module to your rockspec, so that it is installed together with your testrock.lua file, is straightforward:

    -- ...
    build = {
       type = "builtin",
       modules = {
           testrock = "testrock.lua",
           foo = "foo.lua"
       }
    }
    -- ...