198

After updating to the latest version of Xcode at the moment (version 10.0) the project is unable to build because it found some errors regarding some "Command CompileSwift failed with a nonzero exit code" error.

How do I solve this errors? They appear in most of the Pods (I use CocoaPods) I use inside my project.

I have tried updating the pod version and the pods to the latest version available, but the problem is still there.

I have searched a lot through the web and there is very little information regarding this issue.

Joshua
  • 3,055
  • 3
  • 22
  • 37
Jordi Gámez
  • 3,400
  • 3
  • 22
  • 35
  • 26
    `pod install --repo-update`, then in your xCode clean build folder and delete derived data, try build again after that. – inokey Sep 18 '18 at 13:15
  • It didn't work @inokey – Jordi Gámez Sep 18 '18 at 13:20
  • 1
    Check deployment target in the project settings. – inokey Sep 18 '18 at 13:23
  • The iOS deployment target is set to 9.0, is that a problem? Thanks again @inokey – Jordi Gámez Sep 18 '18 at 13:26
  • Ok, now, I'm not 100% sure but you better check your pods repos. They may have version suitable for iOS 9.0 and with your pod update your could accidentally overwrite it with the newest version that are being released right now with new iOS being public. – inokey Sep 18 '18 at 13:30
  • @inokey I have made a quick search in the project and I found that #import is called inside a CocoaPod that I'm using in the project. Could this be the problem? – Jordi Gámez Sep 20 '18 at 06:11
  • That very well could be an issue. I had problems with Swift common crypto before when updating the system. The only solution to that was removing pods folder and performing a clean pod install. – inokey Sep 20 '18 at 06:40
  • Thanks @inokey I have write my steps and I hope it can help other people, but it's very weird because suddenly it started to work...! – Jordi Gámez Sep 20 '18 at 07:23
  • Okay, I'm glad you've figured this out. It's also nice to know that common crypto is now a native framework. – inokey Sep 20 '18 at 07:40
  • 1
    Another possible solution (that worked for me) is found here: https://stackoverflow.com/a/52481653/96153 – yo.ian.g Oct 05 '18 at 03:06
  • https://stackoverflow.com/a/52722094/822420 it may help you – Ramkumar Paulraj Oct 09 '18 at 13:27
  • Clean Build, Remove Derived Data is common solution for it. – Solid Soft Nov 14 '18 at 08:31
  • After updating Xcode, like Xcode 10.3, should restart mac – onmyway133 Jul 30 '19 at 10:47
  • I know that you already tried restarting and rebuilding everything, the only working solution is - Reinstall the Xcode!!!! – Kampai Mar 23 '20 at 13:30
  • In my case I cloned a project from work, and was building with Xcode 12.5.1 while the iOS Project Deployment Target was on 14.5, so opening with a newer version of Xcode solved it. – 7RedBits.com Oct 12 '21 at 14:18
  • just cleaning project works using Shift + Command + K or Option + Shift + Command + K. – Yogesh Rathore Apr 14 '23 at 18:29

23 Answers23

147

For me, just cleaning project works using ShiftCommandK & OptionShiftCommandK.

George
  • 25,988
  • 10
  • 79
  • 133
Mahgolsadat Fathi
  • 3,107
  • 4
  • 16
  • 34
91

You're most likely not reading the entire error message. If you look above the "Command CompileSwift failed with a nonzero exit code" message you should find some specification like this:

enter image description here

In this example I had two files with the same name. Once I fixed it everything worked as it should.

Samy
  • 1,172
  • 9
  • 13
53

I attempted

  • Closing & Reopening Xcode
  • Cleaning Build Folder
  • Running pod install --repo-update

and all of these still did not fix the problem.

Restarting the Mac did the trick!

codeherk
  • 1,609
  • 15
  • 24
  • 2
    I updated my xcode to 10.3 and faced similar issues. I tried all the tricks nothing helped but as indicated in here in bold, "Restarting the mac did the trick", it worked for me. – Yoku Jul 23 '19 at 11:29
  • Hate to say that but restarting my mac did solve the issue for me. BTW I got this error after upgrading Xcode to 10.3. – luin Jul 24 '19 at 06:39
  • @luin yeah restarting the Mac can be pretty annoying but it worked lol. I figured I’ll share my solution hoping it could save others the trouble. – codeherk Jul 24 '19 at 06:44
  • 2
    Before you try anything "Restart the Mac". This should be top answer. – Rammohan Raja Aug 09 '19 at 21:23
  • 1
    Clean Build Folder worked perfectly for me (only clean, without restarting the Mac). – DaniChi Sep 03 '19 at 16:22
  • 1
    The thing with restarting the Mac might look funny, but it's a valid point! In my case I modified the `.bash_profile`, then quickly in the terminal loaded it: `source .bash_profile` and proceeded to work. So after few days when I returned back, I had again issues because `.bash_profile` was not loaded... Then restarting does indeed help :) – Andy Res Jul 21 '20 at 13:48
  • @DaniChi That worked for me too. I tried restarting XCode, and was preparing to restart the Mac when I saw your comment. – Manngo Oct 26 '20 at 03:19
  • This should be top answer... – Jack David Evans Feb 08 '23 at 14:37
