diff --git a/src/commands/new.rs b/src/commands/new.rs index 98eb1c4d..467afdae 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -14,11 +14,10 @@ use std::{ path::{Path, PathBuf}, process::Command, }; -use toml_edit::{table, value, Array, Document, InlineTable, Item, Table, Value}; +use toml_edit::{table, value, Document, Item, Table, Value}; use url::Url; const WIT_BINDGEN_CRATE: &str = "wit-bindgen"; -const WIT_BINDGEN_VERSION: &str = "0.16.0"; fn escape_wit(s: &str) -> Cow { match s { @@ -282,13 +281,7 @@ impl NewCommand { metadata.set_implicit(true); metadata.set_position(doc.len()); metadata["component"] = Item::Table(component); - doc["package"]["metadata"] = Item::Table(metadata); - doc["dependencies"][WIT_BINDGEN_CRATE] = value(InlineTable::from_iter([ - ("version", Value::from(WIT_BINDGEN_VERSION)), - ("default-features", Value::from(false)), - ("features", Value::from(Array::from_iter(["realloc"]))), - ])); fs::write(&manifest_path, doc.to_string()).with_context(|| { format!( @@ -297,6 +290,20 @@ impl NewCommand { ) })?; + // Run cargo add cargo-component-bindings + let mut cargo_add_command = std::process::Command::new("cargo"); + cargo_add_command.arg("add"); + cargo_add_command.arg("--quiet"); + cargo_add_command.arg("--no-default-features"); + cargo_add_command.arg("--features"); + cargo_add_command.arg("realloc"); + cargo_add_command.arg(WIT_BINDGEN_CRATE); + cargo_add_command.current_dir(out_dir); + let status = cargo_add_command.status().context("failed to execute `cargo add` command")?; + if !status.success() { + bail!("`cargo add` command exited with non-zero status"); + } + config.terminal().status( "Updated", format!("manifest of package `{name}`", name = name.display), diff --git a/tests/build.rs b/tests/build.rs index ea5f979f..03985cd0 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -50,13 +50,6 @@ fn it_builds_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -82,6 +75,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the projects have been created + project.file( + "Cargo.toml", + r#"[workspace] + members = ["foo", "bar", "baz"] + "#, + )?; + project .cargo_component("build") .assert() diff --git a/tests/check.rs b/tests/check.rs index 8284dfea..54359016 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -46,13 +46,6 @@ fn it_checks_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -78,6 +71,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] + members = ["foo", "bar", "baz"] + "#, + )?; + project .cargo_component("check") .assert() diff --git a/tests/clippy.rs b/tests/clippy.rs index 8331269d..75f77330 100644 --- a/tests/clippy.rs +++ b/tests/clippy.rs @@ -67,13 +67,6 @@ fn it_checks_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -99,6 +92,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] +members = ["foo", "bar", "baz"] +"#, + )?; + project .cargo_component("clippy") .assert() diff --git a/tests/metadata.rs b/tests/metadata.rs index 2fb09026..e233dd1f 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -44,13 +44,6 @@ fn it_prints_workspace_metadata() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -76,6 +69,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] +members = ["foo", "bar", "baz"] +"#, + )?; + project .cargo_component("metadata --format-version 1") .assert()