Skip to content

Instantly share code, notes, and snippets.

@Stebalien
Created March 17, 2025 17:36
Show Gist options
  • Save Stebalien/038ab35a0ec2899b95af1980040cd22b to your computer and use it in GitHub Desktop.
Save Stebalien/038ab35a0ec2899b95af1980040cd22b to your computer and use it in GitHub Desktop.
Cargo wrapper that puts target directories in the cache.
#!/bin/bash
if [[ -z "$CARGO_TARGET_DIR" ]] && [[ -z "$CARGO_BUILD_TARGET_DIR" ]]; then
cargo_conf="$(/usr/bin/cargo locate-project --offline --frozen --workspace --message-format plain)"
project_root="$(dirname "${cargo_conf}")"
CARGO_TARGET_DIR="${XDG_CACHE_DIR:-$HOME/.cache}/cargo/build/$(systemd-escape "${project_root}")/"
export CARGO_TARGET_DIR
fi
exec /usr/bin/cargo "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment