Skip to content

Commit

Permalink
Respect build target from cargo config (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 authored Jul 9, 2024
1 parent 9a32b9d commit 87d9021
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,20 @@ pub async fn run_cargo_command(
}
cargo.args(args);

// TODO: consider targets from .cargo/config.toml
let cargo_config = cargo_config2::Config::load()?;

// Handle the target for buildable commands
if command.buildable() {
install_wasm32_wasip1(config)?;

// Add an implicit wasm32-wasip1 target if there isn't a wasm target present
if !cargo_args.targets.iter().any(|t| is_wasm_target(t)) {
if !cargo_args.targets.iter().any(|t| is_wasm_target(t))
&& !cargo_config
.build
.target
.as_ref()
.is_some_and(|v| v.iter().any(|t| is_wasm_target(t.triple())))
{
cargo.arg("--target").arg("wasm32-wasip1");
}

Expand Down Expand Up @@ -235,7 +241,7 @@ pub async fn run_cargo_command(
}

let runner = if needs_runner && command.runnable() {
Some(get_runner(command == CargoCommand::Serve)?)
Some(get_runner(&cargo_config, command == CargoCommand::Serve)?)
} else {
None
};
Expand All @@ -259,9 +265,7 @@ pub async fn run_cargo_command(
Ok(outputs.into_iter().map(|o| o.path).collect())
}

fn get_runner(serve: bool) -> Result<PathAndArgs> {
let cargo_config = cargo_config2::Config::load()?;

fn get_runner(cargo_config: &cargo_config2::Config, serve: bool) -> Result<PathAndArgs> {
// We check here before we actually build that a runtime is present.
// We first check the runner for `wasm32-wasip1` in the order from
// cargo's convention for a user-supplied runtime (path or executable)
Expand Down
56 changes: 55 additions & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn it_adds_a_producers_field() -> Result<()> {
}

#[test]
fn it_builds_wasm32_unknown_unknown() -> Result<()> {
fn it_builds_wasm32_unknown_unknown_from_cli() -> Result<()> {
let project = Project::new("foo")?;

project
Expand All @@ -182,6 +182,60 @@ fn it_builds_wasm32_unknown_unknown() -> Result<()> {
Ok(())
}

#[test]
fn it_builds_wasm32_unknown_unknown_from_config() -> Result<()> {
let project = Project::new("foo")?;

project.file(
".cargo/config.toml",
r#"[build]
target = "wasm32-unknown-unknown"
"#,
)?;

project
.cargo_component("build")
.assert()
.stderr(contains(
"Finished `dev` profile [unoptimized + debuginfo] target(s)",
))
.success();

validate_component(
&project
.build_dir()
.join("wasm32-unknown-unknown")
.join("debug")
.join("foo.wasm"),
)?;

Ok(())
}

#[test]
fn it_builds_wasm32_unknown_unknown_from_env() -> Result<()> {
let project = Project::new("foo")?;

project
.cargo_component("build")
.env("CARGO_BUILD_TARGET", "wasm32-unknown-unknown")
.assert()
.stderr(contains(
"Finished `dev` profile [unoptimized + debuginfo] target(s)",
))
.success();

validate_component(
&project
.build_dir()
.join("wasm32-unknown-unknown")
.join("debug")
.join("foo.wasm"),
)?;

Ok(())
}

#[test]
fn it_regenerates_target_if_wit_changed() -> Result<()> {
let project = Project::new("foo")?;
Expand Down

0 comments on commit 87d9021

Please sign in to comment.