Steve Kuo
Steve Kuo

Reputation: 63054

Xcode hangs on "Compiling Swift source files"

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?

Upvotes: 23

Views: 23387

Answers (13)

Himanth
Himanth

Reputation: 2421

Just check if any duplicated files added or missing files in Compile Sources in Build Phases.

In my case

  1. Same file added two times.
  2. Later after clean and build again, I faced one more error related to function is not in scope. I found that the file was not added to compile sources which contains same function.

These will happen if you rebase or cherry-pick.

Upvotes: 0

basvk
basvk

Reputation: 4546

Something I found useful was the post from RoystonP on https://developer.apple.com/forums/thread/115303?answerId=391817022#391817022:

  1. Open Activity Monitor.
  2. Start your project building.
  3. Wait for the the build’s progress to completely stop for several minutes.
  4. Locate the Swift processes in Activity Monitor (they should be using nearly 100% CPU) and select one of them.
  5. In the menu bar, select “View”, then “Send Signal To Process…”
  6. Select “Abort (SIGABRT)” from the drop-down list.
  7. Click the “Send” button. This will simulate an assertion failing, so the compiler will print information about what it’s doing and then exit.
  8. In Xcode, switch to the Report Navigator (the “speech bubble” button over the left-side pane) and select the build.
  9. Scroll down to the now-failed compilation step and click the transcript button (button with five lines, to the right of the “Compile [Filename]” line).
  10. Scroll down to see the diagnostic information. It will probably include a list of command-line flags, a few lines saying things like “In pass SimplifyCFG”, and a stack trace.

You might have to repeat step 6 and 7 in order to get it to work.

Upvotes: 15

M.Bonjour
M.Bonjour

Reputation: 1152

What I came across the bug is that The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

I missed a left arrow here.

enter image description here

Upvotes: 0

Renetik
Renetik

Reputation: 6373

So I believe that in most cases it's well known dictionary literal type interference problem.

Code like this:

 let params = [
                "title": title, "desc": desc, "velikost": velikost,
                "cena": cena, "vykon": vykon, "telefon": telefon,
                "rokVyroby": rokVyroby, "stkDo": stkDo,
                "zemePuvodu": zemePuvodu, "najetoKilometru": najetoKilometru,
                "stav": stav, "ZnackaId": znackaId,
                "VyrobceId": vyrobceId,
                "category": categoryId, "subCategory": subCategoryId,
                "modely[]": modelId, "prodejNakup": prodejNakup,
                "mena": mena, "isNovy": isNovy, "serviska": serviska,
                "abs": abs, "technicak": technicak,
            ]

Have to be written better always like this:

let params: [String: String] = [
                "title": title, "desc": desc, "velikost": velikost,
                "cena": cena, "vykon": vykon, "telefon": telefon,
                "rokVyroby": rokVyroby, "stkDo": stkDo,
                "zemePuvodu": zemePuvodu, "najetoKilometru": najetoKilometru,
                "stav": stav, "ZnackaId": znackaId,
                "VyrobceId": vyrobceId,
                "category": categoryId, "subCategory": subCategoryId,
                "modely[]": modelId, "prodejNakup": prodejNakup,
                "mena": mena, "isNovy": isNovy, "serviska": serviska,
                "abs": abs, "technicak": technicak,
            ]

But I believe that even shorter literals are issue and to save time by waiting for build it's better to always define type for dictionary literals so compiler don't have to find out by himself, as he obviously struggle. PS: I think apple engineers have some serious personal problems there, they have to hire some people from Jatbrains or maybe even me ;) to focus on important things and not waste time on discussions on how Swift have to be different from others...

Upvotes: 0

denkeni
denkeni

Reputation: 989

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.

Upvotes: 5

Jeff Evernham
Jeff Evernham

Reputation: 41

I had the same problem. In my case it seems to be a result of applying too many nil coalescing actions. I was building a json item:

json = [ "item1": value1 ?? "",
         "item2": value2 ?? "",
         "item3": value3 ?? "",
         ...
         "item14": value14 ?? "" ]

This wouldn't compile. When I removed all the nil coalescing so that it looked like the following, it compiled fine.

json = [ "item1": value 1,
         "item2": value 2,
         "item3": value 3,
         ...
         "item14": value 14 ]

I did not try to figure out the cutoff point for the number of items before it got stuck.

Upvotes: 4

Serhiy
Serhiy

Reputation: 331

In my case XCode stucks on big dictionary literal:

requestParameters = [
                        "asset" : "...",
                        "user" : "...",
                        // about 15 additional keys
                        ]

The problem was fixed after replacing this part by:

var requestParameters = [String : Any]()
requestParameters["asset"] = "..."
requestParameters["user"] = "..."
// about 15 additional keys

Upvotes: 0

Hai
Hai

Reputation: 690

I made a class extend itself. That also causes Swift compiler to get stuck in a loop without error:

class X: X

Upvotes: 25

Werner Kratochwil
Werner Kratochwil

Reputation: 382

xcode seems to have a problem concatenating more than 5 strings. See this: Xcode freezes when trying to execute this in a Swift playground? The given workaround solved my problem

Upvotes: 0

Miroslav Kuťák
Miroslav Kuťák

Reputation: 1915

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).

Upvotes: 7

Swati Gupta
Swati Gupta

Reputation: 364

In my case the problem was during JSON parsing. I was sending an optional value in a dictionary parameter during JSON parsing.

Upvotes: 2

UKDataGeek
UKDataGeek

Reputation: 6882

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.

Upvotes: 7

Steve Kuo
Steve Kuo

Reputation: 63054

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.

Upvotes: 8

Related Questions