Skip to content

Commit 62820ce

Browse files
committed
fix(cli): Pass current binary name of Rust wrapper through for errors and make-deps
1 parent 696aafe commit 62820ce

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

core/makedeps.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ local makeDeps = {
22
_deps = {},
33

44
add = function (self, filename)
5+
-- The Rust CLI sets the main chunk name as =[C], but we also pass ourselves the actual binary name for use here.
6+
if filename == "=[C]" and _G.arg[0] then
7+
filename = _G.arg[0]
8+
end
59
SU.debug("makedeps", "Adding:", filename)
610
local resolvedFile, msg = package.searchpath(filename:gsub("^@?%./", ""), "?;" .. package.path, "/")
711
if not resolvedFile then

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ pub fn start_luavm() -> crate::Result<Lua> {
2626
}
2727
// For parity with the legacy Lua arg parser, allows inspection of CLI args at runtime in Lua.
2828
{
29+
let rt = std::env::args()
30+
.next()
31+
.unwrap_or_else(|| "sile".to_string());
2932
let args: Vec<String> = std::env::args().skip(1).collect();
3033
let arg_table = lua.create_table()?;
3134
for (i, arg) in args.iter().enumerate() {
3235
arg_table.set(i + 1, arg.clone())?;
3336
}
37+
// A bit non-orthodox, but the Lua side sets the VM chunk name to what moste CLIs expect $0
38+
// to be, their own binary name. The Rust side of mlua is setting the chunk name to =[C],
39+
// making error messages a bit cryptic. By setting a 0 index here we give the later Lua
40+
// side a chance to replace the chunk name with it.
41+
arg_table.set(0, rt)?;
3442
lua.globals().set("arg", arg_table)?;
3543
}
3644
lua = inject_paths(lua)?;

0 commit comments

Comments
 (0)