Yuescript
Yuescript copied to clipboard
[Bug] yue.require does not propagate errors to Lua
The YueScript documentation says to use yue.require() to perform custom error handling:
local yue = require("yue")
local success, result = xpcall(function()
yue.require("yuescript_module_name")
end, function(err)
return yue.traceback(err)
end)
However, this does not work. When throwing an error in a .yue file, the call to yue.require() will not fail and just return nothing. The error handler defined here will not be called, either. Furthermore, yue.require() always prints a traceback with yue.traceback().
The screenshot shows a file called temp.yue that contains the following code:
const f = () ->
error("Test message")
f()
It is being executed with LuaJIT, with the following code:
yue = require("yue")
print(pcall(yue.require, "temp"))
The output is:
Test message
Stack Traceback
===============
(1) '=(yuescript)':172
(2) global C function 'pcall'
(3) '=stdin':1
(4) C function 'function: 0x7e8c2865c588'
true nil
A concise way I found to test this is:
_G.yue = require("yue")
s, r = xpcall(
(() -> require("nonexistent_file")),
((err)-> print("THIS IS NOT MISSING IN OUTPUT"))
)
s, r = xpcall(
(()-> yue.require("nonexistent_file")),
((err)-> print("THIS IS MISSING IN OUTPUT"))
)
The same error occurs when using pcall.
The yue.require() function was unintentionally swallowing errors, which is inconsistent with Lua's standard require() behavior. To align with Lua's conventions, I've decided to remove this shortcut API and instead provide an example for users to handle module loading properly:
local yue = require("yue")
yue.insert_loader()
local success, result = xpcall(function()
require("yuescript_module_name")
end, function(err)
return yue.traceback(err)
end)
I've just pushed a new commit that removes yue.require() and updated the documentation accordingly.