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

embedded_asset! panics on unwrap during path manipulation #10377

Closed
bonsairobo opened this issue Nov 5, 2023 · 3 comments · Fixed by #10383
Closed

embedded_asset! panics on unwrap during path manipulation #10377

bonsairobo opened this issue Nov 5, 2023 · 3 comments · Fixed by #10383
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@bonsairobo
Copy link
Contributor

Bevy version

0.12.0

What you did

pub struct TriplanarMaterialPlugin;

impl Plugin for TriplanarMaterialPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(MaterialPlugin::<TriplanarMaterial>::default());

        embedded_asset!(app, "shaders/triplanar.wgsl");
        embedded_asset!(app, "shaders/biplanar.wgsl");
    }
}

What went wrong

thread 'main' panicked at src/plugin.rs:11:9:
called `Option::unwrap()` on a `None` value

Additional information

Root Cause

I believe the root cause it trying to split a path string by "/src/" when the path does not even contain that substring.

Here's the implementation:

macro_rules! embedded_path {
    ($path_str: expr) => {{
        embedded_path!("/src/", $path_str)
    }};

    ($source_path: expr, $path_str: expr) => {{
        let crate_name = module_path!().split(':').next().unwrap();
        let after_src = file!().split($source_path).nth(1).unwrap();
        let file_path = std::path::Path::new(after_src)
            .parent()
            .unwrap()
            .join($path_str);
        std::path::Path::new(crate_name).join(file_path)
    }};
}

This line is where the panic happens:

        let after_src = file!().split($source_path).nth(1).unwrap();

The issue is $source_path = "/src/" while file!() = "src/plugin.rs". Splitting will produce only one element from the iterator, so nth(1) will be None.

Solution

Generally it's a bad idea to do path manipulation with raw strings when you could use Path or PathBuf instead, and I recommend eliminating any use of "/" (it's not portable), e.g. in "/src/". So my suggested solution is to convert strings into PathBuf as soon as possible and do manipulation after that.

@bonsairobo bonsairobo added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Nov 5, 2023
@443eb9
Copy link

443eb9 commented Nov 5, 2023

I also find that. A pr about that is open #10379 by me.

@443eb9
Copy link

443eb9 commented Nov 5, 2023

It's such a annoying problem. I spent the whole morning trying to find the source. : (

@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 Nov 8, 2023
@MikMikus
Copy link

An example, temporary solution if you have problems with linux/windows compilation:

#[cfg(any(not(target_family = "windows"), target_env = "gnu"))]
{
   embedded_asset!(app, "src/", "fonts/Ubuntu-Light.ttf");
}

#[cfg(all(target_family = "windows", not(target_env = "gnu")))]
{
  embedded_asset!(app, "src\\", "fonts\\Ubuntu-Light.ttf");
}

ria8651 added a commit to ria8651/bevy-voxel-engine that referenced this issue Feb 1, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 2, 2024
# Objective

Fixes #10377

## Solution

Use `Path::strip_prefix` instead of `str::split`. Avoid any explicit "/"
characters in path manipulation.

---

## Changelog

- Added: example of embedded asset loading
- Added: support embedded assets in external crates
- Fixed: resolution of embedded assets
- Fixed: unexpected runtime panic during asset path resolution

## Migration Guide

No API changes.

---------

Co-authored-by: Shane Celis <[email protected]>
tjamaan pushed a commit to tjamaan/bevy that referenced this issue Feb 6, 2024
# Objective

Fixes bevyengine#10377

## Solution

Use `Path::strip_prefix` instead of `str::split`. Avoid any explicit "/"
characters in path manipulation.

---

## Changelog

- Added: example of embedded asset loading
- Added: support embedded assets in external crates
- Fixed: resolution of embedded assets
- Fixed: unexpected runtime panic during asset path resolution

## Migration Guide

No API changes.

---------

Co-authored-by: Shane Celis <[email protected]>
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

Successfully merging a pull request may close this issue.

4 participants