Skip to content

cargo install doesn't set env variable defined in .cargo/config.toml #15609

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

Closed
cakebaker opened this issue May 29, 2025 · 1 comment
Closed
Labels
A-configuration Area: cargo config files and env vars C-bug Category: bug Command-install S-triage Status: This issue is waiting on initial triage.

Comments

@cakebaker
Copy link

Problem

Hi,

In our project, coreutils, we have the following section in .cargo/config.toml:

[env]
PROJECT_NAME_FOR_VERSION_STRING = "uutils coreutils"

We then use the env! macro from std to retrieve the value. The macro emits a compilation error if the environment variable is not defined.

Everything works fine if I use cargo install with --path:

$ cargo install --path ~/projects/coreutils/ --locked

However, if I try to install it from crates.io with:

$ cargo install coreutils --locked

I get many errors about the environment variable not being defined at compile time, for example:

error: environment variable `PROJECT_NAME_FOR_VERSION_STRING` not defined at compile time
   --> /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/uu_wc-0.1.0/src/wc.rs:399:18
    |
399 |         .version(uucore::crate_version!())

My initial thought was that .cargo/config.toml is missing from the crates file, but that's not the case as you can see when downloading the crates file from https://crates.io/api/v1/crates/coreutils/0.1.0/download.

Steps

I guess the steps are:

  • add [env] section to .cargo/config.toml and define an env variable
  • read the env variable with env! macro in the application
  • publish crate
  • cargo install <crate_name> --locked

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.87.0 (99624be96 2025-05-06)
release: 1.87.0
commit-hash: 99624be96e9d213b0e9b1e36451271f24e4a41d8
commit-date: 2025-05-06
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Arch Linux [64-bit]
@cakebaker cakebaker added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels May 29, 2025
@epage epage added A-configuration Area: cargo config files and env vars Command-install labels May 29, 2025
@epage
Copy link
Contributor

epage commented May 29, 2025

Yes, this is a surprising aspect to cargo install and config.toml in general.

config.toml is environmental configuration for where the command is being run, not where the command comes from. We actually make a small exception for cargo install to only be dependent on the user config and not also the current working directory so where you run it on your system isn't impacted as much,

As this looks like this is the same case as #14001, I'm going to close in favor of that issue. If there is a reason to keep this open, let us know!

@epage epage closed this as completed May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars C-bug Category: bug Command-install S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants