Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import packages using Swift 4 Package Manager

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?

like image 863
Timka Avatar asked Jul 10 '17 23:07

Timka


2 Answers

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.

like image 157
Timka Avatar answered Sep 26 '22 23:09

Timka


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

like image 23
Krishnadas PC Avatar answered Sep 23 '22 23:09

Krishnadas PC