swiftly icon indicating copy to clipboard operation
swiftly copied to clipboard

Swift libraries lookup / Current toolchain symlinks

Open kattouf opened this issue 6 months ago • 0 comments

Hi! I'm working on a project that uses swift-package-manager - package-swift-lsp

Problem

While working on Linux adoption, I installed the latest Swift (6.1.2) via the latest Swiftly (1.0.1). Everything worked fine from the terminal when building packages using swift.
However, I encountered an issue when using the installed Swift toolchain by the SwiftPM package — it could not find the required libraries and produced the following error:

2025-08-08T10:33:45+0700 error com.kattouf.package-swift-lsp : [PackageSwiftLSPLibrary] SwiftPM: Invalid manifest (compiled with: ["/home/kattouf/.local/share/swiftly/bin/swiftc", "-vfsoverlay", "/tmp/TemporaryDirectory.F3I765/vfs.yaml", "-L", "/home/kattouf/.local/share/swiftly/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/home/kattouf/.local/share/swiftly/lib/swift/pm/ManifestAPI", "-swift-version", "6", "-I", "/home/kattouf/.local/share/swiftly/lib/swift/pm/ManifestAPI", "-package-description-version", "6.0.0", "/home/kattouf/Development/package-swift-lsp/Package.swift", "-o", "/tmp/TemporaryDirectory.Jk6NJ6/package-swift-lsp-manifest"])
/home/kattouf/Development/package-swift-lsp/Package.swift:4:8: error: no such module 'PackageDescription'
 2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
 3 | 
 4 | import PackageDescription
   |        `- error: no such module 'PackageDescription'

This issue did not occur when Swift was installed manually from the official tarball — in that case, SwiftPM was able to locate all required libraries without additional configuration.

Investigation

From the error message, it’s clear that SwiftPM is looking for libraries at:

/home/kattouf/.local/share/swiftly/lib/swift/pm/ManifestAPI

Based on a quick review of SwiftPM’s source code and "googling", this path seems to be calculated relative to the swift binary location.

When I checked the Swift path provided from error, I found that /home/kattouf/.local/share/swiftly/lib does not exist, which matches the error:

$ cd ~/.local/share/swiftly
$ ll
total 24
drwxrwxr-x  4 kattouf kattouf 4096 Aug  8 09:57 ./
drwx------ 21 kattouf kattouf 4096 Aug  8 09:58 ../
drwxrwxr-x  2 kattouf kattouf 4096 Aug  6 19:53 bin/
-rw-rw-r--  1 kattouf kattouf  237 Aug  6 20:41 config.json
-rw-rw-r--  1 kattouf kattouf  386 Aug  8 09:57 env.sh
drwxrwxr-x  4 kattouf kattouf 4096 Aug  6 20:40 toolchains/

I then located the expected libraries (and more) under:

/home/kattouf/.local/share/swiftly/toolchains/6.1.2/usr/

This directory appears to contain all the required files for the Swift 6.1.2 toolchain.

To test my theory, I modified /home/kattouf/.local/share/swiftly/env.sh to point SWIFTLY_BIN_DIR directly to the toolchain’s usr/bin directory:

- export SWIFTLY_BIN_DIR="/home/kattouf/.local/share/swiftly/bin"
+ export SWIFTLY_BIN_DIR="/home/kattouf/.local/share/swiftly/toolchains/6.1.2/usr/bin"

After this change, SwiftPM started working properly.

Solution Idea

One possible fix would be to use a current symlink to the active toolchain’s usr/bin directory, instead of pointing SWIFTLY_BIN_DIR to swiftly/bin.

This approach would not only resolve the SwiftPM issue, but also make it easier to reference the active toolchain’s full directory path for other use cases (e.g., scripts, tooling integrations, custom build processes).

In /home/kattouf/.local/share/swiftly/env.sh:

- export SWIFTLY_BIN_DIR="/home/kattouf/.local/share/swiftly/bin"
+ export SWIFTLY_BIN_DIR="/home/kattouf/.local/share/swiftly/toolchains/current/usr/bin"

And then set up the symlink (from terminal for testing purposes):

$ cd ~/.local/share/swiftly/toolchains
$ ln -sfn 6.1.2 current

~/.local/share/swiftly/toolchains$ ll
total 16
drwxrwxr-x 4 kattouf kattouf 4096 Aug  8 10:21 ./
drwxrwxr-x 4 kattouf kattouf 4096 Aug  8 10:33 ../
drwxrwxr-x 3 kattouf kattouf 4096 Aug  6 19:53 6.1.2/
drwxrwxr-x 3 kattouf kattouf 4096 Aug  6 20:40 6.1-snapshot-2025-03-25/
lrwxrwxrwx 1 kattouf kattouf    5 Aug  8 10:21 current -> 6.1.2/

After these changes, SwiftPM was able to find all the required libraries, and the Swift CLI worked correctly.

Before:

$ which swift
/home/kattouf/.local/share/swiftly/bin/swift

After:

$ which swift
/home/kattouf/.local/share/swiftly/toolchains/current/usr/bin/swift

kattouf avatar Aug 08 '25 04:08 kattouf