Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Respect build target from cargo config #319

Merged
merged 1 commit into from
Jul 9, 2024
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
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