Command-line build fails with mainfest parse error: "no such module 'PackageDescription'"

I'm a beginner, trying to create a Swift command-line application (and building it on the command-line).

My host:

% uname -a
Darwin foo.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

Swift version:

% swift --version
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
Target: x86_64-apple-darwin19.4.0

I want to create a new executable:

% mkdir zzz
% cd zzz
% swift package init --type=executable
Creating executable package: zzz
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/zzz/main.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/zzzTests/
Creating Tests/zzzTests/zzzTests.swift
Creating Tests/zzzTests/XCTestManifests.swift

and then try to build it:

% swift build
zzz: error: manifest parse error(s):
zzz/Package.swift:4:8: error: no such module 'PackageDescription'
import PackageDescription
       ^

If I create a Mac OS project in Xcode, the project builds and runs successfully. I want to duplicate that success using Swift directly on the command-line. Is there some kind of a PATH variable I have to set to have Swift find the PackageDescription pacakge?

Does this look familiar to anyone?

I have never seen this on macOS. I did see something like it on Linux years ago when I installed the toolchain in one place, and made a symlink to the swift executable from somewhere else, and put that symlink in PATH instead. Then SwiftPM was looking for the some of the other toolchain pieces relative to the symlink instead of relative to the executable.

How did you install Swift?

I think I installed it using Xcode… I don't see it in my list of brew packages. Is there anything I can run that would help me know? I see this:

% ls -l `which swift`
-rwxr-xr-x  1 root  wheel  31488 Mar 17 09:42 /usr/bin/swift
% ls -l `xcrun --find swift`
-rwxr-xr-x  1 root  wheel  94775104 Dec 19 00:44 /Library/Developer/CommandLineTools/usr/bin/swift

Also:

% /usr/bin/swift --version
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
Target: x86_64-apple-darwin19.4.0
% /Library/Developer/CommandLineTools/usr/bin/swift --version
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
Target: x86_64-apple-darwin19.4.0

Is this helpful?

If you’d tried to install it in some exotic fashion, you’d know.

I’m stabbing in the dark here, but the particular library that isn’t being found can be searched for like this:

find / -name libPackageDescription.dylib

(You can add sudo if that finds nothing and you think it might be in a directory you don’t normally have permissions for. But that is unlikely.)

With my standard Xcode install, I see this (besides a wall of permission denials):

Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4/libPackageDescription.dylib
Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2/libPackageDescription.dylib
System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4/libPackageDescription.dylib
System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4_2/libPackageDescription.dylib

Ok, similar for me, but I must admit that while poking around I had not installed the Swift toolchain .pkg file before my first post (I didn't know it was a thing… I've gone back and carefully followed the Download instructions from swift.org):

% find / -name libPackageDescription.dylib 2>/dev/null
/Library/Developer/CommandLineTools/usr/lib/swift/pm/4_2/libPackageDescription.dylib
/Library/Developer/CommandLineTools/usr/lib/swift/pm/4/libPackageDescription.dylib
/Library/Developer/Toolchains/swift-5.2.1-RELEASE.xctoolchain/usr/lib/swift/pm/4_2/libPackageDescription.dylib
/Library/Developer/Toolchains/swift-5.2.1-RELEASE.xctoolchain/usr/lib/swift/pm/4/libPackageDescription.dylib

Just for good measure, I've got the following in my $PATH:

/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin

and I re-created the new project, just in case having the toolchain up front was important (same effect: no such module 'PackageDescription')

I have also set the TOOLCHAINS environment variable to swift (also tried swift-latest).

How does swift build know to look in /Library/Developer/Toolchains in the first place?

You shouldn’t have to do anything at all. After installing Xcode via the App Store and opening it once, $ swift build should just work.

The other fancy stuff mentioned on Swift.org - Download Swift is only if you are wanting to use an older version of Swift or try it out in its development state or something.

How does swift build know to look in /Library/Developer/Toolchains in the first place?

Because in looks relative to itself by default. The source is here. The bin directory discovery is here, so maybe you can help it in the right direction with SWIFTPM_CUSTOM_BINDIR?

But I repeat that none of this should be necessary in the first place. It should just work. Hopefully someone more knowledgeable than myself can figure out why it didn’t in your case.

Thanks Jeremy. Here is something interesting. If I build like this:

% /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift build
[3/3] Linking zzz

Success!

However, this is not the same swift as /usr/bin/swift or the one found by xcrun:

% /Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift --version
Apple Swift version 5.2.1 (swift-5.2.1-RELEASE)
Target: x86_64-apple-darwin19.4.0

I have three copies of the swift binary on my machine now. My question is this: do I need to fix my $PATH somehow? Make symlinks? Is there anyway to "clean up" swift and reinstall it properly? What's safe to remove? Does anyone else have this many copies of swift?

For future me who runs into this again, this command fixed something:

% sudo xcode-select --reset
4 Likes