Skip to content
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

Environment variable to identify the binary name #8251

Closed
joshtriplett opened this issue May 17, 2020 · 2 comments · Fixed by #8270
Closed

Environment variable to identify the binary name #8251

joshtriplett opened this issue May 17, 2020 · 2 comments · Fixed by #8270
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` E-easy Experience: Easy S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@joshtriplett
Copy link
Member

Currently, cargo provides an environment variable CARGO_PKG_NAME containing the name of the crate. clap uses this in clap::crate_name to get the name of the crate to use as the program name.

However, this doesn't work for crates that build more than one binary. I'd like to have an environment variable specifying the name of the binary being built, such as CARGO_BIN_NAME, which clap could use for a more accurate idea of the name of the program.

For example, given the following Cargo.toml:

[package]
name = "cratename"
version = "0.1.0"
edition = "2018"

[[bin]]
name = "binname"
path = "src/main.rs"

[dependencies]
clap = "2.33.1"

When building src/main.rs, cargo should set CARGO_PKG_NAME=cratename and CARGO_BIN_NAME=binname.

@joshtriplett joshtriplett added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` E-easy Experience: Easy E-help-wanted S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review labels May 17, 2020
@ehuss
Copy link
Contributor

ehuss commented May 20, 2020

The Cargo team discussed this and decided it would be worthwhile to add two separate environment variables, CARGO_BIN_NAME which would be set when building a binary, and CARGO_CRATE_NAME which would be set for every build.

Instructions

The function that creates a rustc process (rustc_process) and the analogous function for rustdoc_process need to be altered to add the appropriate environment variables. They can look at the unit to obtain the information. The crate name can be obtained from unit.target.crate_name(), and the bin name is from unit.target.name() (bin type can be detected from unit.target.is_bin()).

The crate_env_vars test should be updated to check for the new vars. I would change the name of the binary to be different from the package, something like name = "foo-bar" in the [[bin]] part of Cargo.toml to check how dash/underscores are handled. Documentation on writing and running tests can be found in CONTRIBUTING.md and cargo-test-support.

Finally, the documentation needs to be updated.

@marvin-bitterlich
Copy link
Contributor

I would like to work on this issue as my first contribution to cargo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` E-easy Experience: Easy S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants