Cargo: Override the target directory

Created on 25 Aug 2014  路  12Comments  路  Source: rust-lang/cargo

Cargo writes build results to a target directory next Cargo.toml. I鈥檇 like to have a configuration key in .cargo/config to override this location. Ideally, this configuration would be a template based on the project name or path to the source directory: target_dir = "~/tmp/cargo/{}" would expand to /home/simon/tmp/cargo/rust-url or /home/simon/tmp/cargo/home/simon/projects/rust-url.

(Motivation: ~/tmp is ignored in my backups. Ignoring all directories named target is risky as they鈥檙e not necessarily from Cargo build results. For what it鈥檚 worth, my ~/tmp/servo-build is currently 2.2 GB (although not using Cargo.))

Most helpful comment

I think Cargo.toml is a bad place for this, given that this file is committed to git and shared across multiple computers. I think this should go into $HOME/.cargo/config.

This feature would help a lot for backups, I currently have to exclude every target/ folder manually.

All 12 comments

As a work-around, I can make target (the one next to Cargo.toml) a symlink. Not as nice as a system-wide config that would get automatically applied to every project, but good enough to not make this request as pressing.

Closing, since I鈥檓 happy with symlinks. (Especially now that Servo has upgraded to a Rust version that fixes https://github.com/rust-lang/rust/issues/16496.)

Actually, I think the Cargo.toml file itself could name where the target directory is.

For what it鈥檚 worth, these days Servo鈥檚 build system as configuration to set the CARGO_TARGET_DIR environment variable, which is how Cargo allows that location to be overridden.

If so, a one-liner could be similar to:

CARGO_TARGET_DIR=foo; cargo run args

Cool.

cargo --target-dir=foo run args

Would be more unix-like, but I'll settle for the former :)

CARGO_TARGET_DIR=foo; cargo run args

Without the semi-colon, in order to set the variable for that command only.

Yes, you're right.

Closing, since I鈥檓 happy with symlinks.

Unsatisfactory for Windows.

CARGO_TARGET_DIR

Unsatisfactory for scripting; this value should be configurable from the Cargo.toml file itself.

I think Cargo.toml is a bad place for this, given that this file is committed to git and shared across multiple computers. I think this should go into $HOME/.cargo/config.

This feature would help a lot for backups, I currently have to exclude every target/ folder manually.

Windows supports symlinks.

I think Cargo.toml is a bad place for this, given that this file is committed to git and shared across multiple computers. I think this should go into $HOME/.cargo/config.

That would be worse for per-project configurations.

You can put a .cargo/config in the repository.

Was this page helpful?
0 / 5 - 0 ratings