Xcode Archive freezes near the end: a large Swift literal and the one-line fix
One of our iPhone apps built in seconds in Debug and never finished in Release. Product, Archive ran to what looked like the last step and stayed there: fans on, no error, no end. The cause was a single Swift file, and the fix was one attribute. Here is how to recognise the problem, how to prove which file is responsible, and what to change.
What it looks like
- Debug builds are fine. Only Release builds and archives hang.
- The progress bar stops close to the end and Xcode still responds. Nothing is printed, because nothing has failed.
- Activity Monitor shows one swift-frontend process using a full processor core for many minutes.
- Leaving it overnight does not help. In our case we measured more than ten minutes of processor time with no end in sight.
The cause
The app kept all of its translations in one dictionary literal: about 270 keys in 10 languages, 108 KB of source in a single expression. A second file held a list of 198 countries written the same way.
Swift compiles a literal like that into one very large function that builds the collection step by step. In a Debug build nothing tries to improve that function. In a Release build the optimiser does, and one of its passes, the one that looks for avoidable array copies, takes far longer than linear time on a function with thousands of blocks. It is not an endless loop in theory. In practice it does not finish.
With whole-module compilation the whole target is one compile job, which is why the progress bar sits near 100% and gives no hint about the file.
Prove which file it is
Do not guess. While the build is stuck, find the compiler process and ask macOS what it is doing:
ps -Ao pid,etime,time,command | grep swift-frontend
sample <pid> 5 -file ~/Desktop/swift-sample.txt
Open the sample file and read the top of the call graph. If you see COWArrayOpt and ColdBlockInfo there, you have this problem. The swift-frontend command line from the first command names the source files of the job, and the one with the largest literal is your suspect.
The fix
Move the literal into a function and tell the optimiser to leave that one function alone. Everything else in the app keeps full optimisation.
private let strings: [String: [String: String]] = buildStrings()
@_optimize(none)
private func buildStrings() -> [String: [String: String]] {
[
"en": ["title": "Welcome"],
"de": ["title": "Willkommen"],
// ...the rest of the table
]
}
Our Release archive went from never finishing to 139 seconds. The function runs once at launch and only builds a table, so there is no speed to lose by not optimising it.
@_optimize(none) starts with an underscore, which means Apple does not promise to keep it stable. It has worked for years, but leave a comment next to it that says why it is there.
Two things not to do while it hangs
- Do not kill swift-frontend from Terminal, and do not delete DerivedData while Xcode is open. Both leave half-written build files, and the next archive fails with "unable to open dependencies file".
- If you already did, the way back is Product, Clean Build Folder, then Archive again.
A cleaner long-term answer
A table that size does not need to be Swift source at all. A JSON file or a strings catalog in the app bundle loads at launch, compiles in no time and is easier for a translator to edit. The attribute is the fix for today; moving the data out of the code is the fix for the next year.
We write about problems like this one in our Swift and Xcode books, in plain language, for people who are starting out.