Can't import packages using Swift 4 Package Manager

杀马特。学长 韩版系。学妹 提交于 2019-12-03 06:40:28

问题


Trying to test Swift 4 using Xcode-beta (v9) on my machine and having issues with importing packages into a test project:

  • Initiated project using swift package init --type executable
  • Changed Package.swift and added 2 projects to try out:

Package.swift

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

import PackageDescription

let package = Package(
    name: "sampleproject",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/IBM-Swift/Kitura.git", from: "1.7.6"),
        .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.5.0")
    ],
    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 which this package depends on.
        .target(
            name: "sampleproject",
            dependencies: []),
    ]
)
  • Run swift build && swift package generate-xcodeproj
  • When I open project in Xcode-beta(v9) and try to import Kitura or Alamofire, I'm getting No such module Kitura/Alamofire error message
  • Running swift build in terminal produces the following error:

Compile Swift Module 'investprosto' (1 sources) /Users/username/Projects/sampleproject/Sources/sampleproject/main.swift:1:8: error: no such module 'Kitura' import Kitura ^ error: terminated(1): /Applications/Xcode- beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift- build-tool -f /Users/username/Projects/sampleproject/.build/debug.yaml

Dependencies virtual folder contains the directories with the same package names, but they are empty. However, .build\checkouts and .build\repositories contain packages folders and corresponding files.

Is there something I'm missing in my system's configuration?


回答1:


Turns out I had to also include the dependencies into the .target of the Package.swift:

.target(named: "sampleproject", dependencies: ["Kitura", "Alamofire"])

and build the project again.




回答2:


Even though you need to add the target into the Package.swift sometimes it's not enough. I have fixed the issue by deleting the .build dir and the file Package.resolved, then running swift build or build from Xcode. Build command was not fetching packages as it was already in resolved file but if you delete the .build dir it becomes meaningless. You can verify it by checking the Dependencies dir from Xcode it will be empty if there is error like No such package/module



来源:https://stackoverflow.com/questions/45023201/cant-import-packages-using-swift-4-package-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!