luvit/luvit

fs module does not propagate errors properly

ravener opened this issue · 0 comments

Some synchronous functions in fs do not check for errors properly, e.g consider this portion of fs.readdirSync

function fs.readdirSync(path)
  local req = uv.fs_scandir(path)
  local files = {}
  local i = 1
  while true do
    local ent = uv.fs_scandir_next(req)

It did not consider the possibility of uv.fs_scandir returning a nil, err and ends up passing nil to the next uv call instead of propagating the error to the user.

This leads to confusing errors like the following when a filepath does not exist:

> fs.readdirSync("aksnsjwjsjs")
[string "bundle:/deps/fs.lua"]:205: bad argument #1 to 'fs_scandir_next' (uv_req expected, got nil)
stack traceback:
        [C]: in function 'fs_scandir_next'
        [string "bundle:/deps/fs.lua"]:205: in function <[string "bundle:/deps/fs.lua"]:200>
        [C]: in function 'xpcall'
        [string "bundle:/deps/repl.lua"]:97: in function 'evaluateLine'
        [string "bundle:/deps/repl.lua"]:189: in function <[string "bundle:/deps/repl.lua"]:187>
>

It should instead report the error as an ENOENT from our side instead of a stacktrace coming from luvit's source.