Search code examples
luapandoc

pandoc-lua cannot load system wide modules


In lua interpreter, I have:

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> require('socket')
table: 0x55edf33e82d0   /usr/share/lua/5.4/socket.lua

In pandoc-lua, I have:

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
Embedded in pandoc 3.1.3
> require('socket')
stdin:1: module 'socket' not found:
    no field package.preload['socket']
    no file '/usr/local/share/lua/5.4/socket.lua'
    no file '/usr/local/share/lua/5.4/socket/init.lua'
    no file '/usr/local/lib/lua/5.4/socket.lua'
    no file '/usr/local/lib/lua/5.4/socket/init.lua'
    no file './socket.lua'
    no file './socket/init.lua'
    no file '/usr/local/lib/lua/5.4/socket.so'
    no file '/usr/local/lib/lua/5.4/loadall.so'
    no file './socket.so'
stack traceback:
    stdin:1: in main chunk

I tried to change package.path in pandoc-lua by appending the correct directory:

> package.path = package.path .. ';/usr/share/lua/5.4/?.lua'
> require('socket')
/usr/share/lua/5.4/socket.lua:12: module 'socket.core' not found:
    no field package.preload['socket.core']
    no file '/usr/local/share/lua/5.4/socket/core.lua'
    no file '/usr/local/share/lua/5.4/socket/core/init.lua'
    no file '/usr/local/lib/lua/5.4/socket/core.lua'
    no file '/usr/local/lib/lua/5.4/socket/core/init.lua'
    no file './socket/core.lua'
    no file './socket/core/init.lua'
    no file '/usr/share/lua/5.4/socket/core.lua'
    no file '/usr/local/lib/lua/5.4/socket/core.so'
    no file '/usr/local/lib/lua/5.4/loadall.so'
    no file './socket/core.so'
    no file '/usr/local/lib/lua/5.4/socket.so'
    no file '/usr/local/lib/lua/5.4/loadall.so'
    no file './socket.so'
stack traceback:
    /usr/share/lua/5.4/socket.lua:12: in main chunk
    [C]: in function 'require'
    stdin:1: in main chunk

Now it's the socket.core which is not found...

I thought that pandoc-lua could load any lua package available on the system. I've spent a lot of time trying other solutions, and browsing the web, but did not find the solution. Does any see the error?

Thanks!

I am just trying to load lua module available on my system (Debian Testing) in pandoc filters.

My ultimate goal is to try to use the ideas from the code of pyluatex in a pandoc lua filter.


Solution

  • I think there are two issues here: the paths don't seem to be set correctly; and even if they were set correctly, it's possible that some further conditions need to be met.

    As to the former: You need to set package.cpath as well, which would point to /usr/<your system lib folder>/lua/5.4/?.so. The lua-socket package installs several .so files as well under LUA_CPATH: core.so, serial.so, unix.so, etc.

    (Also, in general package.path should include /usr/share/lua/5.4/?/init.lua, since that's the entry point for some packages.)

    I think the most straightforward way of setting these is via the environment variables, LUA_PATH and LUA_CPATH.

    Assuming that LUA_PATH and LUA_CPATH are both correctly set, the pandoc executable needs to be dynamically linked for compiled lua modules to work:

    from pandoc.org

    The executable is statically linked and has no dynamic dependencies or dependencies on external data files. Note: because of the static linking, the pandoc binary from this package cannot use lua filters that require external lua modules written in C.

    The debian package is probably statically linked; so is the binary available on github releases. If you compile from source via cabal, it can import compiled modules. Not sure about getting the latest release via cabal.