🔥

VSCode/Cursor Core Data: Manually Compiling xcdatamodeld for Tests

(Updated on )

TL;DR: When running Swift tests in VSCode or Cursor, Core Data .xcdatamodeld files do not compile into .momd automatically as they do in Xcode. This causes unit tests to crash because the model files cannot be found. The solution is to configure the correct toolchain and use xcrun momc to manually compile the model.

Background

With the release of Swift 6, an increasing number of developers are turning to VSCode or Cursor (an AI-powered editor based on VSCode) for server-side or cross-platform Swift development. However, when working with Apple’s proprietary frameworks like Core Data, the build process in non-Xcode environments via SPM (Swift Package Manager) has gaps. The most typical issue is the failure to compile specific resource types.

Step 1: Configure the Xcode Toolchain

While you can install the open-source Swift toolchain via tools like swiftly, Core Data depends on the macOS SDK. Therefore, you must force VSCode to utilize the toolchain provided by your Xcode installation.

1. Locate Xcode

Run the following command in your terminal to confirm the installation path of Xcode and the Swift version:

Bash
/usr/bin/swift -v
# Expected output should include: /Applications/Xcode.app/...

2. Modify VSCode Settings

In your project’s root directory, modify .vscode/settings.json to explicitly specify the swift.path and the DEVELOPER_DIR environment variable.

Note: Adjust the paths according to your actual Xcode version (e.g., Xcode 17.0) if necessary.

JSON
{
  "swift.path": "/usr/bin/",
  "swift.swiftEnvironmentVariables": {
    "DEVELOPER_DIR": "/Applications/Xcode.app" 
  },
  "lldb.library": "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
  "lldb.launch.expressions": "native"
}

Step 2: Manually Compile the Data Model (Critical)

This is the step most frequently overlooked. In Xcode, the build system automatically invokes the momc compiler to convert .xcdatamodeld source files into a .momd directory that the runtime can read. In VSCode, we must perform this step manually.

Even if you declare resources: [.process(...)] in your Package.swift, standard SPM will simply copy the raw .xcdatamodeld folder into the bundle. The Core Data runtime cannot initialize a model from source files; it requires the compiled binary.

Compilation Script (compile_coredata.sh)

Create a script file in your project root to invoke xcrun momc:

Bash
#!/bin/bash

# --- Configuration ---
PACKAGE_NAME="MyCoreDataProject"   # Your Package Name
TARGET_NAME="DataHandler"          # The Target Name containing the Core Data model
MODEL_NAME="MyModel"               # The filename of xcdatamodeld (without extension)
# ---------------------

# Source file path
SOURCE_MODEL="Sources/$TARGET_NAME/Resources/$MODEL_NAME.xcdatamodeld"

# Target path: Pointing to the SPM build directory
# Note: You may need to adjust the path wildcard based on architecture (arm64/x86) and build mode (debug/release)
TARGET_DIR=".build/arm64-apple-macosx/debug/${PACKAGE_NAME}_${TARGET_NAME}.bundle/$MODEL_NAME.momd"

if [ ! -d "$SOURCE_MODEL" ]; then
    echo "❌ Error: Source model file not found: $SOURCE_MODEL"
    exit 1
fi

echo "Compiling Core Data model..."
mkdir -p "$TARGET_DIR"

# Invoke the Xcode momc compiler
xcrun momc "$SOURCE_MODEL" "$TARGET_DIR"

if [ $? -eq 0 ]; then
    echo "✅ Success! Model compiled to:"
    echo "   $TARGET_DIR"
else
    echo "❌ Compilation failed!"
    exit 1
fi

Usage

  1. Grant execution permissions to the script: chmod +x compile_coredata.sh.
  2. Run this script (./compile_coredata.sh) every time you modify the data model or perform a Clean Build.
  3. Now, when you run swift test or click the “Test” button in VSCode, Core Data will successfully load the model.

Demo

Once configured, the VSCode Testing panel can successfully execute unit tests involving Core Data:

Further Reading

Related Tips

Subscribe to Fatbobman

Weekly Swift & SwiftUI highlights. Join developers.

Subscribe Now