Using swift-corelibs-foundation on Mac OS?

I'm writing a Swift service that I will be deploying to several Linux servers. I'm developing the service on my Macbook Pro, with VS Code/sourcekit-lsp. When I click through to definitions of things, I'm (obviously) taken to the Darwin Foundation definitions.

The helpful advice over on GitHub - swift-server/guides: Guides for building, debugging and deploying Swift Server applications is that it's...critical to test on Linux.

Swift supports architecture-specific code. By default, Foundation imports architecture-specific libraries like Darwin or Glibc. While developing on macOS, you may end up using APIs that are not available on Linux. Since you are most likely to deploy a cloud service on Linux, it is critical to test on Linux.

Obviously, I can't account for every difference, but it'd go a long way to be able to compile against swift-corelibs-foundation (and have sourcekit go to those definitions, so I can see ahead of time what will give me an unimplemented exception).

I understand that the usual mode should certainly to be compiling/linking against the native Foundation, but when targeting Linux, it'd be great to use corelibs-foundation...

This is definitely something I want to see better tools for. In the short term, though, have you considered using Docker for Mac? It will give you the ability to rapidly get a Linux userspace with Swift installed, which should at least let you get the compile done.

Docker is definitely the way to go and ensuring you run CI on Linux as well.

There's also a discussion to be had about standardising the behaviour of SwiftPM (and therefore swift test, swift run, swift build etc across all platforms. This would provide a much better experience for people working across platforms though I suspect there'll be significant push back and this is probably off topic for this thread

1 Like

I decided to actually pitch it so I've written it here

You should still test whether the software is working on a Linux machine (or Docker), but if you just want to see whether it is compiling, a Linux cross compiler toolchain from SPMDestinations might work for you. (I usually use this prior committing things, and then have GH Actions run the package tests)

I appreciate all the thoughts/advice so far! I've poked at using Docker to do the compiles (success!) but I haven't figured out how to get VS Code to rely on that sourcekit-lsp. I'll continue poking. It's good to hear this isn't completely crazy.

Can you run VSCode within the Docker? I thought it had some kind of remoting feature (not a user myself).

There is an old post from @IanPartridge that explains a VSCode setup:

Not sure how uptodate this is though.

Official docs (without mentioning Swift):

Using the "community provided" Docker container and a few configuration tweaks inside VS Code and I am able to easily compile and test the Linux platform builds. Success! I much appreciate all the hints.

I got sourcekit-lsp providing completions, etc... I've got one part of my dream unfulfilled: jump to definition/jump to implementation for swift-corelibs-foundation and the standard library just...don't jump anywhere. Hover-docs and completion for Foundation work fine, but unlike on Mac OS I cannot actually jump to the definition.

I suspect this is due to the Docker image needing the swift-corelibs-foundation etc... sources instead of just precompiled binaries. I can probably figure this out, but figured I'd continue to document what I'm running into.

Nice, can you share with what setup you ended up with? Would also like to give it a try :slight_smile:

Hey Tobias, short of actually getting a write up of what I'm doing, I threw things in a git repo to demonstrate.

git clone https://repo.or.cz/swcgi.git

Then, launch VS Code and install the "Remote Development" extension (which includes the Remote-Containers extension). Once its installed, Cmd-Shift-P (command pallet) and then type/select "Open Folder in Container". Select the cloned repository, and VS Code will start assembling the docker container and getting you connected.

In the repo, the .devcontainer director contains the configuration that's going to be used. There's the actual Dockerfile (which starts with the swift5.3 image), as well as a devcontainer.json file which tells the remote VS Code to install swift plugins and points them to the right sourcekit-lsp.

As I mentioned before, click-to-definition works for project files and dependencies, but not for Foundation or the Stdlib. Still working on that.

Hope this helps someone!

1 Like