无法使用Swift 4软件包管理器导入软件包

34

我正在尝试在我的电脑上使用Xcode-beta (v9)测试Swift 4,但是在导入软件包到测试项目中遇到了问题:

  • 使用swift package init --type executable初始化项目
  • 更改了Package.swift并添加了2个要尝试的项目:

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: []),
    ]
)
  • 运行swift build && swift package generate-xcodeproj
  • 当我在Xcode-beta(v9)中打开项目并尝试导入Kitura或Alamofire时,我收到No such module Kitura/Alamofire错误信息
  • 在终端中运行swift build会产生以下错误:

编译 Swift 模块 '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 虚拟文件夹包含相同包名称的目录,但它们是空的。然而,.build\checkouts.build\repositories包含包文件夹和相应的文件。

我系统配置是否有遗漏?

2个回答

67

原来我还必须将依赖项包含在Package.swift的.target中:

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

然后再次构建该项目。


3
哇,这在文档中并不明显https://swift.org/blog/swift-package-manager-manifest-api-redesign/。看起来需要前往特定包的git repo URL来将其映射到缺失的模块,因为导入名称并不一定与包名称相同。 - Chris Prince

1
即使您需要将target添加到Package.swift中,有时仍然不够。我通过删除.build目录和文件Package.resolved来解决了这个问题,然后运行swift build或从Xcode构建。构建命令不会获取软件包,因为它已经在resolved文件中,但是如果您删除.build目录,它就变得无意义了。您可以通过检查来自XcodeDependencies目录来验证它,如果出现No such package/module等错误,则该目录将为空。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接