Skip to content

Commit

Permalink
fix: store relative paths in executable
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmay committed Dec 27, 2024
1 parent bcbb401 commit 0a0d525
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
27 changes: 2 additions & 25 deletions packages/cli/src/cli/build_assets.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {} --> {} {:#?}",
Expand All @@ -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::<Vec<_>>();

components.iter().fold(PathBuf::new(), |mut acc, c| {
acc.push(c);
acc
})
}
3 changes: 0 additions & 3 deletions packages/manganis/manganis-core/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 3 additions & 7 deletions packages/manganis/manganis-macro/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
});
};

// 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)
}
Expand Down

0 comments on commit 0a0d525

Please sign in to comment.