Skip to content

Support #[cfg()] in children! #18953

Open
@janhohenheim

Description

@janhohenheim

What problem does this solve or what need does it fill?

I build a menu UI that includes an "Exit" button.

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:

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<B: Bundle>(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions