SwiftSyntax does not work with --static-swift-stdlib

building the dependency SwiftSyntax with --static-swift-stdlib does not work

error: no such module '_InternalSwiftSyntaxParser'
@_implementationOnly import _InternalSwiftSyntaxParser
                            ^

it works without --static-swift-stdlib. how do i fix this? i am cross-compiling from a docker image, so i need --static-swift-stdlib.

2 Likes

I think you could probably hack around it a bit to link the dynamic version of this library, as long as in the end you copied it along as well as your final binary, but that's definitely less than ideal.

There are some related tickets [SR-9038] Swift Syntax requires the use of dynamic linking · Issue #442 · apple/swift-syntax · GitHub [SR-14001] Missing static library of libSwiftSyntaxParser for static build · Issue #313 · apple/swift-format · GitHub

I started working on a change to include both a static and dynamic version of the library in the toolchain Add static library configuration for _InternalSwiftSyntaxParser by keith · Pull Request #36151 · apple/swift · GitHub but got stalled on the cmake setup.

Since then I've actually been wondering if we could just change it to be static all the time instead, since I'm not entirely sure I see the purpose of it being dynamic (aside from slightly reducing disk usage on macOS). You being forced to use a compatible version of Swift + this library doesn't always seem useful to me, and while theoretically newer versions of Swift might not be fully supported by older versions of the library (I assume?), we've been shipping a vendored old version of the library for a while (to avoid the general issue you're hitting) and haven't hit any of these incompatibilities. But I'd love to hear from some folks on what the original trade-offs were for making that decision.

2 Likes

i ended up just recreating the directory structure of the swift installation in the deployment target in my dockerfile, which worked, surprisingly.

i agree. i don’t understand why i cannot build a SwiftSyntax module that understands a different version of Swift than the one that compiled the parser itself. those two things shouldn’t be connected…

1 Like

I started vendoring a Swift package for this for macOS, but something similar could be done for Linux as well GitHub - keith/StaticInternalSwiftSyntaxParser: A static library for using SwiftSyntax / lib_InternalSwiftSyntaxParser

3 Likes