diff --git a/cli/src/build.ts b/cli/src/build.ts index bbff6286..d6374cff 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -513,6 +513,7 @@ export class BuildCommand extends Command { ...additionalEnv, TYPE_DEF_TMP_PATH: intermediateTypeFile, WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile, + CARGO_CFG_NAPI_RS_CLI_VERSION: version, } try { diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 86e4a022..a73b3db5 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -15,7 +15,7 @@ independent = true [features] noop = [] strict = [] -type-def = ["regex", "once_cell"] +type-def = ["regex", "once_cell", "semver"] [dependencies] convert_case = "0.6" @@ -30,3 +30,7 @@ version = "1" [dependencies.once_cell] optional = true version = "1" + +[dependencies.semver] +optional = true +version = "1" diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 2340ded4..7421cffa 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -14,6 +14,8 @@ pub use ast::*; pub use codegen::*; pub use error::{BindgenResult, Diagnostic}; #[cfg(feature = "type-def")] +pub use semver; +#[cfg(feature = "type-def")] pub use typegen::*; #[derive(Debug)] diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 4da7eead..a3474700 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -3,11 +3,19 @@ mod r#enum; mod r#fn; pub(crate) mod r#struct; -use std::{cell::RefCell, collections::HashMap}; +use std::{cell::RefCell, collections::HashMap, env}; use once_cell::sync::Lazy; use syn::Type; +pub static NAPI_RS_CLI_VERSION: Lazy = Lazy::new(|| { + let version = env::var("CARGO_CFG_NAPI_RS_CLI_VERSION").unwrap_or_else(|_| "0.0.0".to_string()); + semver::Version::parse(&version).unwrap_or_else(|_| semver::Version::new(0, 0, 0)) +}); + +pub static NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX: Lazy = + Lazy::new(|| semver::Version::new(2, 15, 1)); + #[derive(Default, Debug)] pub struct TypeDef { pub kind: String, @@ -103,9 +111,17 @@ impl ToString for TypeDef { } else { "".to_owned() }; + // TODO: remove this in v3 + // This is a workaround for lower version of @napi-rs/cli + // See https://github.com/napi-rs/napi-rs/pull/1531 + let prefix = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX { + format!("{}:", pkg_name) + } else { + "".to_string() + }; format!( - r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#, - pkg_name, + r#"{}{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#, + prefix, self.kind, self.name, escape_json(&self.js_doc), diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index af5770f5..c81ed0ca 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -68,7 +68,12 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream { // logic on first macro expansion #[cfg(feature = "type-def")] if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") { - if let Err(_e) = remove_existed_type_def(type_def_file) { + use napi_derive_backend::{NAPI_RS_CLI_VERSION, NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX}; + if let Err(_e) = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX { + remove_existed_type_def(type_def_file) + } else { + fs::remove_file(type_def_file) + } { #[cfg(debug_assertions)] { println!("Failed to manipulate type def file: {:?}", _e);