Skip to content

Commit

Permalink
Canonicalize source path
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed May 17, 2023
1 parent 2bdc2c5 commit 4c99a91
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions crates/loader/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4c99a91

Please sign in to comment.