napi-rs / napi-rs

A framework for building compiled Node.js add-ons in Rust via Node-API
https://napi.rs
Other
6k stars 267 forks source link

`#[napi(constructor)]` is failing to expand behind a `#[cfg_attr]` #1194

Open Hywan opened 2 years ago

Hywan commented 2 years ago

Hello,

This issue is similar to https://github.com/napi-rs/napi-rs/issues/1120. The following code is working:

#[cfg_attr(feature = "nodejs", napi)]
pub struct Bar {}

#[cfg_attr(feature = "nodejs", napi)]
impl Bar {
    #[napi(constructor)]
    pub fn new() -> Self {
        Self {}
    }
}

but changing #[napi(constructor)] to #[cfg_attr(feature = "nodejs", napi(constructor))] will fail:

error: associated `static` items are not allowed
  --> ….rs:13:36
   |
13 |     #[cfg_attr(feature = "nodejs", napi(constructor))]
   |                                    ^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the attribute macro `napi::bindgen_prelude::ctor` (in Nightly builds, run with -Z macro-backtrace for more info)

etc.

dignifiedquire commented 8 months ago

any updates on this?

Hywan commented 8 months ago

Not from my part.

dignifiedquire commented 8 months ago

I figured out using the noop feature is the only way to conditionally deactivate the derive atm