/src: error: could not find source files for target(s): MyKituraAppTests; use the 'path' property in the Swift 4 manifest to set a custom target path - swift4

I'm trying to compile using swift build
Package.swift
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swift-tools-version:x.x
import PackageDescription
let package = Package(
name: "MyKituraApp",
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura", from: "2.7.0")
],
targets: [
.target(
name: "MyKituraApp",
dependencies: ["Kitura"],
path: "Sources"),
.testTarget(
name: "MyKituraAppTests",
dependencies: ["MyKituraApp"],
path: "Test")
]
)
But, I get the following error although I did add the path property.
'MyKituraApp' /src: error: could not find source files for target(s): MyKituraAppTests; use the 'path' property in the Swift 4 manifest to set a custom target path

Without knowing your project structure I can't give you a definite answer but I'll do my best!
I'm going to assume you've generated your project using the Swift Package Manager tool, something like this:
swift package init --type executable
So... Typically you shouldn't need to set the path property unless you've moved the tests for your application to another directory. The Swift Package Manager, by default, will create a Tests directory and when you do not provide a value for the path property the Swift Package Manager will look for that Tests directory by default when you run swift build. In your path property you are providing a value of Test not Tests
So my first solution to test would be:
To remove the path property from the .testTarget section
OR
Rename the path properties value to Tests rather than Test.
I've provided an example Package.swift that I was able to run swift build with:
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyKituraApp",
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura", from: "2.7.0")
],
targets: [
.target(
name: "MyKituraApp",
dependencies: ["Kitura"],
path: "Sources"),
.testTarget(
name: "MyKituraAppTests",
dependencies: ["MyKituraApp"])
]
)
As you can see I've also removed an extra line from the top of the file:
// swift-tools-version:x.x
You've already provided a swift-tools-version at the top of your file, this line may end up confusing things later down the line.
I hope this helps!

Related

How to create an SPM package based on ObjC code?

I'd like to create a SwiftPackageManager library based on Objective-C code but I can't seem to grasp what I'm missing.
My latest change to just vanilla ObjC interface .h file inside the include folder was to add an extra C header that includes de ObjC but still had no success. What am I missing?
The Package.swift file is the default generated one and from what I read it should automatically generate the module map from the include folder.
My swift-tools-version is 5.5
Figured it out.
I added a modulemap that specifies my ObjC header and it worked
Not sure if it is the correct way to do it since the include folder should already do this automatically.
So just had the same issue. (my 5 cent)
This is may Package.swift:
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "AlgorithmSDK",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages. , "AlgorithmSDKObjc"
.library(
name: "AlgorithmSDK",
targets: ["AlgorithmSDK","AlgorithmSDKObjc" ]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.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 this package depends on.
.target(
name: "AlgorithmSDK",
dependencies: []),
.testTarget(
name: "AlgorithmSDKTests",
dependencies: ["AlgorithmSDK"]),
.target(
name: "AlgorithmSDKObjc",
dependencies: [],
publicHeadersPath:"include"),//<----- This path is relative to the target! (and can be ignored)
]
)
With the structure:
The publicHeadersPath should be publicHeadersPath:"include" OR ignored according to docs (it seems that it takes it relative to target and not the root). I don't think we should touch module.modulemap for such a simple structure

Sharing code across test targets when using the Swift Package Manager

I have some code that I need to share across test targets, while I'm using the Swift Package Manager. To do this, I have a .testTarget that I also name as a dependency in another .testTarget.
Here is a simple 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: "ExampleLib",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "ExampleLib",
targets: ["ExampleLib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.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 this package depends on.
.target(
name: "ExampleLib",
dependencies: []),
.testTarget(
name: "Common",
dependencies: ["ExampleLib"]),
.testTarget(
name: "ExampleLibTests",
dependencies: ["Common"]),
]
)
If I try to build this package in Xcode, I get the following error:
Unable to resolve build file: XCBCore.BuildFile (The workspace has a reference to a missing target with GUID 'PACKAGE-TARGET:Common')
However, if I build from the command line (swift build) or test from the command line (swift test), I get success.
I'm using Xcode 12 beta 6, but have also tried Xcode 11.5 (with a change to the Package.swift header) and get the same results.
Here is the complete Swift package example:
https://www.dropbox.com/s/h6ypvbfonnb2zyk/ExampleLib.zip?dl=0
I really would like to use this in Xcode to build for iOS. Thoughts?
I faced the same issue and solved it by defining a Target (not Test Target) instead for Common.
My Package.swift file:
// ...
.target(name: "Common", dependencies: ["App"], path: "Tests/Common"),
.testTarget(name: "AppTests", dependencies: ["App", "Common"]),
.testTarget(name: "IntegrationTests", dependencies: ["App", "Common"])
// ...

Problem with Swift Package Manager - module map should be inside "Include" directory

This is my Package.swift file:
let package = Package(
name: "MyProject",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/AlwaysRightInstitute/cows.git",
from: "1.0.0"),
.package(url: "https://github.com/NozeIO/MicroExpress.git",
.branch("branches/swift-nio-lib"))
],
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: "Demo",
dependencies: [
"cows", "MicroExpress"
],
path: ".")
]
)
I've already initialised the Package correctly (I think) and the git repos are fetched (using "Swift package fetch/resolve")
Now i'm trying to "Swift build" but I get the follosing error:
'MyTarget' /Users/nimrodshai/Documents/MyProject: error: package has unsupported layout; modulemap '/Users/nimrodshai/Documents/MyProject/Pods/FirebaseCrash/Frameworks/FirebaseCrash.framework/Modules/module.modulemap' should be inside the 'include' directory
Anyone has any idea how do I solve this?
Edit:
I've trying uninstalling everything from the Pod file (including firebase) and then build. This is what i'm getting:
'Toky' /Users/nimrodshai/Documents/TOKY-TOY: error: target at '/Users/nimrodshai/Documents/TOKY-TOY' contains mixed language source files; feature not supported
Try using the documentation found here for publicHeadersPath for targets. It says:
publicHeadersPath: This property defines the path to the directory containing public headers of a C target. This path is relative to the target path and default value of this property is include. Only valid for C family library targets.
You should be able to set that to the path to your module.modulemap and it will then be happy.

Unable to create a Swift Package Manager library

The documentation about SPM is quite poor and most of the articles about it were published when SPM began.
I have implemented an algorithm (Hungarian algorithm) in Swift and was looking forward to publish it as a library in Github. I have had to use the SPM for the first time in this project to resolve another dependency and once it has started to work, it was perfect.
Now, I am not being able to use my library from another project. I have decided to start with a new fresh git repo cause I couldn't get the previous to work.
The library is named Hume and it has inside a Hume.swift file which defines a Hume class.
The steps I have gone through are these:
Created directory for the library
swift package init --type library
Populated the files with the code
Pushed to a GitHub repo + tagged the version
Created directory for the executable which is going to use the library
swift package init --type executable
Add the dependency in Package.swift
swift build
At this point, swift clones the repo of the library and compiles it without problems (as main.swift contains just a Hello world).
swift package generate-xcodeproj
An when I open the project and try to import my library, it seems the module name is recognized but when I try to declare an object, it says I can't declare a variable of a type that is a module.
This is the Package.swift file in the library:
// 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: "Hume",
products: [
.library(
name: "Hume",
targets: ["Hume"]),
],
dependencies: [
.package(url: "https://github.com/aleph7/Upsurge.git", from: "0.10.2"),
],
targets: [
.target(
name: "Hume",
dependencies: ["Upsurge"]),
.testTarget(
name: "HumeTests",
dependencies: ["Hume"]),
]
)
The Library has only one file with this style:
import Upsurge
class Hume {
// Attributes
....
init(matriz:[[Double]]){
....
}
public func resuelve() -> (Double, [(Int, Int)]){
....
}
}
This is the Package.swift in the dummy executable:
// 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: "aa",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/Jasagredo/Hume.git", from: "0.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 which this package depends on.
.target(
name: "aa",
dependencies: ["Hume"]),
]
)
The output when building:
~/Desktop/aa  swift build
Fetching https://github.com/Jasagredo/Hume.git
Fetching https://github.com/aleph7/Upsurge.git
Cloning https://github.com/Jasagredo/Hume.git
Resolving https://github.com/Jasagredo/Hume.git at 0.1.1
Cloning https://github.com/aleph7/Upsurge.git
Resolving https://github.com/aleph7/Upsurge.git at 0.10.2
Compile Swift Module 'Upsurge' (30 sources)
Compile Swift Module 'Hume' (1 sources)
Compile Swift Module 'aa' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/aa
~/Desktop/aa 
But when I edit main.swift in the dummy executable, I find this error:
import Hume
var a = Hume(matriz: [[1,1],[1,1]]) //Cannot call value of non-function type 'module<Hume>'
Also, Xcode doesn't auto-suggest me the class Hume. I just don't know what I'm doing wrong.
Any help is appreciated.
I have finally managed to get things to work. The problem was the class (and it's init method) weren't declared as public. The rest of the configuration is correct.

error: could not find target(s): MyLib; use the 'path' property in the Swift 4 manifest to set a custom target path

I am running sudo swift test and got following error:
error: could not find target(s): MyLib; use the 'path' property in the Swift 4 manifest to set a custom target path
Package.swift:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "MyLib",
targets: [
.target(name: "MyLib"),
.testTarget(
name: "MyLibTests",
dependencies: ["MyLib"])
]
)
According to the proposal there is an impact on exiting code. The purposal says:
These enhancements will be added to the version 4 manifest API, which will release with Swift 4. There will be no impact on packages using the version 3 manifest API. When packages update their minimum tools version to 4.0, they will need to update the manifest according to the changes in this proposal.
Since your minimum tools version is 4.0, you must add path: "path/to/sources" in .Target().
Your Package.swift should look like this:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "MyLib",
targets: [
.target(
name: "MyLib",
path: "Sources"), //path for target to look for sources
.testTarget(
name: "MyLibTests",
dependencies: ["MyLib"],
path: "Tests")
]
)