From 7c5c549f9052189f753a47560f329b90c16fbe57 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Sat, 19 Aug 2023 09:32:41 +0400 Subject: [PATCH] Fix panic when using `.load_folder()` with absolute paths On case-insensitive filesystems (Windows, Mac, NTFS mounted in Linux, etc.), a path can be represented in a multiple ways: - `c:\users\user\rust\assets\hello\world` - `c:/users/user/rust/assets/hello/world` - `C:\USERS\USER\rust\assets\hello\world` If user specifies a path variant that doesn't match asset folder path bevy calculates, `path.strip_prefix()` will fail, as demonstrated below: ```rs dbg!(Path::new("c:/foo/bar/baz").strip_prefix("c:/foo")); // Ok("bar/baz") dbg!(Path::new("c:/FOO/bar/baz").strip_prefix("c:/foo")); // StripPrefixError(()) ``` This commit rewrites the code in question in a way that prefix stripping is no longer necessary. --- crates/bevy_asset/src/io/file_asset_io.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/bevy_asset/src/io/file_asset_io.rs b/crates/bevy_asset/src/io/file_asset_io.rs index 9dabb1b1b5a94..842ce53775846 100644 --- a/crates/bevy_asset/src/io/file_asset_io.rs +++ b/crates/bevy_asset/src/io/file_asset_io.rs @@ -114,10 +114,11 @@ impl AssetIo for FileAssetIo { path: &Path, ) -> Result>, AssetIoError> { let root_path = self.root_path.to_owned(); - Ok(Box::new(fs::read_dir(root_path.join(path))?.map( + let path = path.to_owned(); + Ok(Box::new(fs::read_dir(root_path.join(&path))?.map( move |entry| { - let path = entry.unwrap().path(); - path.strip_prefix(&root_path).unwrap().to_owned() + let file_name = entry.unwrap().file_name(); + path.join(file_name) }, ))) }