Skip to content

Commit 3afea1b

Browse files
committed
use CARGO_CRATE_NAME instead of CARGO_PKG_NAME and add the version to prevent conflicts if the same crate is included twice with different versions
1 parent e5ac8cc commit 3afea1b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

godot-macros/src/ffi_macros.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ pub(super) fn wasm_declare_init_fn(input: TokenStream) -> ParseResult<TokenStrea
2323
return bail!(input, "macro expects no arguments");
2424
}
2525

26-
let crate_name = env::var("CARGO_PKG_NAME")
27-
.expect("CARGO_PKG_NAME env var not found. This macro must be run by Cargo.")
28-
.replace('-', "_"); // crate names may contain hyphens, but Rust identifiers must not.
26+
let crate_name = env::var("CARGO_CRATE_NAME")
27+
.expect("CARGO_CRATE_NAME env var not found. This macro must be run by Cargo.");
28+
29+
let crate_version = env::var("CARGO_PKG_VERSION")
30+
.expect("CARGO_PKG_VERSION env var not found. This macro must be run by Cargo.")
31+
.replace(['.', '+', '-'], "_"); // version must follow semver, which allows digits, dots, hyphens, and plus signs, and alphanumeric characters.
2932

3033
let index = FUNCTION_COUNTER.fetch_add(1, Ordering::Relaxed);
3134

@@ -34,7 +37,8 @@ pub(super) fn wasm_declare_init_fn(input: TokenStream) -> ParseResult<TokenStrea
3437
//
3538
// As such, instead we export a function with a known prefix to be used by the embedder.
3639
// This prefix is queried at load time, see godot-macros/src/gdextension.rs.
37-
let function_name = format_ident!("__godot_rust_registrant_{crate_name}_{index}");
40+
let function_name =
41+
format_ident!("__godot_rust_registrant_{crate_name}_{crate_version}_{index}");
3842

3943
let code = quote! {
4044
#[cfg(target_family = "wasm")] // Strictly speaking not necessary, as this macro is only invoked for Wasm.

0 commit comments

Comments
 (0)