Swift Package Manager (SPM) supports package traits (SE-0450) starting in Swift 6.2. Package traits let packages conditionally compile parts of a library, letting you include only the features and dependencies your application needs.
Why client libraries use traits
Several Google Cloud APIs define large numbers of independent services. For example, Compute Engine v1 defines over 120 services, and Vertex AI defines over 30 services. Generating and compiling every client, request, response, and model type causes long compile times and larger binaries. This overhead occurs even when an application uses only one or two services.
To optimize build performance, the Google Cloud client libraries for Swift use package traits to partition large libraries. Enabling a trait conditionally compiles the specific service client and all the types required to use that client. The client libraries enable package traits for large APIs to manage build overhead, though the exact threshold is subject to change.
Default traits
Packages that use traits define a set of default traits for commonly used
services. For example, Compute Engine enables only Instances (and
transitively ZoneOperations) by default.
When you add a dependency without specifying any traits, SPM enables the default traits automatically:
.package(
url: "https://github.com/googleapis/swift-google-cloud-compute-v1.git",
from: "0.4.0"
)
If the default traits provide all the services that your application needs, you don't need further configuration.
Enable additional traits
To enable services beyond the defaults, specify the required traits in your
Package.swift file.
Combine default traits with additional traits
To keep the default services and enable additional services, include the
".defaults" trait alongside the traits that you want to enable:
// swift-tools-version: 6.2
import PackageDescription
let package = Package(
name: "MyApplication",
dependencies: [
.package(
url: "https://github.com/googleapis/swift-google-cloud-compute-v1.git",
from: "0.4.0",
traits: [".defaults", "Disks"]
),
],
targets: [
.target(
name: "MyApplication",
dependencies: [
.product(
name: "GoogleCloudComputeV1",
package: "swift-google-cloud-compute-v1"
),
]
),
]
)
Disable defaults and select specific traits
To disable the default traits and compile only specific services, list the
selected traits without ".defaults":
.package(
url: "https://github.com/googleapis/swift-google-cloud-compute-v1.git",
from: "0.4.0",
traits: ["Disks"]
)
Compiler diagnostics for disabled traits
If your code attempts to use a client whose trait isn't enabled in
Package.swift, the Swift compiler produces an error message:
error: 'DisksClient' is unavailable: Enable the 'Disks' trait in Package.swift to use this client.
This diagnostic indicates which trait to add to your Package.swift
dependencies.
Command-line workflows
When developing, testing, or building from the command line, you can enable
traits using the --traits option with swift build or swift test:
swift build --traits defaults,Disks
To build with only specific traits and bypass the defaults, omit defaults:
swift build --traits Disks
Discover available traits
Each generated client library documents its available traits, default traits, and the corresponding client classes in two places:
- The
README.mdfile in each package under the "Package Traits" section - The DocC documentation for each package in the "Package Traits" article
What's next
- Follow the Quickstart: Get started with Swift to set up and run your first Swift application.
- Learn how to enable debug logging to troubleshoot requests and responses.
- Browse the Google Cloud client libraries for Swift reference documentation.