Fix require not throwing syntax error (#168)

This commit is contained in:
Someon1e 2024-04-15 22:21:25 +01:00 committed by GitHub
parent 8220216893
commit 3f79756f70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use mlua::prelude::*; use mlua::prelude::*;
use mlua::Error::ExternalError;
use super::context::*; use super::context::*;
@ -27,26 +28,33 @@ where
'lua: 'ctx, 'lua: 'ctx,
{ {
// 1. Try to require the exact path // 1. Try to require the exact path
if let Ok(res) = require_inner(lua, ctx, &abs_path, &rel_path).await { match require_inner(lua, ctx, &abs_path, &rel_path).await {
return Ok(res); Ok(res) => return Ok(res),
Err(err) => {
if !is_file_not_found_error(&err) {
return Err(err);
}
}
} }
// 2. Try to require the path with an added "luau" extension // 2. Try to require the path with an added "luau" extension
let (luau_abs_path, luau_rel_path) = (
append_extension(&abs_path, "luau"),
append_extension(&rel_path, "luau"),
);
if let Ok(res) = require_inner(lua, ctx, &luau_abs_path, &luau_rel_path).await {
return Ok(res);
}
// 3. Try to require the path with an added "lua" extension // 3. Try to require the path with an added "lua" extension
let (lua_abs_path, lua_rel_path) = ( for extension in ["luau", "lua"] {
append_extension(&abs_path, "lua"), match require_inner(
append_extension(&rel_path, "lua"), lua,
); ctx,
if let Ok(res) = require_inner(lua, ctx, &lua_abs_path, &lua_rel_path).await { &append_extension(&abs_path, extension),
return Ok(res); &append_extension(&rel_path, extension),
)
.await
{
Ok(res) => return Ok(res),
Err(err) => {
if !is_file_not_found_error(&err) {
return Err(err);
}
}
}
} }
// We didn't find any direct file paths, look // We didn't find any direct file paths, look
@ -55,21 +63,23 @@ where
let rel_init = rel_path.join("init"); let rel_init = rel_path.join("init");
// 4. Try to require the init path with an added "luau" extension // 4. Try to require the init path with an added "luau" extension
let (luau_abs_init, luau_rel_init) = (
append_extension(&abs_init, "luau"),
append_extension(&rel_init, "luau"),
);
if let Ok(res) = require_inner(lua, ctx, &luau_abs_init, &luau_rel_init).await {
return Ok(res);
}
// 5. Try to require the init path with an added "lua" extension // 5. Try to require the init path with an added "lua" extension
let (lua_abs_init, lua_rel_init) = ( for extension in ["luau", "lua"] {
append_extension(&abs_init, "lua"), match require_inner(
append_extension(&rel_init, "lua"), lua,
); ctx,
if let Ok(res) = require_inner(lua, ctx, &lua_abs_init, &lua_rel_init).await { &append_extension(&abs_init, extension),
return Ok(res); &append_extension(&rel_init, extension),
)
.await
{
Ok(res) => return Ok(res),
Err(err) => {
if !is_file_not_found_error(&err) {
return Err(err);
}
}
}
} }
// Nothing left to try, throw an error // Nothing left to try, throw an error
@ -109,3 +119,11 @@ fn append_extension(path: impl Into<PathBuf>, ext: &'static str) -> PathBuf {
}; };
new new
} }
fn is_file_not_found_error(err: &LuaError) -> bool {
if let ExternalError(err) = err {
err.as_ref().downcast_ref::<std::io::Error>().is_some()
} else {
false
}
}