How to add executable in Xcode that can be run by the Swift Package Manager? - swift

I used SPM to initialize the package and then generated xcodeproj for editing in Xcode. I added an executable in Xcode but SPM isn't picking it up when building. If I run generate-xcodeproj again using SPM, the executable product is gone. I looked into the .build folder and the executable isn't there. Right now this is my 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: "Swerver",
products: [
.library(
name: "Swerver",
targets: ["Swerver"]),
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/BlueSocket.git", from: "1.0.8"),
],
targets: [
.target(
name: "Swerver",
dependencies: [
"Socket"
],
path: "Sources/SwerverCore"),
.testTarget(
name: "SwerverTests",
dependencies: ["Swerver"]),
]
)

You can't add an executable in Xcode and have it picked up by the Swift Package Manager. You need to add it in the Package.swift file and regenerate the Xcode project.
Alternatively, add the executable to a new project that references the generated project.

Related

How to compile SwiftPackage executable product?

I have a Swift library that exports a CLI executable product. I want to ship the CLI binary as a Git release asset to ease the installation, but I am struggling to build the package.
let package = Package(
name: "swift-graphql",
platforms: [...],
products: [
// SwiftGraphQL
.library(name: "SwiftGraphQL", targets: ["SwiftGraphQL"]),
.library(name: "SwiftGraphQLClient", targets: ["SwiftGraphQLClient"]),
.library(name: "SwiftGraphQLCodegen", targets: ["SwiftGraphQLCodegen"]),
// CLI
.executable( name: "swift-graphql", targets: ["SwiftGraphQLCLI"]),
],
dependencies: [...],
targets: [...]
)
How can I build a binary executable file for swift-graphql using swift?
I have already tried swift build -c release, but I can't find the binary file that may be executed by the user, and swift build -c release --show-bin-path, but the returned path points to the folder containing all packages, not just SwiftGraphQLCLI executable.
I figured you could list all products in the package using swift package describe.
Once you find the product name of the package you want to build, you can build it using the following script
swift build -c release --product <product> --disable-sandbox

Product dependency not found — only before Swift 4.2

I'm maintaining two Swift Package Manager projects on GitHub. One of them, DiceKit, depends on the other, ProtocolKit. They are also both using a swift-tools-version of 4.0.
DiceKit has continuous integration set up with Travis CI, shown here. It is tested on both macOS and Linux, on Swift 4.0, 4.0.2, 4.0.3, 4.1, 4.1.1, 4.1.2, 4.1.3, and 4.2. However, the tests on every version except 4.2 are failing, while the tests on 4.2 succeed.
Here's the error from the macOS Swift 4.0 log:
$ swift test
error: dependency graph is unresolvable; found these conflicting requirements:
Dependencies:
https://github.com/Samasaur1/ProtocolKit.git # 1.0.0..<2.0.0
error: product dependency 'ProtocolKit' not found
Fetching https://github.com/Samasaur1/ProtocolKit.git
However, on the Swift 4.2 log, fetching the dependencies is successful:
$ swift test
Fetching https://github.com/Samasaur1/ProtocolKit.git
Completed resolution in 1.44s
Cloning https://github.com/Samasaur1/ProtocolKit.git
Resolving https://github.com/Samasaur1/ProtocolKit.git at 1.0.2
Compile Swift Module 'ProtocolKit' (1 sources)
Compile Swift Module 'DiceKit' (3 sources)
Compile Swift Module 'DiceKitTests' (4 sources)
For reference, here is the Package.swift file for DiceKit:
// swift-tools-version:4.0
// Managed by ice
import PackageDescription
let package = Package(
name: "DiceKit",
products: [
.library(name: "DiceKit", targets: ["DiceKit"]),
],
dependencies: [
.package(url: "https://github.com/Samasaur1/ProtocolKit.git", from: "1.0.0"),
],
targets: [
.target(name: "DiceKit", dependencies: ["ProtocolKit"]),
.testTarget(name: "DiceKitTests", dependencies: ["DiceKit"]),
]
)
And here is the one for ProtocolKit:
// swift-tools-version:4.0
// Managed by ice
import PackageDescription
let package = Package(
name: "ProtocolKit",
products: [
.library(name: "ProtocolKit", targets: ["ProtocolKit"]),
],
targets: [
.target(name: "ProtocolKit", dependencies: []),
.testTarget(name: "ProtocolKitTests", dependencies: ["ProtocolKit"]),
]
)
I'm at my wit's end about this, and I'd really appreciate any help you all can give. Thanks in advance!

Running 'swift build' crashes with segmentation fault when dependency name equals project's name

Example:
> mkdir myProject && cd myProject
> swift package init --type executable
Edit Package.swift (added 1 dependency):
// 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: "myProject",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/someone/myProject.git", 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 which this package depends on.
.target(
name: "myProject",
dependencies: []),
]
)
Build it:
> swift build
Segmentation fault (core dumped)
Environment
Ubuntu 16.04 LTS
Swift version 4.1-dev (LLVM 260a172ffb, Clang cd84be6c42, Swift 05b1b2be7c)
Is anyone aware if this was recently introduced and/or is happening with other versions of swift?
It is a bug:
Swift Bug SR-7597
https://bugs.swift.org/browse/SR-7597

Cannot Deploy To Vapor Cloud with Swift 4

