From 0a0d5259c24878dae00838954a6ab2a9b8e0e3f0 Mon Sep 17 00:00:00 2001 From: Brian May Date: Mon, 23 Dec 2024 09:19:00 +1100 Subject: [PATCH] fix: store relative paths in executable --- packages/cli/src/cli/build_assets.rs | 27 ++----------------- packages/manganis/manganis-core/src/asset.rs | 3 --- packages/manganis/manganis-macro/src/asset.rs | 10 +++---- 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/packages/cli/src/cli/build_assets.rs b/packages/cli/src/cli/build_assets.rs index 3a68346598..b81c905ae8 100644 --- a/packages/cli/src/cli/build_assets.rs +++ b/packages/cli/src/cli/build_assets.rs @@ -1,7 +1,4 @@ -use std::{ - fs::create_dir_all, - path::{Path, PathBuf}, -}; +use std::{fs::create_dir_all, path::PathBuf}; use crate::{Result, StructuredOutput}; use clap::Parser; @@ -27,8 +24,7 @@ impl BuildAssets { create_dir_all(&self.destination)?; for (path, asset) in manifest.assets.iter() { - let relative_path = turn_asset_path_into_relative_path(path); - let source_path = self.source.join(relative_path); + let source_path = self.source.join(path); let destination_path = self.destination.join(asset.bundled_path()); debug!( "Processing asset {} --> {} {:#?}", @@ -42,22 +38,3 @@ impl BuildAssets { Ok(StructuredOutput::Success) } } - -/// Hack to turn an absolute path into a relative path. -/// -/// For example, the executable path might have the absolute path: -/// "/build/lknys4lnckh88mxvi7pba1zsvgfyh1a1-source/assets/header.svg -/// -/// And we need a relative path to the source directory: -/// "assets/header.svg" -fn turn_asset_path_into_relative_path(asset_path: &Path) -> PathBuf { - let components = asset_path - .components() - .skip_while(|c| c.as_os_str() != "assets") - .collect::>(); - - components.iter().fold(PathBuf::new(), |mut acc, c| { - acc.push(c); - acc - }) -} diff --git a/packages/manganis/manganis-core/src/asset.rs b/packages/manganis/manganis-core/src/asset.rs index 97ceb6ef7e..e2a809ed02 100644 --- a/packages/manganis/manganis-core/src/asset.rs +++ b/packages/manganis/manganis-core/src/asset.rs @@ -48,9 +48,6 @@ impl BundledAsset { /// Create a new asset but with a relative path /// /// This method is deprecated and will be removed in a future release. - #[deprecated( - note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`" - )] pub const fn new_relative( absolute_source_path: &'static str, bundled_path: &'static str, diff --git a/packages/manganis/manganis-macro/src/asset.rs b/packages/manganis/manganis-macro/src/asset.rs index 5774c37d4c..36eb92e720 100644 --- a/packages/manganis/manganis-macro/src/asset.rs +++ b/packages/manganis/manganis-macro/src/asset.rs @@ -40,14 +40,10 @@ fn resolve_path(raw: &str) -> Result { }); }; - // 4. Ensure the path doesn't escape the crate dir - // - // - Note: since we called canonicalize on both paths, we can safely compare the parent dirs. - // On windows, we can only compare the prefix if both paths are canonicalized (not just absolute) - // https://github.com/rust-lang/rust/issues/42869 - if path == manifest_dir || !path.starts_with(manifest_dir) { + // 4. Strip the manifest dir from the path + let Ok(path) = path.strip_prefix(&manifest_dir).map(|x| x.to_path_buf()) else { return Err(AssetParseError::InvalidPath { path }); - } + }; Ok(path) }