49

Currently my build is working. Here you are the steps I tried until it finally worked:

  1. Search in the whole project the word CommonCrypto.
  2. If you have a Pod containing that header import, remove this Pod from the Podfile and perform a pod install.
  3. Clean and build the project.
  4. Add again the Pod to the Podfile and perform a pod install.
  5. Clean and build the project again using a real device if possible.

And If you don't have that Pod, maybe you can try by making the same steps with some old Pod that you may encounter in your project.

Added information: also If you have some code error inside a Pod, first you need to solve that code problem and then try to compile again the project.

I'm going to copy the changes made in my project.pbxproj. I know it's not very helpful but it's the only thing that have changed in the git difference commit:

Removed: BDC9821B1E9BD1B600ADE0EF /* (null) in Sources */ = {isa = PBXBuildFile; };
Added:   BDC9821B1E9BD1B600ADE0EF /* BuildFile in Sources */ = {isa = PBXBuildFile; };

I hope this can help,

Regards.

Jordi Gámez
  • 3,400
  • 3
  • 22
  • 35
40

I had this issue and changing the Compilation Mode setting for the project from Incremental to Whole Module fixed it for me.

enter image description here

Werner Altewischer
  • 10,080
  • 4
  • 53
  • 60
  • 5
    This helped me fix the problem. When I turned Whole Module mode on it started reporting actual errors to me rather than a generic high level failure. Once all fixed you can then change it back to Incremental, if that's what you'd like. – CMash Oct 30 '18 at 08:53
  • 1
    Any hints where to find this setting? i looked under build settings for my targets and build settings for the project and cant find it (by searching for it) maybe because im on an objective-c project thats quite old? – Fonix Nov 20 '18 at 07:39
  • It's under Swift Compiler -> Code Generation. Alternatively it may sometimes be listed under User Defined -> SWIFT_COMPILATION_MODE – Werner Altewischer Nov 20 '18 at 08:31
  • 1
    This did not solve the issue for me, it did nothing. – Van Du Tran Nov 26 '18 at 16:55
  • 2
    Thanks for the tip and it fixed my problem. My case is to migration project to **Xcode 10.2 and Swift 5**. I wrote detail explanation about it in my blog: https://davidchuprogramming.blogspot.com/2019/03/xcode-102-swift-5-ios-122-released.html – David.Chu.ca Mar 29 '19 at 18:21
  • For me this solves the problem, which seems to be caused by having a regular compiler error in an expression that takes long to evaluate, while also having the compiler flag `-Xfrontend -warn-long-expression-type-checking=100` set. – Aderstedt Jun 12 '19 at 18:14
  • There is no `Compilation Mode` build setting. – Iulian Onofrei Oct 30 '19 at 08:29
12

My problem was I had due to the non-existence of a native Swift CommonCrypto, used a bridging header and a target that with some magic included it in the build. Since CommonCrypto is now native, I solved the problem by removing the target and the #import and instead added an import CommonCrypto where I used it.

Omid Ariyan
  • 1,164
  • 13
  • 19
  • Yes! I had this too (No cocoapods or anything like that, just CommonCrypto) Thanks for saving my day – Orion Edwards Sep 25 '18 at 11:47
  • @OmidAriyan Hello, guys this issue had stopped me from working for the past two weeks :s, I tried almost all solutions found on this website but its simply not working. For your answer I didn't get the point actually. Would you please clarify your answer for me in details? What is the problem and what should I do in steps please :(:( – mojtaba al moussawi Nov 20 '18 at 16:21
  • @mojtabaalmoussawi I had basically followed https://stackoverflow.com/a/42852743/1038245 in order to use CommonCrypto in my project before Xcode 10 was released. I just tried to undo that to fix the error that is the topic of this question. – Omid Ariyan Nov 21 '18 at 16:12
12

This is a known issue with Swift 4.2 and Xcode 10. I found an article here that fixed it for me: https://github.com/Yummypets/YPImagePicker/issues/236

In short, go to your projects build settings, and add a user defined setting named SWIFT_ENABLE_BATCH_MODE and set its value to NO.

Previously, I tried each of the following methods suggested elsewhere (rebuild, exit Xcode, clean and rebuild, purge Derived Data files). None of them worked.

Once I added the user define build setting per the article, Swift then told me the true error. In my case, it was a missing }, but it could be any number of problems.

cdeerinck
  • 688
  • 6
  • 17
10

I did the following and it works like charm:

  • Open Xcode Preferences (Command,)
  • Go to the Locations tab
  • Click on the small gray arrow which shows URL path for Derived Data
  • The finder window will open up and show the following folders:

    • DerivedData
    • Archives
    • iOS DeviceSupport
    • UserData
    • DocumentationCache
    • iOS Device Logs
  • Quit Xcode

  • Delete the DerivedData folder (it just contains a cache from previous builds)
  • Restart Xcode
Spomky-Labs
  • 15,473
  • 5
  • 40
  • 64
Mile Dev
  • 652
  • 6
  • 13
7

Here is the solution: - Go to build settings - Search SWIFT_COMPILATION_MODE - If this is "Whole Module" for Release configuration then change it to "Incremental". - Archive now.

When you change the setting to "Incremental" the process succeeds.

Ankit Saini
  • 284
  • 2
  • 14
7

Running pod install --repo-update and closing and reopening x-code fixed this issue on all of my pods that had this error.

ricks
  • 3,154
  • 31
  • 51
6

Mine was a name spacing issue. I had two files with the same name. Just renamed them and it resolved.

Always gotta check the 'stupid me' box first before looking elsewhere. : )

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
4

Let me share my experience for this issue fix.

Open target -> Build phases -> Copy Bundle Resources and remove info.plist.

Note: If you're using any extensions, remove the info.plist of that extension from the Targets.

Hope it helps.

4

in my case the problem was due to watchkit extension being set to swift 3 while the main project's target was set to swift 4.2

Itay Bianco
  • 697
  • 6
  • 16
  • This right here! This was bugging me for so long! When you are adding frameworks/extensions to your libraries, make sure that both the extension and the library are looking at the same Swift Language Version! Thanks @Itay – Zez3 Aug 21 '19 at 12:59
3

Class re-declaration will be the problem. check duplicate class and build.

2

It seems like this is a pretty vague error, so I will share what I did to fix it when I ran into this:

Using Xcode 10.1 and Swift 4.2 I tried pretty much all the suggestions here but none of them worked for me, then I realized a dependency I was using was not compatible with Swift 4.2 and that was causing me to get this error on other pods. So to fix it I just had to force that pod to use Swift 4.0 by putting this at the end of my Podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if ['TKRadarChart'].include? target.name
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '4.0'
            end
        end
    end
end
fphilipe
  • 9,739
  • 1
  • 40
  • 52
Tyler927
  • 197
  • 1
  • 12
1

I searched the whole web but I couldn't find any solution for this problem. I managed to REMOVE the "Common Crypto" path in the section: IMPORT Paths in Build settings. The problem seems to be that "common crypto" now exists in the foundation in the ios 12 sdk.

So":

Simply remove the path for file "common crypto" in the build settings and the project will build like a charm! :)

sajede Nouri
  • 109
  • 4
  • Can you clarify where in your build settings that is? Is it a search path? – yo.ian.g Oct 05 '18 at 02:55
  • 1
    Yes. Go to Build settings, select "All" Tab instead of "Basics" or "Customized". Now search for : "Swift Compiler - Search Paths" Here if there exists a path for common crypto, remove it. – sajede Nouri Oct 05 '18 at 05:54
1

Just adding to this question. My issue didn't have anything to do with CommonCrypto. It created a new Single App application and tested to run. Compiler was complaining about using Swift 4.2

Changing the Swift language to version 4.0 in Build settings fixed the issue. Not sure if this is a bug.

Alexander
  • 1,424
  • 18
  • 23
1

I got this randomly since Xcode 10. I think it occurs when I change something in the code while building starts. The next build works every time.

heyfrank
  • 5,291
  • 3
  • 32
  • 46
1

I also encountered the same issue and I did what @cdeerinck suggested and got to the following link which suggested adding a user-defined variable to the Build Settings to disable batch mode i.e. add a new user defined variable named SWIFT_ENABLE_BATCH_MODE and set it to NO, I was able to get more insights into the issue and I got to know that the error was in a framework using CommonCrypto which was added to it(by me) but since Xcode 10 it is exposed natively as part of Swift (for Apple platforms only), and adding it (or its existence from the previous version) was causing a name collision and hence it was throwing the error. To know more refer to the this link which explains the issue in more detail.

Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21
1

I have the same issue and my solution is change a little thing in Build Settings

SWIFT_COMPILATION_MODE = singlefile;
SWIFT_OPTIMIZATION_LEVEL = "-O";
Daniel Le
  • 171
  • 1
  • 4
  • 8
1

In my case, there was a duplicate entry for a framework in the Input Files of Carthage framework section in Build Phases

niks
  • 544
  • 11
  • 28
1

ERROR = Command CompileSwiftSources failed with a nonzero exit code

In my case When I found this error, I got cramped with compilation. But when I see some related problem answers. I found a duplication file on my project. Where the same viewController was there as a class file. So yeah when I realized it I changed it name to new one. And yeah things changed!!!

Harsh Vyas
  • 11
  • 1
1

For me, the error message said I had too many simulator files open to build Swift. When I quit the simulator and built again, everything worked.

mef27
  • 379
  • 4
  • 15