Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix require not throwing syntax error #168

Merged
merged 8 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions src/lune/globals/require/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::{Path, PathBuf};

use mlua::prelude::*;
use mlua::Error::ExternalError;

use super::context::*;

Expand All @@ -27,26 +28,33 @@ where
'lua: 'ctx,
{
// 1. Try to require the exact path
if let Ok(res) = require_inner(lua, ctx, &abs_path, &rel_path).await {
return Ok(res);
match require_inner(lua, ctx, &abs_path, &rel_path).await {
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
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
let (lua_abs_path, lua_rel_path) = (
append_extension(&abs_path, "lua"),
append_extension(&rel_path, "lua"),
);
if let Ok(res) = require_inner(lua, ctx, &lua_abs_path, &lua_rel_path).await {
return Ok(res);
for extension in ["luau", "lua"] {
match require_inner(
lua,
ctx,
&append_extension(&abs_path, extension),
&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
Expand All @@ -55,21 +63,23 @@ where
let rel_init = rel_path.join("init");

// 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
let (lua_abs_init, lua_rel_init) = (
append_extension(&abs_init, "lua"),
append_extension(&rel_init, "lua"),
);
if let Ok(res) = require_inner(lua, ctx, &lua_abs_init, &lua_rel_init).await {
return Ok(res);
for extension in ["luau", "lua"] {
match require_inner(
lua,
ctx,
&append_extension(&abs_init, extension),
&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
Expand Down Expand Up @@ -109,3 +119,11 @@ fn append_extension(path: impl Into<PathBuf>, ext: &'static str) -> PathBuf {
};
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
}
}
Loading