How to get Cargo pkg version of top level crate?

I am using the recommended approach for getting the current cargo version:

env!("CARGO_PKG_VERSION")

inside of a public function in crate A

crate B calls that public function, but the version returned is always that of crate A. Is there anyway for CARGO_PKG_VERSION to reflect the top level Cargo.toml version and not that of dependency crates?

I know I could run the same env! command from crate B but I'm trying to stay DRY since there are many other crates calling crate A (a utility crate)

Update:
I see that if crate B were to call a macro instead of a function from crate A then the version would be produced for crate B:

So I guess that is the only option? The problem with a macro is I don't want to lose the type hinting for my arguments I pass to it like I have with a function

Cargo generally doesn’t do such things. The build is done bottom-up, from leaf crates first to root last. Your crate can’t know where it will be used, especially because the same crate can be reused in multiple places in the build. Crates are supposed to depend only on their dependencies, not on what crate they’re being built for.

1 Like

thanks, and actually I take back my original concerns with using macro_rules!. Although rust-analyzer might not type hint so well, it is a great way to solve this problem.