Set an Xcode Project’s DerivedData Location via Script

Say the words “Derived Data” to any Apple platform developer, and they will immediately cringe, remembering painful times of deleting the directory just to get Xcode to behave properly.

I’m here to tell you, there is a better way! (Or at least a slight improvement.)

(Skip ahead for the tl;dr)

Some Context

Lately, I’ve taken to setting my Xcode projects to keep their derived data folders alongside the project itself. Xcode calls this a “Project-relative Location”, and it can be set in the UI by navigating to “File” > “Project Settings…” in the menu bar. You’ll be presented with a window, where you can change the “Derived Data:” drop-down to “Project-relative Location”.

Now whenever Xcode runs a build, it will place the derived data right next to your project. This has a number of advantages.

  1. If you need to clear derived data, you can do it on a per-project basis, without effecting any other project.
  2. Any Swift Package dependencies get placed in this directory (DerivedData/<project_name>/SourcePackages/).
  3. Having a consistent location means you can more easily refer to or use any built artifacts (e.g. For launching development versions of a macOS app for testing outside of Xcode).

The project settings window, with the "Project-relative Location" option set The project settings window

This setting is a “per-user” setting though, which ideally should be included in your .gitignore file, which means it will not get checked in and saved.

Previously, I would go manually set this setting on the projects that I worked on, but why use a mouse when you could use a script instead!

The Solution

Here’s the Bash script I came up with to perform the modifications needed on whichever project or workspace you pass to it:

#!/usr/bin/env bash

# This script sets the user-specific Derived Data location setting
# for the given Xcode project or workspace to be "project-relative"
# and next to the project or workspace.

set -o errexit  # Exit on error
set -o nounset  # Exit on unset variable
set -o pipefail # Exit on pipe failure

# Output extra debug logging if `TRACE` is set to `true`
if [[ "${TRACE:-false}" == true ]]; then
	set -o xtrace # Trace the execution of the script (debug)

help() {
	echo "Usage: $0 <path/to/project[.xcodeproj | .xcworkspace]>"

main() {
	if [[ $# -ne 1 ]]; then
		exit 1

	case "$1" in
	*.xcodeproj) ;;
	*.xcworkspace) ;;
	-h | --help)
		exit 0
		exit 1

	set_local_derived_data "$1"

set_local_derived_data() {
	# Absolute path to the `.xcodeproj` or `.xcworkspace` file
	local PROJECT_FILE=$1

	if [[ ! -d "$PROJECT_FILE" ]]; then
		echo "Error: $PROJECT_FILE does not exist or is not a directory"
		exit 1

	# Absolute path to the current user's `xcuserdatad` directory
	if [[ "$PROJECT_FILE" == *".xcodeproj" ]]; then
	elif [[ "$PROJECT_FILE" == *".xcworkspace" ]]; then

	# Create the `xcuserdatad` directory if it doesn't exist
	mkdir -p "$XCUSERDATAD_DIR"


	# Create the `WorkspaceSettings.xcsettings` file if it doesn't exist
	if [[ ! -f "$WORKSPACE_SETTINGS_PLIST_PATH" ]]; then
		plutil -create xml1 "$WORKSPACE_SETTINGS_PLIST_PATH"

	# Set the Derived Data settings
	plutil -replace BuildLocationStyle -string UseAppPreferences "$WORKSPACE_SETTINGS_PLIST_PATH"
	plutil -replace CustomBuildLocationType -string RelativeToDerivedData "$WORKSPACE_SETTINGS_PLIST_PATH"
	plutil -replace DerivedDataCustomLocation -string DerivedData "$WORKSPACE_SETTINGS_PLIST_PATH"
	plutil -replace DerivedDataLocationStyle -string WorkspaceRelativePath "$WORKSPACE_SETTINGS_PLIST_PATH"

	# Validate the `WorkspaceSettings.xcsettings` file

main "$@"

Simply call the script and pass it the path to an .xcodeproj or .xcworkspace, and it will do the modifications for you.

./set-local-derived-data.sh <path/to/project.xcodeproj>

Enjoy your newly local DerivedData directory!

Post Metadata
date Jun 2nd, 2023
tags script, software, xcode