Skip to content

Use BEVY_ASSET_ROOT & CARGO_MANIFEST_DIR to load yarn file#274

Merged
janhohenheim merged 2 commits intoYarnSpinnerTool:mainfrom
Galdormin:fix_asset_root
Feb 2, 2026
Merged

Use BEVY_ASSET_ROOT & CARGO_MANIFEST_DIR to load yarn file#274
janhohenheim merged 2 commits intoYarnSpinnerTool:mainfrom
Galdormin:fix_asset_root

Conversation

@Galdormin
Copy link
Copy Markdown
Contributor

To solve #271, register_asser_root needs to take into account the absolute path that may be included through BEVY_ASSET_ROOT or CARGO_MANIFEST_DIR.

The only way I found to solve this is to use the FileAssetReader which is not available in wasm32 builds.

Or we could integrate part of get_base_path inside register_asset_root() as in:

fn register_asset_root(&mut self) -> &mut Self {
    let asset_plugin = get_asset_plugin(self);
    let path_str = asset_plugin.file_path.clone();

    let path = if let Ok(manifest_dir) = env::var("BEVY_ASSET_ROOT") {
        let mut root_path = PathBuf::from(manifest_dir);
        root_path.push(path_str);
        root_path
    } else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
        let mut root_path = PathBuf::from(manifest_dir);
        root_path.push(path_str);
        root_path
    } else {
        PathBuf::from(path_str)
    };

    self.insert_resource(AssetRoot(path))
}

However, I'm not an expert in wasm32 build and I don't know if std::env is available there.

Which version do you prefer ?

@janhohenheim
Copy link
Copy Markdown
Collaborator

@andriyDev mind helping me out here for a second?

Copy link
Copy Markdown

@andriyDev andriyDev left a comment

Choose a reason for hiding this comment

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

Overall I think this is fine! I think calling get_base_path is better than forking this for sure (to stay in sync).

Comment thread crates/bevy_plugin/src/plugin.rs Outdated
#[cfg(not(target_arch = "wasm32"))]
fn register_asset_root(&mut self) -> &mut Self {
let asset_plugin = get_asset_plugin(self);
let mut path = FileAssetReader::get_base_path();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we only conditionally compile these two lines instead of the whole function? I think it's simpler that way (than acting like these are totally different).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Something like this ?

fn register_asset_root(&mut self) -> &mut Self {
    let asset_plugin = get_asset_plugin(self);
    let path_str = asset_plugin.file_path.clone();

    #[cfg(not(target_arch = "wasm32"))]
    let path = FileAssetReader::get_base_path().join(path_str);
    #[cfg(target_arch = "wasm32")]
    let path = PathBuf::from(path_str);

    self.insert_resource(AssetRoot(path))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah I think that'll work!

@Galdormin
Copy link
Copy Markdown
Contributor Author

Not tested on wasm32 though

@janhohenheim janhohenheim merged commit 094c846 into YarnSpinnerTool:main Feb 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants