PC パソコン

Xcode ‘Command CompileSwiftSources failed with a nonzero exit code’: Causes and Fixes

When building a Swift project in Xcode, you may see Command CompileSwiftSources failed with a nonzero exit code and the build stops. This message is usually not the root cause itself. It is a summary indicating that the Swift compilation step failed. The actual problem is typically shown earlier in the build log, such as a syntax error, type mismatch, broken dependency, or stale build cache.

Causes

Common causes include:

  • Swift syntax errors or type-checking errors
  • Type inference failures involving optionals or generics
  • Dependency problems with Swift Package Manager or CocoaPods
  • Corrupted DerivedData or module caches
  • Incorrect Build Settings or Swift compiler configuration
  • Old build artifacts left after an Xcode update
  • A source file included in the wrong target

Typical Situation

The end of the build log may show only:

Command CompileSwiftSources failed with a nonzero exit code

But a few lines earlier, the actual compiler error may look like this:

error: cannot convert value of type 'String' to expected argument type 'Int'

How to Check the Real Error

  1. Open the Issue Navigator in Xcode.
  2. Look for the first red error in the list.
  3. Open the Report Navigator and select the failed build.
  4. Expand the CompileSwift or CompileSwiftSources step.
  5. Find the first error: line before the nonzero exit code message.
  6. Identify the affected Swift file and line number.

Important: The nonzero exit code message is usually only the final result. Fix the first Swift compiler error shown above it before troubleshooting anything else.

Fix 1: Correct the First Swift Compiler Error

The most common cause is an ordinary Swift code error. For example, if a function expects an integer, pass the correct type.

func updateCount(_ value: Int) {
    print(value)
}

updateCount(10)

If a string is passed by mistake, correct it:

// Wrong
updateCount("10")

// Correct
updateCount(10)

Fix 2: Clean the Build Folder

If the error appears even though the source code looks correct, stale build artifacts may be involved.

  1. Open Product in Xcode.
  2. Hold the Option key.
  3. Select Clean Build Folder.
  4. Build the project again.

Fix 3: Delete DerivedData

If cleaning the build folder does not help, remove DerivedData:

rm -rf ~/Library/Developer/Xcode/DerivedData/*

Restart Xcode and rebuild the project afterward.

Fix 4: Re-resolve Swift Package Manager Dependencies

If the project uses Swift Package Manager, the package resolution state or cache may be inconsistent.

  1. Open File > Packages in Xcode.
  2. Run Reset Package Caches.
  3. Run Resolve Package Versions.
  4. Build again.

You can also resolve dependencies from the command line:

xcodebuild -resolvePackageDependencies

Fix 5: Reinstall CocoaPods Dependencies

For projects using CocoaPods, generated Pods files may be stale or inconsistent.

pod deintegrate
pod install

Afterward, open the .xcworkspace file instead of the .xcodeproj file.

Fix 6: Check Target Membership

A Swift file included in an unintended target can fail to compile if that target does not have access to the same modules, frameworks, or symbols.

  1. Select the affected Swift file.
  2. Open the File Inspector.
  3. Check Target Membership.
  4. Remove the file from targets where it does not belong.

Fix 7: Review Swift Compiler Settings

After upgrading Xcode or working with an older project, compiler settings may no longer match the project configuration. Review these items under Build Settings:

  • Swift Language Version
  • Other Swift Flags
  • Active Compilation Conditions
  • Optimization Level

If you added custom compiler flags, temporarily remove them to isolate the problem.

Fix 8: Inspect the Build from the Command Line

If Xcode’s interface does not make the first error obvious, run the build with xcodebuild and inspect the output.

xcodebuild -project MyApp.xcodeproj -scheme MyApp -configuration Debug build

For a workspace-based project:

xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -configuration Debug build

Search the output for the first occurrence of error:.

Notes

  • Searching only for the final nonzero exit code message often hides the real cause.
  • Deleting DerivedData clears caches but does not fix actual source-code errors.
  • Before upgrading dependencies, confirm the versions currently used by the project.
  • If many errors appear at once, fixing the first one may eliminate most of the others.

Summary

Command CompileSwiftSources failed with a nonzero exit code means the Swift compilation phase failed, but the root cause is usually listed earlier in the build log. Start with the first compiler error, then check your Swift code, dependencies, Target Membership, and Build Settings. If no obvious source error exists, clean the build folder, delete DerivedData, and re-resolve package dependencies.