Xcode hangs on "Compiling Swift source files"

18,646

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

  1. Hold down option key and got to Product -> Clean Build Folder ( where Clean used to be in the menu)
  2. If you are using CocoaPods delete your Workspace file and run Pod Install or Pod 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:

  1. Run project
  2. Switch to Report Navigator (command + 9), and select current running Build task. See which source file is taking up a lot of compiling time.
  3. Check recent commit history of that source file. Investigate the possible cause.
Share:
18,646

Related videos on Youtube

Steve Kuo
Author by

Steve Kuo

Software, pilot, travel, life http://www.linkedin.com/in/stevekuo1

Updated on June 18, 2022

Comments

  • Steve Kuo
    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
      vacawama almost 8 years
      If 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
      Hamish almost 8 years
      While 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
    Marchy over 7 years
    Wow, 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
    Kent Robin over 7 years
    1. Clean is not under Editor, but under Product
  • jbm
    jbm over 7 years
    Not 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
    Kirkland over 7 years
    THANK 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
    UKDataGeek over 7 years
    I agree Kirkland, I have needed to do this more frequently since Xcode 8 was released.
  • Cœur
    Cœur over 7 years
  • Mohsin Khubaib Ahmed
    Mohsin Khubaib Ahmed over 7 years
    yes, same is happening with me, what did you do to solve it? Please help asap! Thanks a lot.
  • BallpointBen
    BallpointBen about 7 years
    Sorry, 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
    Baran Emre about 6 years
    Ohh myy... why this is not cached by the compiler.
  • Tamás Sengel
    Tamás Sengel about 6 years
    Still a bug with Xcode 9.3. Thank you, this saved my evening!
  • Peterdk
    Peterdk over 5 years
    Still doesn't catch this. I removed a protocol, and renamed a class, that extended itself now. :S
  • ChikabuZ
    ChikabuZ about 3 years
    Changing Optimization Level to No Optimization helped me!
  • Aswath
    Aswath about 3 years
    This is the only way I could find the error in my project. Thanks
  • Bisca
    Bisca over 2 years
    You saved my day!!