Joy of Debugging: Command swiftc failed with exit code 1
I’ve been working through a particularly nasty issue, so it feels right to document it for anyone else who encounters it. Also, just to serve as a note and reminder that game development is full of unexpected, time consuming tasks on a near weekly basis. This most recent problem — releasing a build to an iOS device fails to compile, but it works on the simulator. Specifically, this occurs when I try to change the Swift compiler optimization level.
Command /Applications/...../XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
Unfortunately, no line number accompanies this issue. Stack Overflow is full of potential causes like missing resource, deleting derived data, or codesign issues. Unfortunately, none of those worked for me. (Side note: two of those links are not swiftc errors. They are clang, and swift-stdlib-tool commands. In my frustration I overlooked that.) However, I did stumble across this wonderful post explaining how to get more information from the build. Going forward with the command line approach, a helpful error message is visible at the bottom of the output:
** BUILD FAILED ** The following build commands failed: CompileSwift normal arm64 /Users/veeneck/Documents/Games/Xcode/Fort/Fort/PinchGesture.swift CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
That shows me that
PinchGesture.swift is the culprit. You can get similar help from Xcode, but it is not as clear. You have to scan each individual file in the output for errors instead of just jumping to the bottom. In any case, if you’re interested in that approach, show all messages in the output like so. Notice the error in the same file, but this time it may read:
LLVM ERROR: Broken function found, compilation aborted!
Now that the error has been isolated, let’s fix the code. I was able to fix this by trial and error, and I still don’t fully understand the differences, but here goes. My original function:
And now the new function. Notice that I changed the variable to not be editable, and to return within the if statements.
Definitely an interesting bug, and anything you encounter will most likely be a different sort of issue. But, now we know how to track something like this down.