Skip to content
Advertisement

Swift – Can’t import packages

I have been trying out Swift on Ubuntu 20.10 and am having trouble importing packages. Swift is installed properly. I am always getting error: no such module, no matter which package I try.
Here is my Package.swift (Embassy as an example):

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
        name: "test",
        /* products: [
             .executable(name: "test", targets: ["test"])
         ],*/
        dependencies: [
            // Dependencies declare other packages that this package depends on.
            // .package(url: /* package url */, from: "1.0.0"),
            .package(url: "https://github.com/envoy/Embassy.git",
                    from: "4.1.1"),
        ],
        targets: [
            // Targets are the basic building blocks of a package. A target can define a module or a test suite.
            // Targets can depend on other targets in this package, and on products in packages this package depends on.
            .target(
                    name: "test",
                    dependencies: []),
            .testTarget(
                    name: "testTests",
                    dependencies: ["test"]),
        ]
)


main.swift(under Sources/test)

import Foundation
import Embassy
let loop = try! SelectorEventLoop(selector: try! KqueueSelector())
let server = DefaultHTTPServer(eventLoop: loop, port: 8080) {
    (
            environ: [String: Any],
            startResponse: ((String, [(String, String)]) -> Void),
            sendBody: ((Data) -> Void)
    ) in
    // Start HTTP response
    startResponse("200 OK", [])
    let pathInfo = environ["PATH_INFO"]! as! String
    sendBody(Data("the path you're visiting is (pathInfo.debugDescription)".utf8))
    // send EOF
    sendBody(Data())
}

// Start HTTP server to listen on the port
try! server.start()

// Run event loop
loop.runForever()

Advertisement

Answer

In your Package.swift file you are declaring Embassy as a dependency, but you are not referencing that dependency in any of your targets. In the example you provided, you can alter your package like this:

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "test",
    dependencies: [
        .package(url: "https://github.com/envoy/Embassy.git", from: "4.1.1"),
    ],
    targets: [
        // Reference the 'Embassy' package here.
        .target(name: "test", dependencies: ["Embassy"]),
        .testTarget(name: "testTests", dependencies: ["test", "Embassy"]),
    ]
)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement