From 4c99a91999a15f5f6ac80939928e0c9302f5d997 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Wed, 17 May 2023 15:47:20 +0200 Subject: [PATCH] Canonicalize source path Signed-off-by: Ryan Levick --- crates/loader/src/local/mod.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/loader/src/local/mod.rs b/crates/loader/src/local/mod.rs index f14ee0ae88..cffd5c86d1 100644 --- a/crates/loader/src/local/mod.rs +++ b/crates/loader/src/local/mod.rs @@ -178,12 +178,7 @@ async fn core( let src = parent_dir(src)?; let source = match raw.source { config::RawModuleSource::FileReference(p) => { - let p = match p.is_absolute() { - true => p, - false => src.join(p), - }; - - ModuleSource::FileReference(p) + ModuleSource::FileReference(canonicalize_and_absolutize(p, &src)) } config::RawModuleSource::Url(us) => { let source = UrlSource::new(&us) @@ -225,6 +220,21 @@ async fn core( }) } +/// Ensures that the path is canonicalized and absolutized base on the `src` path +fn canonicalize_and_absolutize(mut path: PathBuf, src: &Path) -> PathBuf { + path = path.canonicalize().unwrap_or(path); + if let Ok(suffix) = path.strip_prefix("~") { + if let Some(home) = dirs::home_dir() { + path = home.join(suffix) + } + } + if path.is_absolute() { + path + } else { + src.join(path) + } +} + /// A parsed URL source for a component module. #[derive(Debug)] pub struct UrlSource {