Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw error when asset is empty #3379

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions packages/manganis/manganis-macro/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
let input = PathBuf::from(raw);

// 2. absolute path to the asset
manifest_dir
.join(raw.trim_start_matches('/'))
.canonicalize()
.map_err(|err| AssetParseError::AssetDoesntExist {
err,
let Ok(path) = std::path::absolute(manifest_dir.join(raw.trim_start_matches('/'))) else {
return Err(AssetParseError::InvalidPath {
path: input.clone(),
})
});
};

// 3. Ensure the path doesn't escape the crate dir
if path == manifest_dir || !path.starts_with(manifest_dir) {
return Err(AssetParseError::InvalidPath { path });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, this case triggers which gives the wrong error message

}

// 4. Ensure the path exists
if !path.exists() {
return Err(AssetParseError::AssetDoesntExist {
path: input.clone(),
});
}

Ok(path)
}

fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {
Expand All @@ -40,11 +52,10 @@ fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {

// If this is a folder, hash the folder contents
if file_path.is_dir() {
let files =
std::fs::read_dir(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
let files = std::fs::read_dir(file_path).map_err(|err| AssetParseError::IoError {
err,
path: file_path.to_path_buf(),
})?;
for file in files.flatten() {
let path = file.path();
hash_file_contents(&path)?;
Expand All @@ -53,11 +64,10 @@ fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {
}

// Otherwise, open the file to get its contents
let mut file =
std::fs::File::open(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
let mut file = std::fs::File::open(file_path).map_err(|err| AssetParseError::IoError {
err,
path: file_path.to_path_buf(),
})?;

// We add a hash to the end of the file so it is invalidated when the bundled version of the file changes
// The hash includes the file contents, the options, and the version of manganis. From the macro, we just
Expand All @@ -78,18 +88,27 @@ fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {

#[derive(Debug)]
pub(crate) enum AssetParseError {
AssetDoesntExist {
err: std::io::Error,
path: std::path::PathBuf,
},
AssetDoesntExist { path: PathBuf },
IoError { err: std::io::Error, path: PathBuf },
InvalidPath { path: PathBuf },
FailedToReadAsset(std::io::Error),
}

impl std::fmt::Display for AssetParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetParseError::AssetDoesntExist { err, path } => {
write!(f, "Asset at {} doesn't exist: {}", path.display(), err)
AssetParseError::AssetDoesntExist { path } => {
write!(f, "Asset at {} doesn't exist", path.display())
}
AssetParseError::IoError { path, err } => {
write!(f, "Failed to read file: {}; {}", path.display(), err)
}
AssetParseError::InvalidPath { path } => {
write!(
f,
"Asset path {} is invalid. Make sure the asset exists within this crate.",
path.display()
)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
}
Expand Down
Loading