Swift build fails inside xcode build script

I have a build phase script in Xcode that used to build and run a Swift package. Before Xcode 11.4 I was able to build and run that package with swift run --package-path ProjectDir. Since Xcode 11.4 I get the following warning and error:

warning: using sysroot for 'iPhoneSimulator' but targeting 'MacOSX'

error: module 'Swift' was created for incompatible target x86_64-apple-ios13.0: /var/folders/rc/y4glr2yj3xl_qk39vg4f4t_00000gn/C/org.llvm.clang.MyStuff/ModuleCache/Swift-3LLGH9HFMRPI6.swiftmodule

This only happens when I build/run the package from the build script. If I run the same command manually from the terminal, it all works great. So it's probably some environment variables the default swift build command relies on? Question is, what are they and how can I safely adjust them without affecting the xcode build process?

If you are typing swift code inside the text box in BuildPhase then in the Shell text box type /usr/bin/xcrun --sdk macosx swift

If you are executing a .swift file then use the shebang #!/usr/bin/xcrun --sdk macosx swift

Hmm I'm not quite doing either one. I have a Swift package (a command line tool) that I was invoking with swift run

A Run Script build phase has a checkbox, “Show environment variables is build log”, that you can enable to investigate the script’s environment. I did a quick test here (with Xcode 11.4) and the most likely candidate is SDKROOT. Having said that, there are a lot of environment variables that reference the current SDK, so it’s easy to imagine something else tripping you up.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Actually, it looks like the first suggestion worked:

/usr/bin/xcrun --sdk macosx swift run ...

I didn't realize I could use the xcrun command followed by swift build/run <arguments...>

Terms of Service

Privacy Policy

Cookie Policy