How to use Swift Packages in Visual Studio Code?

1.3k Views Asked by At

I'm new with Swift and all this tools. I've tried to look for guides but they didn't help me. I wanna try to work with packages in VS code.So I decided to add a SwiftyJSON package. This is my Package.swift file:

import PackageDescription

let package = Package(
    name: "SwiftyJSON",
    dependencies: [
        .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),
    ]
)

And I got an error error: no such module 'PackageDescription' import PackageDescription For VS Code I use an extension Swift and package dependencies. It automatically detects a package but when I save my Package.swift file terminal gives me an error:

> Executing task: swift package resolve <

'swiftpackages': error: the manifest is missing a Swift tools version specification; 

consider prepending to the manifest '// swift-tools-version: 5.6' to 
specify the current Swift toolchain version as the lowest Swift version
supported by the project; 

if such a specification already exists, consider moving it to the top
of the manifest, or prepending it with '//' to help Swift Package
Manager find it

The terminal process "/opt/homebrew/bin/zsh '-c', 'swift package
resolve'" failed to launch (exit code: 1).

How I can add packages in VS code?

1

There are 1 best solutions below

0
On

I know this post is old, but I ran into it looking for something else and you were so close! What was missing on your Package.swift file was the declaration of what version of swift tools that Package.swift was written to conform to. That line is needed at the top of every Package.swift file.

So closer to right, but still missing things would be:

// swift-tools-version: 5.9


import PackageDescription

let package = Package(
    name: "MyFancyNewPackage",
    dependencies: [
        .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),
    ]
)

The quickest way to start a new package in VSCode with a correct Package.swift is to:

  • create an empty folder with the name of the package you're starting
  • open that empty folder in VSCode
  • Control + ` to get the integrated terminal open
  • swift package init --type library will start a new library package - If not interested in a library there are others, swift package init --help will show the available templates.

Once you have YOUR new package you can do what you've already started to do, add dependencies on others like SwiftJSON.

Now this all depends on you having the right tools installed. Since you're using homebrew you're likely on a Mac. Installing Xcode will get you everything you need. You never have to use it if you don't want. That's the easiest. The homebrew version of swift is frequently behind.

I use VSCode to write Swift both on a Mac and Linux so if that's the IDE you want to use it's very doable.

++1 to using the Swift VSCode extesion by the server working group: https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang

Anyway I hope you stuck with it and figured it out. Happy 2024!