One Package, Three Imports: Understanding Swift Modules

Learn how Swift packages, products, targets, modules, imports, and linking fit together using PhotoEvidenceKit as a concrete example.

One Package, Three Imports: Understanding Swift Modules

PhotoEvidenceKit is one package containing three library modules. That is why RawCull can contain these three imports:

import PhotoEvidenceContracts
import PhotoEvidenceEngine
import PhotoEvidenceUI

Each import names a module. PhotoEvidenceKit is the package name, but this package does not define a module with that name. Consequently, adding the package to Xcode does not make import PhotoEvidenceKit valid.

This guide uses the actual PhotoEvidenceKit manifest at the RawCull-resolved 1.0.7 revision. Alternative designs later in this guide are teaching examples, not changes made to that package.

1. The Names Describe Different Things

TermMeaning in this exampleWhere it appears
RepositoryThe Git repository containing the packageGitHub URL and checkout directory
PackageThe overall SwiftPM definition, named PhotoEvidenceKitPackage(name: ...) in Package.swift
ProductA library or executable offered to consumersThe manifest’s products array and Xcode’s product selection
TargetA build unit with source files, dependencies and settingsThe manifest’s targets array
ModuleThe namespace and access-control boundary compiled from a regular Swift library targetimport PhotoEvidenceContracts, for example
Source fileOne contribution to a target’s moduleSources/PhotoEvidenceContracts/CloseUpContracts.swift

In this package, each library product and its target have the same name. That makes integration convenient, but it can hide the difference between selecting a product and importing a module. SwiftPM’s Product documentation explains that a product is assembled from build artifacts of one or more targets. A product name does not itself create a new module.

Think of the package as the complete distribution, products as the choices offered to its consumer, and targets as the units that actually build the code. They do not have to share a name.

2. Read The Real Manifest From The Outside In

The beginning of the manifest declares the tools version, imports the manifest API, and creates the package description:

// swift-tools-version: 6.2
import PackageDescription

let package = Package(
    name: "PhotoEvidenceKit",
    // products, targets, platform and language settings follow
)

This is a shortened excerpt. import PackageDescription is used while evaluating the manifest. It does not become an application import or add PackageDescription to the photo-inspection runtime.

The package name identifies the whole package. It does not instruct SwiftPM to collect all source files into a PhotoEvidenceKit module.

Products: What Consumers Can Select

The actual library product declarations are:

.library(name: "PhotoEvidenceContracts", targets: ["PhotoEvidenceContracts"]),
.library(name: "PhotoEvidenceEngine", targets: ["PhotoEvidenceEngine"]),
.library(name: "PhotoEvidenceUI", targets: ["PhotoEvidenceUI"]),

Each exposes one same-named target as a library product. There is also an executable product, PhotoEvidenceDemo, for running the synthetic example. The demo is not a fourth library that a host should import to display close-ups.

Targets: What Gets Compiled And What It May Depend On

The three library targets are declared as:

.target(name: "PhotoEvidenceContracts"),
.target(name: "PhotoEvidenceEngine", dependencies: ["PhotoEvidenceContracts"]),
.target(name: "PhotoEvidenceUI", dependencies: ["PhotoEvidenceContracts", "PhotoEvidenceEngine"]),

The first target has no dependencies on another target in this package. Engine can use Contracts, while UI can use both Contracts and Engine. There are no external package dependencies in this manifest. Apple frameworks such as Foundation, CoreGraphics and SwiftUI are SDK modules used by source files, rather than downloaded Swift package dependencies.

These declarations establish the build graph. They are not an instruction to insert import statements into every source file.

3. How Folders Become Importable Modules

For these regular targets, SwiftPM uses the conventional source directories:

PhotoEvidenceKit/
├── Package.swift
├── Sources/
│   ├── PhotoEvidenceContracts/
│   │   ├── CloseUpContracts.swift
│   │   ├── CloseUpRenderSource.swift
│   │   └── CloseUpAFPoint.swift
│   ├── PhotoEvidenceEngine/
│   │   ├── CloseUpCropGeometry.swift
│   │   ├── CloseUpRasterizer.swift
│   │   └── CloseUpRendering.swift
│   └── PhotoEvidenceUI/
│       ├── CloseUpModel.swift
│       ├── CloseUpCardModel.swift
│       └── CloseUpPanel.swift
├── Examples/PhotoEvidenceDemo/
└── Tests/PhotoEvidenceKitTests/

The example executable has an explicit path in the manifest because it lives outside the default Sources/PhotoEvidenceDemo location.

All three files under Sources/PhotoEvidenceContracts contribute declarations to the same PhotoEvidenceContracts module. There is no import CloseUpContracts for the individual file. Source-file names organize the repository; the target determines the module boundary.

Similarly, a nested folder added inside a target does not automatically become another module. A separate target declaration and appropriate source layout are needed to create another regular Swift module. See SwiftPM’s Target documentation for the target’s role in compilation.

4. Follow The Build In Dependency Order

flowchart BT
    Contracts["PhotoEvidenceContracts\nframes, regions, requests, source identity"]
    Engine["PhotoEvidenceEngine\ngeometry, rasterizer, render protocol"]
    UI["PhotoEvidenceUI\nmodel, panel, card loading"]
    App["RawCull\nhost adapters and application policy"]
    Engine --> Contracts
    UI --> Contracts
    UI --> Engine
    App --> Contracts
    App --> Engine
    App --> UI

An arrow means “depends on.” Contracts can be compiled before Engine; UI needs the lower modules’ interfaces. The build system schedules work according to this graph and can reuse outputs when they remain valid.

Conceptually, the build proceeds through these stages:

  1. Resolve the package revision and evaluate its manifest.
  2. Locate source files and establish the required target dependencies.
  3. Compile each required module with its dependencies available.
  4. Produce module information for the compiler and object/library artifacts for linking, according to the build configuration.
  5. Compile the host’s source files against the modules it uses.
  6. Link the required executable code into the final application arrangement.

The exact artifact layout varies between Xcode and command-line builds. Three imports do not imply three separately downloaded packages, three runtime processes, or necessarily three dynamic frameworks. These .library declarations do not explicitly choose a static or dynamic library type.

An import is a compile-time language operation. It does not download the package, run the demo or instantiate a renderer.

5. Why Divide This Particular Package Into Three Modules?

The division follows three different responsibilities in the existing source. It makes those responsibilities enforceable through dependencies rather than relying only on folder names and developer convention.

Contracts: Agree On The Vocabulary

Contracts defines values such as CloseUpFrame, CloseUpRegion, CloseUpRequest, CloseUpRenderSource and CloseUpAFPoint, plus the CloseUpRegionStoring protocol.

For example, a frame identifies a source and its revision, while a request specifies the region and rendering source. A renderer and a view can exchange these values without either defining its own incompatible version.

This module uses Foundation and CoreGraphics, but it does not import SwiftUI or contain decoded image buffers. A client can use the region-record format without adopting the panel. “Contracts” describes a responsibility; it is not a special Swift keyword or a promise of permanent API stability.

Engine: Produce Inspection Pixels

Engine defines CloseUpRendering, CloseUpRender, crop geometry and rasterization. It imports Contracts because it needs the request and coordinate values.

A host can use CloseUpRasterizer with a custom user interface. The engine does not need to know which SwiftUI panel displays the resulting images, which photo is rated, or which RAW loader produced the source.

Notice that not every protocol is in Contracts. CloseUpRendering lives in Engine because it returns the engine’s image-bearing CloseUpRender. The division follows the actual API’s dependencies, rather than placing everything named “protocol” into the lowest module.

UI: Present And Coordinate Inspection

UI defines CloseUpPanel, CloseUpModel and its internal card model. It needs Contracts for frame/region values and Engine for rendering requests and results. It adds SwiftUI and Observation behavior on top of those lower layers.

This direction prevents a change in presentation from requiring the engine to import a view. If Contracts tried to depend on UI while UI already depended on Contracts, that would create a cycle. Shared low-level values must stay in a layer both higher-level components can depend on.

The split also creates access-control boundaries. For example, the actual CloseUpPanel and CloseUpModel are public, while CloseUpCardModel is internal to PhotoEvidenceUI. Moving everything into one module would remove those particular cross-module boundaries. Other access levels still apply within it.

6. Why Does A RawCull File Need All Three Imports?

Look at RawCull’s BurstCloseUpsView.swift. It performs several jobs at once:

Code used by the adapterDefining moduleImport
Frame, request and render-source valuesContractsimport PhotoEvidenceContracts
Rendering protocol, result and rasterizerEngineimport PhotoEvidenceEngine
Panel and session modelUIimport PhotoEvidenceUI

A file that merely handles saved-region values may need only Contracts. A custom renderer may need Contracts and Engine. The composition file uses declarations from all three, so it explicitly imports all three.

Imports apply to the source file containing them. Adding an import to another RawCull file does not provide that import to every file in the app target. Declarations in the app’s own module are a different matter: app files can refer to that module’s accessible declarations without importing each other by filename.

The dependency graph and source imports serve different purposes:

  • Target/product dependencies make required modules available to the build.
  • Source imports state which modules a particular file uses.
  • Access control determines which declarations that file may use across the module boundary.

An ordinary import PhotoEvidenceUI is not a designed umbrella export of all Contracts and Engine names. Do not rely on incidental transitive visibility. Import the modules that define the APIs your file directly uses. The current package provides no explicit PhotoEvidenceKit facade module.

7. Selecting Products In Xcode Or SwiftPM

