No such module 'PackageDescription' sourcekitd

I'am testing SourceKit-LSP in vscode & linux and I am failing to make it work properly.
It doesn't seem to recognize some of modules, for example: it recognizes Foundation but not PackageDescriotion.

I have tried with multiple builds of Swift with no positive results.

Would you mind filing a bug on bugs.swift.org, component "SourceKit-LSP" with some details about what version of sourcekit-lsp you're using and what exactly you're trying? For example, does this reproduce the error?

cd /tmp
mkdir pkg
swift package init
code .
# Open Package.swift in VSCode; are there errors?

Thank you Ben!
That was the issue, workspace was my home folder

I have the same problem and it is not because the workspace is in my home folder.

I have a tmp directory in my home directory:

cd ~/tmp
mkdir pkg
cd pkg   # <== This was not in your instruction but I assume that was needed.
swift package init
code .

When I then open Package.swift I get 1 error and 3 warnings. The error is:

no such module 'PackageDescription'

The warnings are all valid.

I am running this on macOS 10.15.3 using the latest version of sourcekit-lsp.

Some additional information. In my "normal" code in my Sources directory I have two sub-directories where one contains a module and the other the application. Also the import from the module from the application folder shows this problem. Also all packages that are downloaded through Packages.swift show the error.

Let me know if you have additional questions.

Which toolchain/sourcekit-lsp are you using exactly?

I'm not certain how I can find out the version but I used the following steps to install it:

git clone https://github.com/apple/sourcekit-lsp.git
cd sourcekit-lsp
swift build
mv .build/debug/sourcekit-lsp /usr/local/bin

Git log shows that I am using 616510e from the tree.

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

I believe the issue is you are using the latest master sourcekit-lsp combined with a toolchain (5.1) that has an older swiftpm. Because of how we use libSwiftpm these need to be in sync. If your goal is to use sourcekit-lsp with the 5.1 toolchain, you could try building the swift-5.1-branch of sourcekit-lsp (or get sourcekit-lsp out of the 5.1 toolchain download on Swift.org/downloads). If your goal is to hack on sourcekit-lsp, you would want to use a newer toolchain instead. More info at sourcekit-lsp/Development.md at main · apple/sourcekit-lsp · GitHub

Thanks. I will try this tomorrow.

And I can confirm that this solves it. I have used the version found in the toolchain.

hello. I have same issue like that.
I'm using swift-5.1-branch and Swift 5.1.1+ toolchain.
Swift version on my Mac is 5.1.3-dev.

Can anyone help me out?

If I understand correctly, you're using a toolchain from swift.org, but building sourcekit-lsp from source? If so, could you try the sourcekit-lsp that is included in the toolchain (/usr/bin/sourcekit-lsp)? Also, have you verified that the correct toolchain and sourcekit-lsp are being used? If you look in Activity Monitor while it's running, you can choose the sourcekit-lsp and SourceKitService processes and check under "info" > open files, and see that they are using the correct path.

Thank you for reply.

  1. you mean, download the brach and 'swift build' in sourcekit-lsp folder and using .build/~/sourcekit-lsp directory in vscode, which is wrong way??
  2. I can't find ( /usr/bin/sourcekit-lsp ). which means I don't install something?
  3. I found processes both and checked lots of directory.

this is about SourceKitService


this is about sourcekit-lsp

are they using correct path?

hello I think I solved the problem. but I have another problem.
I just opened sourcekit-lsp swift project file on vscode to check if it works or not.
As you can see, only that XCTest has a problem like

cannot load underlying module for 'XCTest'

Weird thing is another import XCTest works in another swift file! What's wrong with that?

Glad you figured out the first issue! For the failure to import XCTest inside LinuxMain.swift, I think the problem is that file is only part of the SwiftPM target on Linux (CC @Aciid to confirm), so if you open it on macOS we have no compiler arguments for it. If that's the issue, it should only impart LinuxMain.swift and no other files.

So you mean, that file just works in Linux and the error like

cannot load underlying module for 'XCTest'

is the one I can ignore and develop other files for sourcekit-lsp even if the error comes up?
I'm not sure I got it all. :sweat_smile:

No, I'm saying that this error is probably expected in LinuxMain.swift when running on macOS. If you're seeing it elsewhere, it's a separate issue that we can look into.

oh I see. Yes I have that issue in LinuxMain.swift on macOS with VScode.
So what shoud I do to fix it?

@Aciid is there a reasonable way for us to get compiler arguments for LinuxMain.swift when building on macOS?

@blangmuir Hey! I've read the reply you put, but here's something different.

I'm using macOS Catalina 10.15.6, and I just upgrade Xcode to the latest one which is v12.
when I execute

xcrun -f sourcekit-lsp

I get the path of my sourcekit-lsp which is the default path in Xcode toolchanin

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp

then I configured sourcekit-lsp path in a vscode plugin named "Maintained Swift Development Environment", but vscode editor still can't find PackageDescription. There seems no problem with other packages.

no such module 'packagedescription' sourcekitd

I guess the problem lies in libSwiftpm and sourcekit-lsp version mismatch? Is there a way I can find out libSwiftpm version and change it?

I also downloaded an older toolchain from swift.org, it is the swift 5.2.5 one. However I can only change toolchain in Xcode, it doesn't affect xcrun and vscode.

So, any advice on it?

To clarify: it's only in the Package.swift file itself you're seeing problem, but things work in other files? Or is it the whole package?

I guess the problem lies in libSwiftpm and sourcekit-lsp version mismatch? Is there a way I can find out libSwiftpm version and change it?

Did you build your package using the same toolchain as you ran sourcekit-lsp from? If so, they should match.

I also downloaded an older toolchain from swift.org, it is the swift 5.2.5 one. However I can only change toolchain in Xcode, it doesn't affect xcrun and vscode.

On the command-line, you can use the TOOLCHAINS environment variable or --toolchain argument to xcrun to specify a specific toolchain. I believe "TOOLCHAINS=swift" should choose the most recent open source toolchain, or you can specify a specific toolchain bundle identifier. E.g.

$ plutil -p /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-09-16-a.xctoolchain/Info.plist | grep BundleIdentifier
  "CFBundleIdentifier" => "org.swift.50202009161a"

$ export TOOLCHAINS="org.swift.50202009161a"

For sourcekit-lsp, you can run the sourcekit-lsp from whatever toolchain you are going to use

$ export TOOLCHAINS="org.swift.50202009161a"
$ xcrun -f sourcekit-lsp
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-09-16-a.xctoolchain/usr/bin/sourcekit-lsp