Support `#[cfg()]` in `children!`
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
I build a menu UI that includes an "Exit" button.
```rust
commands.spawn((
widget::ui_root("Title Screen"),
StateScoped(Screen::Title),
children![
widget::button("Play", enter_loading_or_gameplay_screen),
widget::button("Settings", enter_settings_screen),
widget::button("Credits", enter_credits_screen),
widget::button("Exit", exit_app),
],
));
```
Surely that exit button makes no sense on Wasm, so let's hide it there:
```rust
commands.spawn((
widget::ui_root("Title Screen"),
StateScoped(Screen::Title),
children![
widget::button("Play", enter_loading_or_gameplay_screen),
widget::button("Settings", enter_settings_screen),
widget::button("Credits", enter_credits_screen),
#[cfg(not(target_family = "wasm"))]
widget::button("Exit", exit_app),
],
));
```
Run compile and... Oh no!
```
Compiling bevy_new_2d v0.1.0 (/home/hhh/git/bevy_new_2d)
error[E0061]: this struct takes 1 argument but 0 arguments were supplied
--> src/screens/title.rs:15:9
|
15 | / children![
16 | | widget::button("Play", enter_loading_or_gameplay_screen),
17 | | widget::button("Settings", enter_settings_screen),
18 | | widget::button("Credits", enter_credits_screen),
19 | | #[cfg(not(target_family = "wasm"))]
20 | | widget::button("Exit", exit_app),
21 | | ],
| |_________^ argument #1 is missing
|
note: tuple struct defined here
--> /home/hhh/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/spawn.rs:36:12
|
36 | pub struct Spawn(pub B);
| ^^^^^
= note: this error originates in the macro `children` (in Nightly builds, run with -Z macro-backtrace for more info)
help: provide the argument
--> /home/hhh/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.0/src/hierarchy.rs:502:66
|
502- $crate::hierarchy::Children::spawn(($($crate::spawn::Spawn($child)),*))
502+ $crate::hierarchy::Children::spawn(($($crate::spawn::Spawn(/* value */)),*))
|
For more information about this error, try `rustc --explain E0061`.
error: could not compile `bevy_new_2d` (lib) due to 1 previous error
WARN Failed to run cargo, trying to find automatic fix.
```
Wow, that sure is one spooky looking error message!
## What solution would you like?
Support this use-case, it seems fairly common
## What alternative(s) have you considered?
- Leave it be and document this somewhere
- Improve the error message
## Additional context
This sneakily broke the web build in bevy_new_2d in a very non-obvious way.
Contributor guide
Research direction
Start with the `children!` macro and reproduce the reported menu example using `#[cfg(not(target_family = "wasm"))]` on one child. Check the generated expansion on both target configurations. Done means the macro compiles, includes the child on non-Wasm targets, and omits it on Wasm targets without the confusing `Spawn` error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, wasm
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100