Skip to content

Unable to use #[cfg(target_os = "windows")] into a #[function_component] function #3046

@ClementGre

Description

@ClementGre

Problem
The cfg macros that check about the os does not works into component functions, preceded with #[function_component(App)].

Steps To Reproduce
In a component function :

#[function_component(App)]
pub fn app() -> Html {
}

You can't use any cfg macro or any conditional compiling macro.
e.g.

#[cfg(target_os = "windows")]
html! {
        <section class="sidebar leftbar">
            <h2>{"On Windows"}</h2>
        </section>
}
#[cfg(not(target_os = "windows"))]
html! {
        <section class="sidebar leftbar">
            <h2>{"Not on Windows"}</h2>
        </section>
}

The cfg!(target_os = "xxx") boolean macro also returns false whatever the parameter provided.
The statement env::var("CARGO_CFG_TARGET_OS") returns env::var("CARGO_CFG_TARGET_OS")

⚠️ This only occurs into functions components, that are preceded by :

#[function_component(App)]

or into functions that are called from a component function.

Expected behavior
The statement #[cfg(target_os = "windows")] should hide to the compiler the following statement.
The expression cfg!(target_os = "windows") should return true or false depending of the current platform.
The expression env::var("CARGO_CFG_TARGET_OS")

Environment:

  • Yew version: 0.20.0
  • Rust version: 1.65.0
  • Target, if relevant: wasm32-unknown-unknown
  • Build tool, if relevant: tauri
  • OS, if relevant: MacOS
  • Browser and version, if relevant: Using tauri webpack

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions