Skip to content

Commit 94c67af

Browse files
authored
Fix panic when using .load_folder() with absolute paths (#9490)
Fixes #9458. 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. I've tested with the following paths on my computer: ```rs let res = asset_server.load_folder("C:\\Users\\user\\rust\\assets\\foo\\bar"); dbg!(res); let res = asset_server.load_folder("c:\\users\\user\\rust\\assets\\foo\\bar"); dbg!(res); let res = asset_server.load_folder("C:/Users/user/rust/assets/foo/bar"); dbg!(res); ```
1 parent a2bd93a commit 94c67af

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

crates/bevy_asset/src/io/file_asset_io.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ impl AssetIo for FileAssetIo {
114114
path: &Path,
115115
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> {
116116
let root_path = self.root_path.to_owned();
117-
Ok(Box::new(fs::read_dir(root_path.join(path))?.map(
117+
let path = path.to_owned();
118+
Ok(Box::new(fs::read_dir(root_path.join(&path))?.map(
118119
move |entry| {
119-
let path = entry.unwrap().path();
120-
path.strip_prefix(&root_path).unwrap().to_owned()
120+
let file_name = entry.unwrap().file_name();
121+
path.join(file_name)
121122
},
122123
)))
123124
}

0 commit comments

Comments
 (0)