Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

v0.11.0 Locally installed wasm-opt not found #1247

Closed
nakamurarts opened this issue Mar 21, 2023 · 5 comments · Fixed by #1257
Closed

v0.11.0 Locally installed wasm-opt not found #1247

nakamurarts opened this issue Mar 21, 2023 · 5 comments · Fixed by #1257

Comments

@nakamurarts
Copy link

🐛 Bug description

When I run the command wasm-pack build --target web, I get an error that says wasm-opt is not found, even though wasm-opt exists in PATH.

The situation is similar to #1062, but the version (and possibly the cause) is different, so I have started a new issue.

See also: #1062 (comment) #1062 (comment)

🤔 Expected Behavior

The installed wasm-opt will be used

👟 Steps to reproduce

  1. run cargo new --lib hello-wasm and implement a simple project hello-wasm following MDN tutorial

  2. run wasm-pack build --target web and get the following error

> wasm-pack build --target web
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
  Finished release [optimized] target(s) in 0.03s
[WARN]: :-) origin crate has no README
[INFO]: Installing wasm-bindgen...
[INFO]: found wasm-opt at "C:\\Users\\user\\scoop\\shims\\wasm-opt.exe"
Error: C:\Users\user\scoop\shims\bin/wasm-opt.exe binary does not exist
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
Caused by: C:\Users\user\scoop\shims\bin/wasm-opt.exe binary does not exist
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

🌍 Your environment

on Windows 10:

> rustc --version
rustc 1.68.0 (2c8cc3432 2023-03-06)

> wasm-pack --version
wasm-pack 0.11.0

> wasm-opt --version
wasm-opt version 112 (version_112)

on Fedora 37:

$ rustc --version
rustc 1.67.1 (d5a82bbd2 2023-02-07)

$ wasm-pack --version
wasm-pack 0.11.0

$ wasm-opt --version
wasm-opt version 110
@nakamurarts
Copy link
Author

nakamurarts commented Mar 21, 2023

ad hoc fix (still passes all cargo tests in my environment):

(src/wasm_opt.rs)

//! Support for downloading and executing `wasm-opt`

use crate::child;
use crate::install;
use crate::PBAR;
use anyhow::Result;
use binary_install::Cache;
use std::path::Path;
use std::process::Command;

/// Execute `wasm-opt` over wasm binaries found in `out_dir`, downloading if
/// necessary into `cache`. Passes `args` to each invocation of `wasm-opt`.
pub fn run(cache: &Cache, out_dir: &Path, args: &[String], install_permitted: bool) -> Result<()> {
    let wasm_opt_path = match find_wasm_opt(cache, install_permitted)? {
        Status::Found(path) => path,
        Status::Install(status) => match status {
            install::Status::Found(dl) => dl.binary("bin/wasm-opt")?,
            install::Status::CannotInstall => {
                PBAR.info("Skipping wasm-opt as no downloading was requested");
                return Ok(());
            }
            install::Status::PlatformNotSupported => {
                PBAR.info("Skipping wasm-opt because it is not supported on this platform");
                return Ok(());
            }
        },
    };

    PBAR.info("Optimizing wasm binaries with `wasm-opt`...");

    for file in out_dir.read_dir()? {
        let file = file?;
        let path = file.path();
        if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
            continue;
        }

        let tmp = path.with_extension("wasm-opt.wasm");
        let mut cmd = Command::new(&wasm_opt_path);
        cmd.arg(&path).arg("-o").arg(&tmp).args(args);
        child::run(cmd, "wasm-opt")?;
        std::fs::rename(&tmp, &path)?;
    }

    Ok(())
}

/// Possible outcomes of attempting to find/install `wasm-opt`
pub enum Status {
    /// We found `wasm-opt` at the specified path (in PATH)
    Found(std::path::PathBuf),
    /// Installation results
    Install(install::Status),
}

/// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a
/// precompiled binary.
///
/// Returns `Some` if a binary was found or it was successfully downloaded.
/// Returns `None` if a binary wasn't found in `PATH` and this platform doesn't
/// have precompiled binaries. Returns an error if we failed to download the
/// binary.
pub fn find_wasm_opt(cache: &Cache, install_permitted: bool) -> Result<Status> {
    // First attempt to look up in PATH. If found assume it works.
    if let Ok(path) = which::which("wasm-opt") {
        PBAR.info(&format!("found wasm-opt at {:?}", path));
        return Ok(Status::Found(path));
    }

    Ok(Status::Install(install::download_prebuilt(
        &install::Tool::WasmOpt,
        cache,
        "latest",
        install_permitted,
    )?))
}

@tmpfs
Copy link

tmpfs commented Mar 21, 2023

Same issue here on MacOS with 0.11:

[INFO]: found wasm-opt at "/opt/homebrew/bin/wasm-opt"
Error: /opt/homebrew/bin/bin/wasm-opt binary does not exist

Looks like it is adding an extra bin to the path for some reason.

Going to downgrade back to 0.10.3 for now.

@mogtofu33
Copy link

Temporary fix is to symlink ~/bin/wasm-opt to ~/bin/bin/wasm-opt

Liamolucko added a commit to Liamolucko/wasm-bindgen that referenced this issue Mar 24, 2023
This is a workaround for rustwasm/wasm-pack#1247, which causes `wasm-pack` to fail to run if a local version of `wasm-opt` is installed. That in turn caused the `build_examples` CI job to break.

This PR fixes that by not having it download binaryen, instead letting `wasm-pack` install it itself, which still works.
alexcrichton pushed a commit to rustwasm/wasm-bindgen that referenced this issue Mar 24, 2023
* Don't install binaryen in CI

This is a workaround for rustwasm/wasm-pack#1247, which causes `wasm-pack` to fail to run if a local version of `wasm-opt` is installed. That in turn caused the `build_examples` CI job to break.

This PR fixes that by not having it download binaryen, instead letting `wasm-pack` install it itself, which still works.

* Put back the binaryen-installing step, but only install wasm2js instead of the whole thing.

I also updated to binaryen 112 while I was at it.
Liamolucko added a commit to Liamolucko/wasm-pack that referenced this issue Mar 26, 2023
nekofar added a commit to lilnouns/lilnouns-bots that referenced this issue Sep 19, 2023
Added wasm-pack configuration and necessary dependencies to enhance the packaging of rust code to WebAssembly. This change was made due to rustwasm/wasm-pack#1247. Additionally, included 'worker' as a new dependency and adjusted the release profile to optimize for size and performance.
nekofar added a commit to lilnouns/lilnouns-bots that referenced this issue Sep 19, 2023
Added wasm-pack configuration and necessary dependencies to enhance the packaging of rust code to WebAssembly. This change was made due to rustwasm/wasm-pack#1247. Additionally, included 'worker' as a new dependency and adjusted the release profile to optimize for size and performance.
JohnFredok added a commit to JohnFredok/lilnouns-bots that referenced this issue Oct 24, 2023
Added wasm-pack configuration and necessary dependencies to enhance the packaging of rust code to WebAssembly. This change was made due to rustwasm/wasm-pack#1247. Additionally, included 'worker' as a new dependency and adjusted the release profile to optimize for size and performance.
zaphar added a commit to zaphar/kitchen that referenced this issue Nov 27, 2023
Motivated by this bug in v0.11.0:
rustwasm/wasm-pack#1247
@dylanh724
Copy link

This issue still persists on Windows to this day, @drager

@drager
Copy link
Member

drager commented Jul 1, 2024

This issue still persists on Windows to this day, @drager

Still an issue in 0.13.0? If so, please open a new issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants