Using swift-foundation with Linux static SDK

I'm trying to build an executable using the latest Linux Static SDK. I want to use Foundation, but can't seem to get the tooling right. I did install the matching toolchain and static sdk, setting the current toolchain via the environment: export TOOLCHAINS=<snapshot_name>.

I built a demo project first:

swift package init --type executable
swift build --swift-sdk x86_64-swift-linux-musl

This works fine. Then I added import Foundation to main.swift which results in a series of repeated errors: <unknown>:0: error: missing required module '_FoundationCollections'. I was under the impression the nightly 6.0 builds included Foundation automatically? Ok maybe I need to be explicit.

Next I tried adding swift-foundation as a dependency in Package.swift and imported FoundationEssentials instead. This compiles fine. However, my project is also using the App Store Server Library and the types seem incompatible. For example FoundationEssentials.Data is not accepted by the App Store Server Library that expects Foundation.Data.

This seems like it should be obvious (it probably is) but I'm not sure how to use a Foundation dependency on a Linux executable. Searched through all the forum posts, documentation, and how-to's that I could find so I did try to do my own homework but no luck. Appreciate any guidance.

2 Likes

Thanks for bringing this up! I believe this indeed should work. Can you confirm which Linux Static SDK you're using? I see that the main one hasn't been updated since early July but the release/6.0 one is quite recent. Any chance you're using the one from July 2nd / main?

Yes Foundation is included in the toolchain by default, no need to specify it in a package dependency. The incompatibility of types is expected behavior since the package build is only used for ease of local development and is not intended to be depended on by clients, so hopefully we should be able to get this working without that package dependency

Glad to hear I'm not totally off base. Here's what I'm using:

swift toolchain 6.0 snapshot from 9/11/2024
linux static sdk from 9/11/2024
swift-foundation 6.0 RELEASE (tagged 9/13/2024)

Thanks for the info! Yeah something seems off here (unless my understanding is incorrect too). I pinged some relevant folks on this GitHub issue that describes the problem: Missing required module '_FoundationCollections' when building with the Static Linux SDK · Issue #917 · apple/swift-foundation · GitHub, hopefully we can figure out what might be misconfigured here.

1 Like

Thanks! I’ll follow along there as well.

I'm trying to do the same and encounter the same issue. Seams Foundation is not included in the toolchain.

Although I ;like the quick response and reference to the issue I do not thing it is the issue that @seanmrich is stating. The issue is that import Foundation fails due to some weird reason.

The doc of github/apple/swift-foundation state that it should be included in the toolchaing. But as @seanmrich describes they are not.

I took the liberty to report this as a bug in swift-foundation too. It also happens when just doing swift build evan without the --swift-sdk x86_64-swift-linux-musl

See bug report

There are two issues in play here: one with the Static Linux SDK and one with using the OSS toolchain on Darwin platforms when importing the Darwin module. The issue that @seanmrich encountered is the former (where there is a missing _FoundationCollections module) and that is actively being resolved by a change in swift-collections (Missing required module '_FoundationCollections' when building with the Static Linux SDK · Issue #420 · apple/swift-collections · GitHub).

The issue you are describing is an issue with the toolchain in general when using the OSS toolchains on Darwin platforms which results in the compiler crash that you reported. @ian-twilightcoder and folks have been looking into this. I noted the GitHub issue that was previously reported that you can follow along with. The reason that import Foundation causes the issue here is because Foundation imports the Darwin module and the issue appears to be with the toolchain's Darwin module.