dyld: Library not loaded: when adding SwiftSyntax via SPM

Viewed 1564

I did the following:

  1. Create a new macOS project.
  2. Include a dependency on https://github.com/apple/swift-syntax.git via Swift Package Manager.
  3. Choose exact version and use version number 0.50300.0 (since I'm using Xcode 12.4 and that is the highest version supported).

When I launch the app, I get the runtime error:

dyld: Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib

Referenced from: ....

Reason: image not found

The readme mentions that:

SwiftSyntax depends on the lib_InternalSwiftSyntaxParser.dylib/.so library which provides a C interface to the underlying Swift C++ parser. When you do swift build SwiftSyntax links and uses the library included in the Swift toolchain. If you are building an application make sure to embed _InternalSwiftSyntaxParser as part of your application's libraries.

You can either copy lib_InternalSwiftSyntaxParser.dylib/.so directly from the toolchain...

How can I find this file in the toolchain?

How do I "embed _InternalSwiftSyntaxParser"?

1 Answers
  1. Find the file in your toolchain by locating it within the Xcode.app bundle:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/lib_InternalSwiftSyntaxParser.dylib
    
  2. Copy this file to your Xcode project's directory.

  3. Drag the file into the Project navigator in Xcode.

    This should automatically add it to your target's Frameworks and Libraries.

  4. Under your target's settings, change lib_InternalSwiftSyntaxParser.dylib to "Embed and Sign".

Related