3

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?

6
  • Use Xcode to create packages. Commented May 20, 2022 at 17:21
  • You mean package file? And then open it with VS code for the next use? Commented May 20, 2022 at 17:23
  • What does the surrounding context look like? Package.swift only does anything within a Swift package, which you make with swift package init <details> Commented May 20, 2022 at 17:30
  • Although the library you link to has XCode 8 I'm assuming you're just using VSCode as an editor. In which case you may find it useful to install the Swift VS Code extension which has a command for resolving all dependencies in the file you have open. That should include installing the PackageDescription Swift package. You can find the VS Code extension in the extensions tab or via the web here: marketplace.visualstudio.com/items?itemName=sswg.swift-lang Commented May 21, 2022 at 6:32
  • @RustyFluff I use this extension but when I save my Package.swift file terminal shows 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 ``` So I cant add my package to Dependencies Commented May 21, 2022 at 10:11

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.