Xcode hangs on "Compiling Swift source files"
Solution 1
Thanks for all commentors' suggestions. I narrowed it down to a map
's closure referencing a property that I had removed. Example:
var people: [Person] = ...
let foo = people.map { "\($0.name), \($0.age)" }
where Person
looks something like:
protocol Person {
var name: String { get }
var age: Int { get }
}
This all works fine. Then I removed age
while keeping the closure unchanged. This caused Xcode to become hopelessly confused. Probably related to the Swift's type inference.
Solution 2
I made a class extend itself. That also causes Swift compiler to get stuck in a loop without error:
class X: X
Solution 3
Change "Swift Compiler Optimization Level" in Build Settings from "Whole module optimization" to "Single file optimization". It may not be your problem, but it solved mine I was stuck with for half a day. It may just be a temporary bug in the recent Xcode version (8.2.1 was the one I have been using at the time I wrote this).
Solution 4
Try clean your Project Build Folder
- Hold down option key and got to Product -> Clean Build Folder ( where Clean used to be in the menu)
- If you are using CocoaPods delete your Workspace file and run
Pod Install
orPod Update
I think 2 is probably the cause.
Solution 5
There seems to be various possible causes of extremely long compilation time. Corner or edge cases are everywhere. So the best way is to observe and investigate your own case.
Although being mentioned by others in comments, but steps below still worths more attention:
- Run project
- Switch to Report Navigator (command + 9), and select current running
Build
task. See which source file is taking up a lot of compiling time. - Check recent commit history of that source file. Investigate the possible cause.
Related videos on Youtube
Steve Kuo
Software, pilot, travel, life http://www.linkedin.com/in/stevekuo1
Updated on June 18, 2022Comments
-
Steve Kuo almost 2 years
I'm running Xcode 7.3.1. When building my Swift based project, it hangs on "Compiling Swift source files". I've tried various combination of deleting
DerivedData
, clean, run, restarting Xcode, restarting OS X, none seem to work. Any ideas?-
vacawama almost 8 yearsIf you have any large Array or Dictionary literals, make sure you tell Swift the type and don't make it infer the type from the data.
-
Hamish almost 8 yearsWhile compiling, go into the report navigator (CMD-8) and you should see which particular source file is the bottleneck. Have a look for any complicated lines of code that rely heavily on type inference – e.g complicated array or dictionary literals or complicated functional one-liners.
-
-
Marchy over 7 yearsWow, this was it. Damn those inferred $0 parameters. They're current implementation sucks! (no type inference getting passed through the tools (Xcode, Appcode, etc.... and apparently even the compiler)
-
Kent Robin over 7 years1.
Clean
is not underEditor
, but underProduct
-
jbm over 7 yearsNot exactly the same, but somewhat related. I just spent two hours tracking down a hanging compile that came down to a map() inside a print() call (e.g., print("something mapped: (something.map { $0.stuff })"). Crazy...
-
Kirkland over 7 yearsTHANK YOU! I really do wish Apple would add something like this automatically, like a dev debug mode that runs it before every build. Or just fix whatever the hangup is in the build folder to being with.
-
UKDataGeek over 7 yearsI agree Kirkland, I have needed to do this more frequently since Xcode 8 was released.
-
Cœur over 7 yearsYou can read on similar causes on cocoawithlove.com/blog/2016/07/12/type-checker-issues.html
-
Mohsin Khubaib Ahmed over 7 yearsyes, same is happening with me, what did you do to solve it? Please help asap! Thanks a lot.
-
BallpointBen about 7 yearsSorry, I don't understand... I've seen the auto-complete suggest members of the
$0
variable. For instance, in the example above if I type "$0.n" it will suggest$0.name
. It can do the type inference of[Person]
->$0: Person
->$0.name
, so why can't it detect that$0.age
doesn't exist? Is it because it's in a string? -
Baran Emre about 6 yearsOhh myy... why this is not cached by the compiler.
-
Tamás Sengel about 6 yearsStill a bug with Xcode 9.3. Thank you, this saved my evening!
-
Peterdk over 5 yearsStill doesn't catch this. I removed a protocol, and renamed a class, that extended itself now. :S
-
ChikabuZ about 3 yearsChanging
Optimization Level
toNo Optimization
helped me! -
Aswath about 3 yearsThis is the only way I could find the error in my project. Thanks
-
Bisca over 2 yearsYou saved my day!!