Search code examples
rustrust-cargo

How can I specify a custom Cargo output directory?


I put this in my Cargo.toml

[build]
target-dir = "../my-target"

However, Cargo doesn't recognize this key.

cargo run --release --bin my_project

warning: unused manifest key: build
error: failed to open: /.../project-root/target/releases/.cargo-lock

Caused by:
  Permission denied (os error 13)

The custom target dir with the environment variable works:

CARGO_TARGET_DIR=../my-target cargo run --bin my_project

but how can I specify '../my-target' in Cargo.toml?


Solution

  • [build] is a Cargo-level configuration rather than for the project:

    This document will explain how Cargo’s configuration system works, as well as available keys or configuration. For configuration of a project through its manifest, see the manifest format.

    Put your [build] inside $PROJECT_DIR/.cargo/config.toml or even $HOME/.cargo/config.toml. See the above link for all the options.