luasocket icon indicating copy to clipboard operation
luasocket copied to clipboard

Fails with "module 'socket.http' not found" if LuaSec is not installed

Open sarna opened this issue 1 year ago • 0 comments

Requiring the library fails immediately with an unhelpful message if LuaSec is not installed:

$ cat foo.lua
http = require("socket.http")
$ lua -l set_paths foo.lua
lua: foo.lua:1: module 'socket.http' not found:
	no field package.preload['socket.http']
	no file 'lua_modules/share/lua/5.3/socket/http.lua'
	no file 'lua_modules/share/lua/5.3/socket/http/init.lua'
	no file '/opt/local/share/lua/5.3/socket/http.lua'
	no file '/opt/local/share/lua/5.3/socket/http/init.lua'
	no file '/opt/local/lib/lua/5.3/socket/http.lua'
	no file '/opt/local/lib/lua/5.3/socket/http/init.lua'
	no file './socket/http.lua'
	no file './socket/http/init.lua'
	no file 'lua_modules/lib/lua/5.3/socket/http.so'
	no file '/opt/local/lib/lua/5.3/socket/http.so'
	no file '/opt/local/lib/lua/5.3/loadall.so'
	no file './socket/http.so'
	no file 'lua_modules/lib/lua/5.3/socket.so'
	no file '/opt/local/lib/lua/5.3/socket.so'
	no file '/opt/local/lib/lua/5.3/loadall.so'
	no file './socket.so'
stack traceback:
	[C]: in function 'require'
	foo.lua:1: in main chunk
	[C]: in ?

set_paths is just a helper script that adds stuff in lua_modules to package.[c]path:

$ cat set_paths.lua
local version = _VERSION:match("%d+%.%d+")
package.path = 'lua_modules/share/lua/' .. version .. '/?.lua;lua_modules/share/lua/' .. version .. '/?/init.lua;' .. package.path
package.cpath = 'lua_modules/lib/lua/' .. version .. '/?.so;' .. package.cpath

In 2a46700 there was an attempt to fail with an informative error if LuaSec is not present - this fact is mentioned also in #229 and #293.

Here's the code:

local https = assert(
            require("ssl.https"), 'LuaSocket: LuaSec not found')

Unless I'm missing something, require will throw an error if not called inside pcall:

$ lua
Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> assert(require("notthere"), "lib is not there")
stdin:1: module 'notthere' not found:
	no field package.preload['notthere']
	no file '/opt/local/share/lua/5.3/notthere.lua'
	no file '/opt/local/share/lua/5.3/notthere/init.lua'
	no file '/opt/local/lib/lua/5.3/notthere.lua'
	no file '/opt/local/lib/lua/5.3/notthere/init.lua'
	no file './notthere.lua'
	no file './notthere/init.lua'
	no file '/opt/local/lib/lua/5.3/notthere.so'
	no file '/opt/local/lib/lua/5.3/loadall.so'
	no file './notthere.so'
stack traceback:
	[C]: in function 'require'
	stdin:1: in main chunk
	[C]: in ?
> assert(pcall(require, "notthere"), "lib is not there")
stdin:1: lib is not there
stack traceback:
	[C]: in function 'assert'
	stdin:1: in main chunk
	[C]: in ?

Perhaps it'd work if require was wrapped in a pcall, but seems like there are some subtleties to work around in Lua 5.2. Would be great if somebody fixed it, since the current error is extremely confusing to newbies like me. Using xpcall also sounds like a good idea, but as I said, I'm not that confident with Lua to write a patch that'd work across versions :(

sarna avatar Jun 30 '23 17:06 sarna