Skip to content

Commit 2725dac

Browse files
authored
Fix invalid artifact directory when customizing target (#481)
# Objective The Bevy CLI allows you to configure a custom compilation target in `Cargo.toml`: ```toml [package.metadata.bevy_cli.web] target = "wasm32v1-none" ``` However, `bevy run web` doesn't work with this configuration, because it searches for the Wasm binary in the `wasm32-unknown-unknown` target folder. # Solution The cause of this problem is that we have to run the binary selection _before_ loading the `Cargo.toml` config, to know which package to pull the config from. But the config can then alter the contents of the `BinTarget`, most notably the `artifact_directory`. As a fix, we run the bin target selection again after loading the config, which fixes the problem. # Testing You can test via the [`no_std` package in the bevy_complex_repo](https://github.com/TimJentzsch/bevy_complex_repo/tree/main/no_std). It doesn't fully work yet, but at least it should compile now without an error.
1 parent febf8d2 commit 2725dac

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/bin_target.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ pub struct BinTarget<'p> {
1616
pub bin_name: String,
1717
}
1818

19+
impl BinTarget<'_> {
20+
pub fn update_artifact_directory(
21+
&mut self,
22+
target_directory: impl Into<PathBuf>,
23+
compile_target: Option<&str>,
24+
compile_profile: &str,
25+
is_example: bool,
26+
) {
27+
self.artifact_directory = get_artifact_directory(
28+
target_directory,
29+
compile_target,
30+
compile_profile,
31+
is_example,
32+
);
33+
}
34+
}
35+
1936
/// Determine which binary target should be run.
2037
///
2138
/// The `--package` arg narrows down the search space to the given package,

src/build/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod args;
1616
pub fn build(args: &mut BuildArgs) -> anyhow::Result<()> {
1717
let metadata = cargo::metadata::metadata()?;
1818

19-
let bin_target = select_run_binary(
19+
let mut bin_target = select_run_binary(
2020
&metadata,
2121
args.cargo_args.package_args.package.as_deref(),
2222
args.cargo_args.target_args.bin.as_deref(),
@@ -33,6 +33,13 @@ pub fn build(args: &mut BuildArgs) -> anyhow::Result<()> {
3333
)?;
3434

3535
args.apply_config(&config);
36+
// Update the artifact directory based on the config, e.g. in case the `target` changed
37+
bin_target.update_artifact_directory(
38+
&metadata.target_directory,
39+
args.target().as_deref(),
40+
args.profile(),
41+
args.cargo_args.target_args.example.is_some(),
42+
);
3643

3744
#[cfg(feature = "web")]
3845
if args.is_web() {

src/run/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod args;
1616
pub fn run(args: &mut RunArgs) -> anyhow::Result<()> {
1717
let metadata = cargo::metadata::metadata()?;
1818

19-
let bin_target = select_run_binary(
19+
let mut bin_target = select_run_binary(
2020
&metadata,
2121
args.cargo_args.package_args.package.as_deref(),
2222
args.cargo_args.target_args.bin.as_deref(),
@@ -32,6 +32,13 @@ pub fn run(args: &mut RunArgs) -> anyhow::Result<()> {
3232
args.is_release(),
3333
)?;
3434
args.apply_config(&config);
35+
// Update the artifact directory based on the config, e.g. in case the `target` changed
36+
bin_target.update_artifact_directory(
37+
&metadata.target_directory,
38+
args.target().as_deref(),
39+
args.profile(),
40+
args.cargo_args.target_args.example.is_some(),
41+
);
3542

3643
#[cfg(feature = "web")]
3744
if args.is_web() {

0 commit comments

Comments
 (0)