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

Panic on Loading Asset from Absolute Path #11271

Open
HulloImJay opened this issue Jan 9, 2024 · 2 comments
Open

Panic on Loading Asset from Absolute Path #11271

HulloImJay opened this issue Jan 9, 2024 · 2 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@HulloImJay
Copy link

HulloImJay commented Jan 9, 2024

Bevy version

0.12.1
Also tested on commit hash: 41c3620

Relevant system information

Dell Precision 7670
Windows 11 Enterprise (10.0.22621 Build 22621)

What you did

I've been trying to load assets from absolute file paths (I'm making a small GLTF viewer which does not come pre-packaged with its models). Loading with asset_server.load() (where asset_server is Res) worked okay in Bevy 0.11 but in 0.12 I hit problems.

For example:

commands.spawn((
    SceneBundle {
        // scene: asset_server.load(r"C:\Users\user\my-proj\assets\models\model.glb#Scene0"), // throws error
        scene: asset_server.load(r"models\model.glb#Scene0"), // works!
        ..default()
    },
    ModelInstance {},
));

I also tested this against absolute paths provided from drag-and-drop events.

What went wrong

I expected the two load() lines above to be equivalent, but if I use the one with the absolute path I get the following error and my application panics:

thread 'Compute Task Pool (15)' panicked at C:\Users\koceom0\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_asset-0.12.0\src\path.rs:458:70:
called `Result::unwrap()` on an `Err` value: InvalidSourceSyntax

I don't see anything about this specifically in the migration guide, so I'm assuming this is not expected behaviour.

Additional information

I was able to work around this issue by using the pathdiff crate to convert the absolute path into one which is relative to the current working directory. With this work-around it seems I'm able to load assets from anywhere on the system, confirming for me that issue above is not an access one.

@HulloImJay HulloImJay added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jan 9, 2024
@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds and removed S-Needs-Triage This issue needs to be labelled labels Jan 9, 2024
@BD103
Copy link
Member

BD103 commented Jan 14, 2024

After looking at the line number from the panic message, I believe it is raised in AssetPath::parse_internal.

':' => {
let (_, char) = chars
.next()
.ok_or(ParseAssetPathError::InvalidSourceSyntax)?;
if char != '/' {
return Err(ParseAssetPathError::InvalidSourceSyntax);
}
let (index, char) = chars
.next()
.ok_or(ParseAssetPathError::InvalidSourceSyntax)?;
if char != '/' {
return Err(ParseAssetPathError::InvalidSourceSyntax);
}

My guess is that the code does not like the C:\ syntax at the beginning of the path. I think it was designed to extract the :// syntax commonly seen in URLs, but got tripped up when it saw the backslash.

@BD103
Copy link
Member

BD103 commented Jan 14, 2024

So after some testing, absolute paths still seem to work on Unix-based systems, just not Windows.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(SpriteBundle {
        texture: asset_server.load("/Users/bd103/Downloads/random_image.png"),
        ..default()
    });
}

I think an important to ask is whether you should load absolute paths outside of the assets/ folder. I could imagine some code making incorrect assumptions and breaking, such as what you just encountered.

On the other hand, Bevy doesn't really provide any other good way to load user-provided paths (such as through drag-and-drop). It seems to shame to have to manually call fs::open when a lot of useful logic is already pre-defined.

Edit: After talking on Discord, it seems like maybe a custom asset source should be created for paths outside of the assets folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants