Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

[Bug] yue.require does not propagate errors to Lua

Open SkyyySi opened this issue 10 months ago • 2 comments

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().

Image

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

SkyyySi avatar Jan 25 '25 16:01 SkyyySi

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.

MTadder avatar Jan 27 '25 01:01 MTadder

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.

pigpigyyy avatar Jan 29 '25 06:01 pigpigyyy