From 44adf6774e2fa1fb87c19484b0ae933ddd6be059 Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Sun, 24 Mar 2024 22:10:53 +0000 Subject: [PATCH 1/6] Fix not throwing syntax error in require --- src/lune/globals/require/path.rs | 55 ++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index 3777b7e9..06449170 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -27,8 +27,15 @@ 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(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } // 2. Try to require the path with an added "luau" extension @@ -36,8 +43,15 @@ where 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); + match require_inner(lua, ctx, &luau_abs_path, &luau_rel_path).await { + Ok(res) => return Ok(res), + Err(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } // 3. Try to require the path with an added "lua" extension @@ -45,8 +59,15 @@ where 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); + match require_inner(lua, ctx, &lua_abs_path, &lua_rel_path).await { + Ok(res) => return Ok(res), + Err(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } // We didn't find any direct file paths, look @@ -59,8 +80,15 @@ where 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); + match require_inner(lua, ctx, &luau_abs_init, &luau_rel_init).await { + Ok(res) => return Ok(res), + Err(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } // 5. Try to require the init path with an added "lua" extension @@ -68,8 +96,15 @@ where 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); + match require_inner(lua, ctx, &lua_abs_init, &lua_rel_init).await { + Ok(res) => return Ok(res), + Err(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } // Nothing left to try, throw an error From 9e25ab51b9024abf83615123f2cb0427e3cb4e73 Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Sun, 24 Mar 2024 22:20:28 +0000 Subject: [PATCH 2/6] Use loop in applying extensions --- src/lune/globals/require/path.rs | 88 ++++++++++++-------------------- 1 file changed, 34 insertions(+), 54 deletions(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index 06449170..50e4a35c 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -39,35 +39,25 @@ where } // 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"), - ); - match require_inner(lua, ctx, &luau_abs_path, &luau_rel_path).await { - Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); - } - Err(_) => {} - } - // 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"), - ); - match require_inner(lua, ctx, &lua_abs_path, &lua_rel_path).await { - Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); + 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(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } - Err(_) => {} } // We didn't find any direct file paths, look @@ -76,35 +66,25 @@ 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"), - ); - match require_inner(lua, ctx, &luau_abs_init, &luau_rel_init).await { - Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); - } - Err(_) => {} - } - // 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"), - ); - match require_inner(lua, ctx, &lua_abs_init, &lua_rel_init).await { - Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); + 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(LuaError::SyntaxError { + message, + incomplete_input: _, + }) => { + return Err(LuaError::runtime(message)); + } + Err(_) => {} } - Err(_) => {} } // Nothing left to try, throw an error From 39870f6b879d58f103f96165b497aee704a761f7 Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Sun, 24 Mar 2024 22:35:41 +0000 Subject: [PATCH 3/6] Throw syntax error instead of runtime error --- src/lune/globals/require/path.rs | 42 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index 50e4a35c..b289c8e3 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -29,13 +29,15 @@ where // 1. Try to require the exact path match require_inner(lua, ctx, &abs_path, &rel_path).await { Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); + Err(error) => { + if let LuaError::SyntaxError { + message: _, + incomplete_input: _, + } = error + { + return Err(error); + } } - Err(_) => {} } // 2. Try to require the path with an added "luau" extension @@ -50,13 +52,15 @@ where .await { Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); + Err(error) => { + if let LuaError::SyntaxError { + message: _, + incomplete_input: _, + } = error + { + return Err(error); + } } - Err(_) => {} } } @@ -77,13 +81,15 @@ where .await { Ok(res) => return Ok(res), - Err(LuaError::SyntaxError { - message, - incomplete_input: _, - }) => { - return Err(LuaError::runtime(message)); + Err(error) => { + if let LuaError::SyntaxError { + message: _, + incomplete_input: _, + } = error + { + return Err(error); + } } - Err(_) => {} } } From ba9ebb2a53875f5ee36973c087ab9dadcbaa9e9b Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:46:50 +0100 Subject: [PATCH 4/6] Check if not FileNotFound instead of if SyntaxError --- src/lune/globals/require/path.rs | 44 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index b289c8e3..29e080ce 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -1,6 +1,8 @@ +use std::io::ErrorKind; use std::path::{Path, PathBuf}; use mlua::prelude::*; +use mlua::Error::ExternalError; use super::context::*; @@ -29,13 +31,9 @@ where // 1. Try to require the exact path match require_inner(lua, ctx, &abs_path, &rel_path).await { Ok(res) => return Ok(res), - Err(error) => { - if let LuaError::SyntaxError { - message: _, - incomplete_input: _, - } = error - { - return Err(error); + Err(err) => { + if !is_file_not_found_error(&err) { + return Err(err); } } } @@ -52,13 +50,9 @@ where .await { Ok(res) => return Ok(res), - Err(error) => { - if let LuaError::SyntaxError { - message: _, - incomplete_input: _, - } = error - { - return Err(error); + Err(err) => { + if !is_file_not_found_error(&err) { + return Err(err); } } } @@ -81,13 +75,9 @@ where .await { Ok(res) => return Ok(res), - Err(error) => { - if let LuaError::SyntaxError { - message: _, - incomplete_input: _, - } = error - { - return Err(error); + Err(err) => { + if !is_file_not_found_error(&err) { + return Err(err); } } } @@ -130,3 +120,15 @@ fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { }; new } + +fn is_file_not_found_error(err: &LuaError) -> bool { + if let ExternalError(err) = err { + if let Some(err) = err.as_ref().downcast_ref::() { + err.kind() == ErrorKind::NotFound + } else { + false + } + } else { + false + } +} From 0a2504dbda3aad2c169369136007b612c402546b Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Thu, 11 Apr 2024 16:46:06 +0100 Subject: [PATCH 5/6] Fix missing directory error --- src/lune/globals/require/path.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index 29e080ce..6c8496d5 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -124,7 +124,10 @@ fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { fn is_file_not_found_error(err: &LuaError) -> bool { if let ExternalError(err) = err { if let Some(err) = err.as_ref().downcast_ref::() { - err.kind() == ErrorKind::NotFound + matches!( + err.kind(), + ErrorKind::NotFound | ErrorKind::PermissionDenied + ) } else { false } From 877fd701c6ab156f01cc8a1584baf5b57183a286 Mon Sep 17 00:00:00 2001 From: Someon1e <142684596+Someon1e@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:17:02 +0100 Subject: [PATCH 6/6] Fix errors on non-windows --- src/lune/globals/require/path.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/lune/globals/require/path.rs b/src/lune/globals/require/path.rs index 6c8496d5..7e8084f7 100644 --- a/src/lune/globals/require/path.rs +++ b/src/lune/globals/require/path.rs @@ -1,4 +1,3 @@ -use std::io::ErrorKind; use std::path::{Path, PathBuf}; use mlua::prelude::*; @@ -123,14 +122,7 @@ fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { fn is_file_not_found_error(err: &LuaError) -> bool { if let ExternalError(err) = err { - if let Some(err) = err.as_ref().downcast_ref::() { - matches!( - err.kind(), - ErrorKind::NotFound | ErrorKind::PermissionDenied - ) - } else { - false - } + err.as_ref().downcast_ref::().is_some() } else { false }