Skip to content

Cargo config does not expand env vars for target-dir or CARGO_TARGET_DIR #15588

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
apiraino opened this issue May 23, 2025 · 3 comments
Closed
Labels
A-configuration Area: cargo config files and env vars

Comments

@apiraino
Copy link

I'm not 100% sure of what I am looking at, so feel free to close.
Does cargo support expanding env vars when setting --target-dir or CARGO_TARGET_DIR?

I am using

$ cargo --version
cargo 1.87.0 (99624be96 2025-05-06)

and this is my ~/.cargo/config.toml

[build]
target-dir = "$HOME/.cargo/target"
# or 
# target-dir = "~/.cargo/target"

Both variants seems to be ignored, same if I use the CLI option --target-dir or the corresponding CARGO_TARGET_DIR env var.

I've added a small test to cargo to verify my assumption:

Details

#[cargo_test]
fn cargo_target_dir_env_var() {
    write_config_toml(
        "\
[build]
target-dir = \"$HOME/.cargo/target\"
",
    );

    let gctx = new_gctx();

    assert_eq!(
        gctx.target_dir().unwrap(),
        Some(Filesystem::new("/home/my-user/.cargo/target".into()))
    );
}

and it fails with this error:

assertion `left == right` failed
  left: Some(Filesystem { root: "/home/my-user/$HOME/.cargo/target/tmp/cit/t0/$HOME/.cargo/target" })
 right: Some(Filesystem { root: "/home/my-user/.cargo/target" })

Am I doing something wrong?

Thanks!

@epage epage added the A-configuration Area: cargo config files and env vars label May 23, 2025
@epage
Copy link
Contributor

epage commented May 23, 2025

Cargo does not support environment variable or ~ expansion.

In #14125, we're tracking the creation of a build-dir (all intermediate artifacts) and have some built-in variables that can be used, like {cargo-cache-home}. See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-dir

Would that fit your needs?

@weihanglo
Copy link
Member

Variable expansions are also discussed here #10789

@apiraino
Copy link
Author

ah great thanks for the superquick reply :) For some reason I didn't find the relevant issue

Closing this as duplicate. I'll subscribe to (and maybe also test) #14125

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
Projects
None yet
Development

No branches or pull requests

3 participants