I try to deploy hello world app for testing purpose. I check my swift tools version and it's 4.0
acme-iMac:HelloWorldWeb johndoe$ swift --version
Apple Swift version 4.0 (swift-4.0-RELEASE)
Target: x86_64-apple-macosx10.9
I also use swiftenv and it's point to version 4.0 as well.
acme-iMac:HelloWorldWeb johndoe$ swiftenv version
4.0 (set by /Users/johndoe/Vapor/HelloWorldWeb/.swift-version)
The Package.swift file also seems fine
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "HelloWorldWeb",
products: [
.library(name: "App", targets: ["App"]),
.executable(name: "Run", targets: ["Run"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.2.0")),
.package(url: "https://github.com/vapor/leaf-provider.git", .upToNextMajor(from: "1.1.0")),
],
targets: [
.target(name: "App", dependencies: ["Vapor", "LeafProvider"],
exclude: [
"Config",
"Database",
"Public",
"Resources"
]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App", "Testing"])
]
)
But every time I try to vapor cloud deploy, it always failed.
Creating deployment [Done]
Connecting to build logs ...
Waiting in Queue [Done]
Checkout branch 'master' [Done]
Verifying base folder [Done]
Selected swift version: 3.1.0 [Done]
Selected swift version: 3.1.0 [Done]
Building vapor [Failed]
swift-build: error: Package requires minimum Swift tools version 4.0.0. Current Swift tools version is 3.1.0
Error: deploy failed.
The app run successfully on local machine.
Vapor Cloud needs to be told to use Swift 4; it defaults to 3.1.
Add a file named cloud.yml to the root directory of your project, containing:
type: "vapor"
swift_version: "4.0.0"
Vapor 1 server - I found that the only line needed was:
swift_version: "4.0.0"
this caused errors - worked without it:
type:"vapor"

Vapor project not building/running with Swift 4 and Xcode 9

I have the most recent version of Xcode 9 installed and have reinstalled vapor with brew a couple times now, but I'm still getting the following error whenever I try to build/run/update the project.
Here's what I'm working with
1.) Vapor Toolbox: 3.1.2
2.) Apple Swift version 4.0 (swiftlang-900.0.43 clang-900.0.22.8)
Target: x86_64-apple-macosx10.9
3.) Xcode 9.0
Build version 9M136h
Error: Could not generate Xcode project: error: manifest parse error(s):
/var/folder/60/n3ldjzgs5vsg06v17_1yy44h0000gn/T/TemporaryFile.VwrbJo.swift:41:5
: error: argument 'targets' must preceed argument 'dependencies'
targets" [
^
error: The product dependency 'Vapor' was not found.
error: The product dependency 'FluentProvider' was not found.
error: The product dependency 'Testing' was not found.
Here is my Package.swift file. Looking at my original error, I did try switching the 'targets' and 'dependencies' order in which they appear in the file. This didn't solve anything and produced the following error message: error: argument 'dependencies' must precede argument 'targets'
Package.swift
let package = Package(
name: "test",
products: [
.library(name: "App", targets: ["App"]),
.executable(name: "Run", targets: ["Run"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.1.0")),
.package(url: "https://github.com/vapor/fluent-provider.git", .upToNextMajor(from: "1.2.0")),
],
targets: [
.target(
name: "App",
dependencies: ["Vapor", "FluentProvider"],
exclude: [
"Config",
"Public",
"Resources",
]
),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App", "Testing"])
]
)
Xcode 12 / Swift 5.3 Update
vapor is now part of homebrew/core. Source: github homebrew-core/Formula/vapor.rb.
##### If applicable, remove the obsolete 'vapor/tap' #####
## List current taps:
brew tap
# homebrew/core
# vapor/tap
## Remove tap:
brew untap vapor/tap
#### Install current version of vapor #####
brew install vapor
brew upgrade vapor
Xcode 9 / Swift 4
Double Check Versions: "the most recent releases" would be at least Xcode 9.1 (9B55) and Apple Swift 4.0.2 (swiftlang-900.0.69.1 clang-900.0.38)
Verify that the first line in Packages.swift is // swift-tools-version:4.0. Without swift-tools-version line, Packages.swift could be processed as swift-tools-version 3. which will cause an error.*
cd _PROJECT_PATH_
swift package tools-version
# if not 4.0.0, then …
swift package tools-version --set-current
An example, Package.swift is shown below. See Swift Package Manager Manifest API Redesign and swift-package-manager Usage Documentation for additional information.
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift
// required to build this package.
// Syntax: '// swift-tools-version:<specifier>' on the 1st line
import PackageDescription
let package = Package(
name: "test",
products: [
.library(name: "App", targets: ["App"]),
.executable(name: "Run", targets: ["Run"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.3.0")),
.package(url: "https://github.com/vapor/fluent-provider.git", .upToNextMajor(from: "1.3.0")),
],
targets: [
.target(
name: "App",
dependencies: ["Vapor", "FluentProvider"],
exclude: [
"Config",
"Public",
"Resources",
]
),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App", "Testing"])
]
)
Recheck
cd _PROJECT_PATH_
swift package tools-version
# should be 4.0.0
vapor clean
vapor update
vapor --version
# Vapor Toolbox: 3.1.2
# Vapor Framework: 2.3.0
Try to build again.
More notes are posted on StackOverflow here for using Xcode 9 and Swift 4 with Vapor 2.
The GitHub repository Vapor Examples Lab has several example projects which have been migrated to Vapor 2 and Swift 4.