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

check wasm-bindgen version if running with target web #684

Merged
merged 12 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log = "0.4.6"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
reqwest = "0.9.14"
semver = "0.9.0"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_ignored = "0.0.4"
Expand Down
27 changes: 23 additions & 4 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use binary_install::Download;
use child;
use command::build::{BuildProfile, Target};
use failure::{self, ResultExt};
use install;
use manifest::CrateData;
use std::path::Path;
use semver;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
Expand Down Expand Up @@ -38,14 +40,21 @@ pub fn wasm_bindgen_build(
} else {
"--typescript"
};
let bindgen_path = bindgen.binary("wasm-bindgen")?;
let target_arg = match target {
Target::Nodejs => "--nodejs",
Target::NoModules => "--no-modules",
Target::Web => "--web",
Target::Web => {
if supports_web_target(&bindgen_path)? {
"--web"
} else {
bail!("Your current version of wasm-bindgen does not support the 'web' target. Please update your project to wasm-bindgen version >= 0.2.39.")
}
}
Target::Bundler => "--browser",
};
let bindgen_path = bindgen.binary("wasm-bindgen")?;
let mut cmd = Command::new(bindgen_path);

let mut cmd = Command::new(&bindgen_path);
cmd.arg(&wasm_path)
.arg("--out-dir")
.arg(out_dir)
Expand All @@ -70,3 +79,13 @@ pub fn wasm_bindgen_build(
child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
let cli_version = semver::Version::parse(&install::get_cli_version(
&install::Tool::WasmBindgen,
cli_path,
)?)?;
let expected_version = semver::Version::parse("0.2.39")?;
Ok(cli_version >= expected_version)
}
2 changes: 1 addition & 1 deletion src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn new_command(program: &str) -> Command {
}
}

/// Run the given command and return its stdout.
/// Run the given command and return on success.
pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
info!("Running {:?}", command);

Expand Down
34 changes: 17 additions & 17 deletions src/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn download_prebuilt_or_cargo_install(
}

/// Check if the tool dependency is locally satisfied.
fn check_version(
pub fn check_version(
tool: &Tool,
path: &PathBuf,
expected_version: &str,
Expand All @@ -75,24 +75,24 @@ fn check_version(
expected_version.to_string()
};

let v = get_cli_version(tool, path)?;
info!(
"Checking installed `{}` version == expected version: {} == {}",
tool, v, &expected_version
);
Ok(v == expected_version)
}

/// Fetches the version of a CLI tool
pub fn get_cli_version(tool: &Tool, path: &PathBuf) -> Result<String, failure::Error> {
let mut cmd = Command::new(path);
cmd.arg("--version");
child::run_capture_stdout(cmd, tool)
.map(|stdout| {
stdout
.trim()
.split_whitespace()
.nth(1)
.map(|v| {
info!(
"Checking installed `{}` version == expected version: {} == {}",
tool, v, &expected_version
);
Ok(v == expected_version)
})
.unwrap_or(Ok(false))
})
.unwrap_or(Ok(false))
let stdout = child::run_capture_stdout(cmd, tool)?;
let version = stdout.trim().split_whitespace().nth(1);
match version {
Some(v) => Ok(v.to_string()),
None => bail!("Something went wrong! We couldn't determine your version of the wasm-bindgen CLI. We were supposed to set that up for you, so it's likely not your fault! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md.")
}
}

/// Downloads a precompiled copy of the tool, if available.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate strsim;
extern crate failure;
extern crate glob;
extern crate parking_lot;
extern crate semver;
extern crate serde;
extern crate which;
#[macro_use]
Expand Down
4 changes: 2 additions & 2 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ impl CrateData {
.position(|pkg| pkg.name == manifest.package.name)
.ok_or_else(|| format_err!("failed to find package in metadata"))?;

return Ok(CrateData {
Ok(CrateData {
data,
manifest,
current_idx,
out_name,
});
})
}

/// Read the `manifest_path` file and deserializes it using the toml Deserializer.
Expand Down
48 changes: 48 additions & 0 deletions tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,54 @@ fn renamed_crate_name_works() {
fixture.wasm_pack().arg("build").assert().success();
}

#[test]
fn dash_dash_web_target_has_error_on_old_bindgen() {
let fixture = utils::fixture::Fixture::new();
fixture
.readme()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []

[lib]
crate-type = ["cdylib"]
name = 'bar'

[dependencies]
wasm-bindgen = "=0.2.37"
"#,
)
.file(
"src/lib.rs",
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn one() -> u32 { 1 }
"#,
)
.install_local_wasm_bindgen();
let cmd = fixture
.wasm_pack()
.arg("build")
.arg("--target")
.arg("web")
.assert()
.failure();
let output = String::from_utf8(cmd.get_output().stderr.clone()).unwrap();

assert!(
output.contains("0.2.39"),
"Output did not contain '0.2.39', output was {}",
output
);
}

#[test]
fn it_should_build_nested_project_with_transitive_dependencies() {
let fixture = utils::fixture::transitive_dependencies();
Expand Down