Skip to content

When cargo install --git checks out a commit, also set the branch and tag refs #15467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tessus opened this issue May 1, 2025 · 2 comments
Labels
A-git Area: anything dealing with git C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-install S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@tessus
Copy link

tessus commented May 1, 2025

Problem

Overview

cargo install --git does not retrieve tags or branch info.

My command is: cargo install --git https://github.com/tessus/mvt-cargo.git --branch xyz

So, cargo install fetched the correct branch but renamed it to master and did not fetch any tags either. 🤔

P.S.: Please note that this example is a bit exaggerated. In most cases the most recent tag should match the info in Cargo.toml. I only used it to even more underscore the issue I am seeing.

Details

cargo install

$ cargo install --git https://github.com/tessus/mvt-cargo.git --branch xyz
$ mvt-cargo
Version: 0.1.0-0123b0f4

manual checkout and cargo build/install

$ git clone -b xyz https://github.com/tessus/mvt-cargo.git
$ cd mvt-cargo
$ cargo build --release
$ ./target/release/mvt-cargo
Version: 0.1.1-0123b0f4 (xyz)

Investigation

A cargo install --git https://github.com/tessus/mvt-cargo.git --branch xyz creates a somewhat messed-up git directory in .cargo/git/checkouts/. e.g. repo-7f25bc5356a562d5. I call it messed-up because part of the info is missing (tags) and incorrect (branch).

The binary is then compiled from within this directory. However, in this directory there are no tags from the https://github.com/tessus/mvt-cargo.git repository, nor is the branch name correct.
This is why my build.rs cannot determine a tag nor the correct branch.

In the repo-7f25bc5356a562d5 dir, the following happens:

$ git describe --abbrev=0 --tags
fatal: No names found, cannot describe anything.

$ git rev-parse --abbrev-ref HEAD
master

However, it should show the following (which is also shown when I run these commands in the directory cloned with git clone -b xyz https://github.com/tessus/mvt-cargo.git)

$ git describe --abbrev=0 --tags
0.1.1

$ git rev-parse --abbrev-ref HEAD
xyz

Problem

As you can see, the output is different depending on how the binary was built.
It is currently impossible to use a build.rs script that generates correct information about the git repo, when using cargo install --git ....

Workaround

  • clone the repo
  • use cargo install --path ./path/to/repo or cargo build (from within the repo)

Proposed Solution

Feature request

Preserve tags and branch info, when using cargo install --git ....

Notes

I am not sure, whether the current behavior is a bug or not. If it is, please let me know and I create a bug report instead.
Otherwise, let's keep this feature request as is.

Link to minimal viable test code: https://github.com/tessus/mvt-cargo/tree/xyz

@tessus tessus added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels May 1, 2025
@epage epage added the A-git Area: anything dealing with git label May 1, 2025
@epage epage changed the title Preserve tags and branch info, when using cargo install --git When cargo install --git checks out a commit, also set the branch and tag refs May 1, 2025
@epage
Copy link
Contributor

epage commented May 1, 2025

Whether for cargo install or when depending on a package, Cargo always checks out git commits without other context like this, allowing the checkout to be immutable.

Maybe we could consider creating a separate checkout for the purposes of cargo install that would have the branch and tag refs set. It would slow it down though it is a release build so it'll already be slow. This also wouldn't be at parity with registry packages because they don't have a .git directory.

We also have a request in #6583 for setting package.version dynamically though that is more important for registries while this is more of the "marketing" / "product" version

@epage epage added S-needs-team-input Status: Needs input from team on whether/how to proceed. and removed S-triage Status: This issue is waiting on initial triage. labels May 1, 2025
@tessus
Copy link
Author

tessus commented May 1, 2025

Maybe we could consider creating a separate checkout for the purposes of cargo install that would have the branch and tag refs set.

This would be awesome. 🚀 I hope for the best.

Should this be accepted, I can help coding it... maybe. I am certainly up for trying.
(I am not familiar with the internals of cargo, but if it is just using different options for cloning te repo, it shouldn't be too complicated.)

It would slow it down though it is a release build so it'll already be slow.

Yep, I doubt it would be considerably slower. 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-git Area: anything dealing with git C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-install S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

No branches or pull requests

2 participants