Version inheritance (new feature in 1.64)

I've been getting a package's version (for use in build.rs) by parsing the package's Cargo.toml. This became a little more problematic when using the new inheritance feature in 1.64, because the version is no longer stored in the crate's Cargo.toml.

I could simply check for version.workspace = true and if it is set, traverse up the fs tree until a Cargo.toml is found and parse it. That said, is there some plumbing in cargo get the version directly (no matter where it originates from) and/or the path of the workspace's Cargo.toml?

Package version can be accessed with env!("CARGO_PKG_VERSION"). There is no need to use build.rs for that.

If you need a version of a dependency, you can parse Cargo.lock, see pulldown-cmark-babelmark/build.rs at master · xfix/pulldown-cmark-babelmark · GitHub for an example of doing so.

1 Like

Sorry, I was unclear: I need the version in build.rs, because I use it to embed the version (among other things) in the binary (using the embed-resouce crate).

AFAICT, Cargo does set that for build scripts too, both static env! and runtime std::env::var.

The docs mention this:

Cargo sets several environment variables when build scripts are run. Because these variables are not yet set when the build script is compiled, the above example using env! won't work and instead you'll need to retrieve the values when the build script is run: [example using std::env::var]
...

When the build script itself is compiled, that also gets its version env! matching the package version, but code compiled in a build-dependency would see its own env!.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.