Extract revision/tag of git dependency from cargo

⚓ Rust    📅 2025-06-26    👤 surdeus    👁️ 7      

surdeus

Warning

This post was published 44 days ago. The information described in this article may have changed.

i have the following setup:

  • git repo A (hosted on GitHub) contains two crates: a library and a binary
  • git repo B (also hosted on GitHub):
    • depends on the library of repo A
    • needs to use the binary from repo A in its GitHub workflow and needs to have the same revision of the binary as it uses for the library

these repos are private, accordingly i cannot cargo-publish them to crates.io (and there's no private artefact server available where this is being used). but even if it were released there would still be the issue of grabbing the correct version.

the "easy" (quick-win) option is to just hard-code the version of repo A in two places: the tag of the git dependency in Cargo.toml and in the script which downloads the binary.
but: this will obviously lead to the two running out of sync since somebody will forget to update it in one of the two places. so i'd instead like to extract the tag of the dependency from cargo.
the next quick-win would be to just download the artefact from the latest release, but then i'd run into issues whenever i release a new version of repo A but haven't updated repo B yet.

what's the best way to achieve this?

i've found cargo-metadata which gives me a JSON. i guess i could use jq to try and extract the source of the library from repo A being pulled in as a dependency? and then i'd have to do some string-mangling to extract the tag from that?

is there an easier/cleaner way to achieve this? i was hoping that there's maybe a cargo command which could give me that information, something along the lines of the (imaginary) cargo show-dependency foo-lib --source which would give me a structured (e.g. JSON) source information just for that one foo-lib dependency where i could grab a git: tag: v1.0.0 information easily (again e.g. using jq).

thanks!

4 posts - 2 participants

Read full topic

🏷️ rust_feed