mirror of
https://github.com/lune-org/lune.git
synced 2025-04-18 10:53:46 +01:00
Fix require on files with multiple extensions
This commit is contained in:
parent
df8570b16e
commit
9ff142e6e2
5 changed files with 36 additions and 14 deletions
|
@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed not being able to require files with multiple extensions, eg. `module.spec.luau` was not require-able using `require("module.spec")`
|
||||||
- Fixed instances and `roblox` built-in library APIs erroring when used asynchronously/concurrently.
|
- Fixed instances and `roblox` built-in library APIs erroring when used asynchronously/concurrently.
|
||||||
|
|
||||||
## `0.7.5` - July 22nd, 2023
|
## `0.7.5` - July 22nd, 2023
|
||||||
|
|
|
@ -6,7 +6,6 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use dunce::canonicalize;
|
|
||||||
use mlua::{prelude::*, Compiler as LuaCompiler};
|
use mlua::{prelude::*, Compiler as LuaCompiler};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tokio::sync::Mutex as AsyncMutex;
|
use tokio::sync::Mutex as AsyncMutex;
|
||||||
|
@ -29,6 +28,19 @@ return yield()
|
||||||
|
|
||||||
type RequireWakersVec<'lua> = Vec<Arc<AsyncMutex<RequireWakerState<'lua>>>>;
|
type RequireWakersVec<'lua> = Vec<Arc<AsyncMutex<RequireWakerState<'lua>>>>;
|
||||||
|
|
||||||
|
fn append_extension_and_canonicalize(
|
||||||
|
path: impl Into<PathBuf>,
|
||||||
|
ext: &'static str,
|
||||||
|
) -> Result<PathBuf, std::io::Error> {
|
||||||
|
let mut new = path.into();
|
||||||
|
match new.extension() {
|
||||||
|
// FUTURE: There's probably a better way to do this than converting to a lossy string
|
||||||
|
Some(e) => new.set_extension(format!("{}.{ext}", e.to_string_lossy())),
|
||||||
|
None => new.set_extension(ext),
|
||||||
|
};
|
||||||
|
dunce::canonicalize(new)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
struct RequireContext<'lua> {
|
struct RequireContext<'lua> {
|
||||||
// NOTE: We need to use arc here so that mlua clones
|
// NOTE: We need to use arc here so that mlua clones
|
||||||
|
@ -128,25 +140,28 @@ impl<'lua> RequireContext<'lua> {
|
||||||
.join(&require_path);
|
.join(&require_path);
|
||||||
// Try to normalize and resolve relative path segments such as './' and '../'
|
// Try to normalize and resolve relative path segments such as './' and '../'
|
||||||
let file_path = match (
|
let file_path = match (
|
||||||
canonicalize(path_relative_to_pwd.with_extension("luau")),
|
append_extension_and_canonicalize(&path_relative_to_pwd, "luau"),
|
||||||
canonicalize(path_relative_to_pwd.with_extension("lua")),
|
append_extension_and_canonicalize(&path_relative_to_pwd, "lua"),
|
||||||
) {
|
) {
|
||||||
(Ok(luau), _) => luau,
|
(Ok(luau), _) => luau,
|
||||||
(_, Ok(lua)) => lua,
|
(_, Ok(lua)) => lua,
|
||||||
// If we did not find a luau/lua file at the wanted path,
|
// If we did not find a luau/lua file at the wanted path,
|
||||||
// we should also look for "init" files in directories
|
// we should also look for "init" files in directories
|
||||||
_ => match (
|
_ => {
|
||||||
canonicalize(path_relative_to_pwd.join("init").with_extension("luau")),
|
let init_dir_path = path_relative_to_pwd.join("init");
|
||||||
canonicalize(path_relative_to_pwd.join("init").with_extension("lua")),
|
match (
|
||||||
) {
|
append_extension_and_canonicalize(&init_dir_path, "luau"),
|
||||||
(Ok(luau), _) => luau,
|
append_extension_and_canonicalize(&init_dir_path, "lua"),
|
||||||
(_, Ok(lua)) => lua,
|
) {
|
||||||
_ => {
|
(Ok(luau), _) => luau,
|
||||||
return Err(LuaError::RuntimeError(format!(
|
(_, Ok(lua)) => lua,
|
||||||
"File does not exist at path '{require_path}'"
|
_ => {
|
||||||
)))
|
return Err(LuaError::RuntimeError(format!(
|
||||||
|
"File does not exist at path '{require_path}'"
|
||||||
|
)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
let absolute = file_path.to_string_lossy().to_string();
|
let absolute = file_path.to_string_lossy().to_string();
|
||||||
let relative = absolute.trim_start_matches(&self.pwd).to_string();
|
let relative = absolute.trim_start_matches(&self.pwd).to_string();
|
||||||
|
|
|
@ -69,6 +69,7 @@ create_tests! {
|
||||||
require_children: "require/tests/children",
|
require_children: "require/tests/children",
|
||||||
require_init: "require/tests/init",
|
require_init: "require/tests/init",
|
||||||
require_invalid: "require/tests/invalid",
|
require_invalid: "require/tests/invalid",
|
||||||
|
require_multi_ext: "require/tests/multi_ext",
|
||||||
require_nested: "require/tests/nested",
|
require_nested: "require/tests/nested",
|
||||||
require_parents: "require/tests/parents",
|
require_parents: "require/tests/parents",
|
||||||
require_siblings: "require/tests/siblings",
|
require_siblings: "require/tests/siblings",
|
||||||
|
|
4
tests/require/tests/multi.ext.file.luau
Normal file
4
tests/require/tests/multi.ext.file.luau
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
return {
|
||||||
|
Foo = "Bar",
|
||||||
|
Hello = "World",
|
||||||
|
}
|
1
tests/require/tests/multi_ext.luau
Normal file
1
tests/require/tests/multi_ext.luau
Normal file
|
@ -0,0 +1 @@
|
||||||
|
require("multi.ext.file")
|
Loading…
Add table
Reference in a new issue