In Xcode, adding the repository makes its products available for assignment to application targets. Select the library products the app directly consumes. Selecting UI also brings the lower target dependencies required to build UI; you do not separately download Contracts and Engine repositories.

For a Swift package consumer, the same distinction is explicit in its manifest. The following is an illustrative consumer configuration:

// In the consumer Package.swift, within Package(...):
dependencies: [
    .package(url: "https://github.com/rsyncOSX/PhotoEvidenceKit.git", from: "1.0.7")
],
targets: [
    .target(
        name: "MyInspectionFeature",
        dependencies: [
            .product(name: "PhotoEvidenceContracts", package: "PhotoEvidenceKit"),
            .product(name: "PhotoEvidenceEngine", package: "PhotoEvidenceKit"),
            .product(name: "PhotoEvidenceUI", package: "PhotoEvidenceKit")
        ]
    )
]

This excerpt belongs inside a complete manifest, not in a runtime Swift file. The package requirement permits compatible versions starting at 1.0.7; it is not an exact revision pin. The consumer’s resolution records the chosen revision. SwiftPM’s dependency guide describes package requirements and product dependencies separately.

Once configured, that consumer’s source files import the modules they use. Adding a product dependency alone does not create an import in their source text.

8. Why Not Offer Only import PhotoEvidenceKit?

It is possible. There are several designs, with different consequences.

Design A: Put Everything In One Target

The package could define one library product and one target, both named PhotoEvidenceKit, and move the three source groups into that target. Consumers would then use import PhotoEvidenceKit.

This reduces integration choices and can suit a small, cohesive library. However, Contracts, Engine and UI would share a module. A contracts-only consumer would consume that combined module, and the compiler would no longer enforce the current separation between those three internal layers.

Design B: Rename Only The Product

Suppose a maintainer instead changes the product declaration to:

.library(
    name: "PhotoEvidenceKit",
    targets: ["PhotoEvidenceContracts", "PhotoEvidenceEngine", "PhotoEvidenceUI"]
)

That offers one product containing multiple targets. It still does not create a PhotoEvidenceKit module. The module names remain the target names, so a single product selection alone does not deliver the desired single import.

Design C: Add A Facade Target

A fourth library target named PhotoEvidenceKit could depend on the three existing targets and expose a deliberate convenience API. Consumers of that facade could write import PhotoEvidenceKit, while advanced consumers could keep using the smaller modules.

The facade must actually provide the intended public API through wrappers, forwarding functions, public aliases or a consciously chosen export mechanism. An empty target that merely imports three dependencies does not automatically create a supported umbrella API for all their declarations. Export mechanisms also need language/toolchain review; underscored compiler features should not be presented as an ordinary stable-language recommendation.

The benefit is convenience. The cost is another API surface to design, document, test and maintain. A convenience name does not remove the underlying dependency graph or make all three lower layers independently unnecessary.

9. What This Design Buys, And What It Costs

AspectThree-module PhotoEvidenceKit design
ReuseContracts and Engine can be consumed without the SwiftUI panel
Dependency directionThe build graph enforces lower layers’ independence from UI
API boundariesCross-module use requires suitable access control
IntegrationConsumers choose products and write explicit module imports
MaintenanceMore target configuration and public boundary design
Build performanceDependencies can be reused independently, but module count alone guarantees no speedup
VersioningAll targets ship under the same package revision; three modules do not mean independent release versions

For PhotoEvidenceKit, the source demonstrates a useful boundary: describe an inspection, render it, then present it. Those steps can be reused separately. The decision is an architectural tradeoff, not a rule that every Swift package should have three modules.

10. Diagnose Import Problems By Checking The Correct Layer

SymptomCheck
No such module 'PhotoEvidenceKit'The package currently has no module with that name; use the actual module names
No such module 'PhotoEvidenceEngine'Product/target dependency, resolution, platform/toolchain requirements and successful dependency build
A type is unavailable in one fileThat file’s imports, the spelling of the type, its defining module and access level
Adding a folder did not create a new importA folder inside a target is not a new target/module
Linking fails after compilationRequired linked products/artifacts and target configuration; imports alone do not supply executable code

Start with the manifest, then identify the target owning the declaration, then inspect the consuming file’s imports. This follows the same structure the build system uses and avoids confusing a repository name with a module name.

Source Map

TopicPhotoEvidenceKit source
Package, products and targetsPackage.swift
Shared request and region valuesSources/PhotoEvidenceContracts/CloseUpContracts.swift
Engine imports and rendering boundarySources/PhotoEvidenceEngine/CloseUpRendering.swift
UI imports and public panelSources/PhotoEvidenceUI/CloseUpPanel.swift
Internal UI implementationSources/PhotoEvidenceUI/CloseUpCardModel.swift

Continue with How PhotoEvidenceKit Is Constructed for the detailed runtime contracts and RawCull integration, or return to the package catalog.


Last modified November 23, 2026: update (58d91c1)