diff --git a/Cargo.lock b/Cargo.lock index 7b20a0fc2..7cc84310d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -501,6 +501,22 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "path-absolutize" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "path-dedot 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "path-dedot" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pkg-config" version = "0.3.14" @@ -969,6 +985,7 @@ dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "path-absolutize 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1106,6 +1123,8 @@ dependencies = [ "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" +"checksum path-absolutize 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd20546730045b90f32bf46ecaf86a825c6fe1c912be34e0b5828d0829acec7" +"checksum path-dedot 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d6c852f9c1fd5acf5ce337fc80323cc8d5232fb3fa0976be41817da2d980b47" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum podio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "780fb4b6698bbf9cf2444ea5d22411cef2953f0824b98f33cf454ec5615645bd" "checksum proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "3d7b7eaaa90b4a90a932a9ea6666c95a389e424eff347f0f793979289429feee" diff --git a/Cargo.toml b/Cargo.toml index 56392cfeb..9cd9050d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ indicatif = "0.9.0" lazy_static = "1.1.0" openssl = { version = '0.10.11', optional = true } parking_lot = "0.6" +path-absolutize = "1.1.1" serde = "1.0.74" serde_derive = "1.0.74" serde_json = "1.0.26" diff --git a/src/command/utils.rs b/src/command/utils.rs index 1e49d8532..8879b878e 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -2,21 +2,21 @@ use emoji; use failure; +use path_absolutize::*; use progressbar::Step; use std::fs; -use std::io; use std::path::{Path, PathBuf}; use PBAR; /// If an explicit path is given, then use it, otherwise assume the current /// directory is the crate path. -pub fn set_crate_path(path: Option) -> io::Result { +pub fn set_crate_path(path: Option) -> Result { let crate_path = match path { Some(p) => p, None => PathBuf::from("."), }; - - crate_path.canonicalize() + let absolute_crate_path = crate_path.absolutize()?; + Ok(absolute_crate_path) } /// Construct our `pkg` directory in the crate. diff --git a/src/lib.rs b/src/lib.rs index ed2e390e1..830b6c405 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ extern crate indicatif; #[macro_use] extern crate lazy_static; extern crate parking_lot; +extern crate path_absolutize; #[macro_use] extern crate serde_derive; extern crate serde_json; diff --git a/tests/all/build.rs b/tests/all/build.rs index 240a1a2f8..ee98b4961 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -35,3 +35,33 @@ fn it_should_build_js_hello_world_example() { command::run_wasm_pack(cli.cmd, &logger) .expect("running wasm-pack in a js-hello-world directory should succeed."); } + +#[test] +fn it_should_build_nested_project_with_transitive_dependencies() { + let fixture = utils::fixture::transitive_dependencies(); + fixture.install_local_wasm_bindgen(); + let cli = Cli::from_iter_safe(vec![ + "wasm-pack", + "build", + &fixture.path.join("main").display().to_string(), + ]) + .unwrap(); + let logger = logger::new(&cli.cmd, cli.verbosity).unwrap(); + command::run_wasm_pack(cli.cmd, &logger) + .expect("running wasm-pack in a project with transitive dependencies should succeed."); +} + +#[test] +fn it_should_build_project_with_dependency_on_crc_crate() { + let fixture = utils::fixture::project_with_crc(); + fixture.install_local_wasm_bindgen(); + let cli = Cli::from_iter_safe(vec![ + "wasm-pack", + "build", + &fixture.path.display().to_string(), + ]) + .unwrap(); + let logger = logger::new(&cli.cmd, cli.verbosity).unwrap(); + command::run_wasm_pack(cli.cmd, &logger) + .expect("running wasm-pack in a project with dependency on crc crate should succeed."); +} diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index dc4736929..d54fede97 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -474,3 +474,207 @@ pub fn wbg_test_node() -> Fixture { ); fixture } + +pub fn transitive_dependencies() -> Fixture { + fn project_main_fixture(fixture: &mut Fixture) { + fixture.file(PathBuf::from("main/README"), "# Main Fixture\n"); + fixture.file( + PathBuf::from("main/Cargo.toml"), + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + license = "WTFPL" + name = "main_project" + repository = "https://github.com/rustwasm/wasm-pack.git" + version = "0.1.0" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "=0.2.21" + project_a = { path = "../project_a" } + project_b = { path = "../project_b" } + + [dev-dependencies] + wasm-bindgen-test = "=0.2.21" + "#, + ); + fixture.file( + PathBuf::from("main/src/lib.rs"), + r#" + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + + // Import the `window.alert` function from the Web. + #[wasm_bindgen] + extern { + fn alert(s: &str); + } + + // Export a `greet` function from Rust to JavaScript, that alerts a + // hello message. + #[wasm_bindgen] + pub fn greet(name: &str) { + alert(&format!("Hello, {}!", name)); + } + "#, + ); + } + + fn project_a_fixture(fixture: &mut Fixture) { + fixture.file( + PathBuf::from("project_a/README"), + "# Project Alpha Fixture\n", + ); + fixture.file( + PathBuf::from("project_a/Cargo.toml"), + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + license = "WTFPL" + name = "project_a" + repository = "https://github.com/rustwasm/wasm-pack.git" + version = "0.1.0" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "=0.2.21" + project_b = { path = "../project_b" } + + [dev-dependencies] + wasm-bindgen-test = "=0.2.21" + "#, + ); + fixture.file( + PathBuf::from("project_a/src/lib.rs"), + r#" + extern crate wasm_bindgen; + // extern crate project_b; + use wasm_bindgen::prelude::*; + + // Import the `window.alert` function from the Web. + #[wasm_bindgen] + extern { + fn alert(s: &str); + } + + // Export a `greet` function from Rust to JavaScript, that alerts a + // hello message. + #[wasm_bindgen] + pub fn greet(name: &str) { + alert(&format!("Hello, {}!", name)); + } + "#, + ); + } + + fn project_b_fixture(fixture: &mut Fixture) { + fixture.file( + PathBuf::from("project_b/README"), + "# Project Beta Fixture\n", + ); + fixture.file( + PathBuf::from("project_b/Cargo.toml"), + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + license = "WTFPL" + name = "project_b" + repository = "https://github.com/rustwasm/wasm-pack.git" + version = "0.1.0" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "=0.2.21" + + [dev-dependencies] + wasm-bindgen-test = "=0.2.21" + "#, + ); + fixture.file( + PathBuf::from("project_b/src/lib.rs"), + r#" + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + + // Import the `window.alert` function from the Web. + #[wasm_bindgen] + extern { + fn alert(s: &str); + } + + // Export a `greet` function from Rust to JavaScript, that alerts a + // hello message. + #[wasm_bindgen] + pub fn greet(name: &str) { + alert(&format!("Hello, {}!", name)); + } + "#, + ); + } + + let mut fixture = Fixture::new(); + project_b_fixture(&mut fixture); + project_a_fixture(&mut fixture); + project_main_fixture(&mut fixture); + fixture +} + +pub fn project_with_crc() -> Fixture { + let fixture = Fixture::new(); + fixture.file(PathBuf::from("README"), "# Project Beta Fixture\n"); + fixture.file( + PathBuf::from("Cargo.toml"), + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + license = "WTFPL" + name = "proj" + repository = "https://github.com/rustwasm/wasm-pack.git" + version = "0.1.0" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "=0.2.21" + crc = "*" + + [dev-dependencies] + wasm-bindgen-test = "=0.2.21" + "#, + ); + fixture.file( + PathBuf::from("src/lib.rs"), + r#" + extern crate crc; + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use crc::crc16; + + // Import the `window.alert` function from the Web. + #[wasm_bindgen] + extern { + fn alert(s: &str); + } + + // Export a `greet` function from Rust to JavaScript, that alerts a + // hello message. + #[wasm_bindgen] + pub fn greet(name: &str) { + assert_eq!(crc16::checksum_x25(b"123456789"), 0x906e); + alert(&format!("Hello, {}!", name)); + } + "#, + ); + fixture +}