jmcnamara / xlsxwriter.lua

A lua module for creating Excel XLSX files.
http://xlsxwriterlua.readthedocs.org/
MIT License
142 stars 52 forks source link

Error when using xlsxwriter in openresty #18

Open bacTlink opened 8 years ago

bacTlink commented 8 years ago

I use the following command to install xlsxwriter:

    git clone https://github.com/jmcnamara/xlsxwriter.lua.git
    cd xlsxwriter.lua
    sudo luarocks make

It installed successfully, but when I tried to use it, nginx report failure.

user root;
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 80;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say(package.path);
                local test0 = require("luarocks.loader");
                local workbook = require("xlsxwriter.workbook");
            }
        }
    }
}

I find this in the /nginx/error.log:

2016/08/24 23:21:08 [notice] 2801#0: signal process started
2016/08/24 23:21:12 [error] 2802#0: *3 lua entry thread aborted: runtime error: ...l/openresty/luajit/share/lua/5.1/xlsxwriter/workbook.lua:8: variable 'require' is not declared
stack traceback:
coroutine 0:
    [C]: in function 'require'
    content_by_lua(nginx.conf:17):4: in function <content_by_lua(nginx.conf:17):1>, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost"

It seems something wrong with /workbook.lua. I wonder how to fix it.

Thanks in advance.

moteus commented 8 years ago

Not sure but I think nginx create its own environment to each request. and do not set any variables to _G. So require does not defined in _G and xlsxwriter.strict complain about it. Solution just do not use any global variables in code or turn off strict. Both ways require small source change.

P.S. xlsxwriter require ZipWriter module which require some libraries which it not install automatically

jmcnamara commented 8 years ago

Solution just do not use any global variables in code or turn off strict.

@moteus There shouldn't be any global variables in the code. That is why the strict is there in the first place.

@bacTlink Can you remove the require "xlsxwriter.strict" line from all the xlsxwriter/*.lua files and see if that makes OpenResty happy?

If so, is there a way to if() it out when running under OpenResty or some other way of using it that is compatible with OpenResty.

Anyway, let me know and I'll push a fix.

P.S. xlsxwriter require ZipWriter module which require some libraries which it not install automatically

@moteus The OP had already installed the dependencies via luarocks but ran into issue #13 and couldn't install xlsxwriter.lua.

Thanks for the feedback from everyone.

moteus commented 8 years ago

@moteus There shouldn't be any global variables in the code. That is why the strict is there in the first place.

But they are exists :). require - https://github.com/jmcnamara/xlsxwriter.lua/blob/master/xlsxwriter/workbook.lua#L8 setmetatable - https://github.com/jmcnamara/xlsxwriter.lua/blob/master/xlsxwriter/workbook.lua#L23 assert - https://github.com/jmcnamara/xlsxwriter.lua/blob/master/xlsxwriter/workbook.lua#L27

@moteus The OP had already installed the dependencies via luarocks but ran into issue #13 and couldn't install xlsxwriter.lua.

Problem that luarocks does not install all deps for ZipWriter. There no way specify that this deps needs e.g. only for Lua 5.1 or for Lua <5.3. Also there exists 2 zlib binding wich provide same binary name zlib.so/zlib.dll So to load any of them need call require "zlib". So if system already has insalled e.g. lua-zlib and I install lzlib then existed code may stop working. So ZipWriter did not install any deps and user have to install them by hand. It sad but I do not see any other way.

bacTlink commented 8 years ago

@jmcnamara Can you remove the require "xlsxwriter.strict" line from all the xlsxwriter/*.lua files and see if that makes OpenResty happy?

I remove it and OpenResty seems returning the correct result.

@jmcnamara If so, is there a way to if() it out when running under OpenResty or some other way of using it that is compatible with OpenResty.

I don't know any way to do this. Can Lua check whether a module is loaded? In OpenResty "ngx" is always loaded.