% 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?
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.
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
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):
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.
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?