Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

779
Views
Swift Package Manager: two similar targets different only with the dependencies

I'm migrating a library built and tested as an Xcode Project to Swift Package Manager. The project contains 3 targets: the library itself, the same library but with different dependency and the test (application) target.

So far, porting the library was easy.

Now I'm interested in building the same library with a different dependency (mocks) which could be tested.

In practice, the Package.swift file looks like this:

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "TargetLibrary",
    platforms: [
        .iOS(.v14), .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "TargetLibrary",
            targets: ["TargetLibrary"]),
    ],
    dependencies: [
        .package(
            url: "ssh://git@github.com/DependentLib",
            .upToNextMinor(from: "1.3.18")
        ),
    ],
    targets: [
        .target(
            name: "TargetLibrary",
            dependencies: [
            // Using the "main" dependency for the "TargetLibrary"
            .product(name: "DependentLib", package: "DependentLib"),
            ],
            path: "src",
            exclude: [

            ],
            
            sources:
                [
                "sourcefiles"
                ],
            publicHeadersPath: "SwiftPackage/include",
            cxxSettings: [
                .headerSearchPath("lib"),
            ]),

            // This target is virtually identical, to the previous one,
            // the only difference is that it is using a different dependency library
            // from the same package
        .target(
            name: "TargetLibraryWithMock",
            dependencies: [
            // Using the "mock" dependency for this library
            .product(name: "DependentLibMock", package: "DependentLib"),
            ],
            path: "src",
            exclude: [

            ],
            
            sources:
                [
                "sourcefiles"
                ],
            publicHeadersPath: "SwiftPackage/include",
            cxxSettings: [
                .headerSearchPath("lib"),
            ]),

            // Now testing the "TargetLibraryWithMock"
            .testTarget()....

    ],
    cxxLanguageStandard: .cxx14
)


Now, of course, if I just duplicate two targets, I'll get a target overlapping sources error

So, the question is: How can I build two targets different only by the dependencies they include?

It looks like target dependency condition could have helped here

            dependencies: [
                .target(name: "DependentLib", condition: .when(configuration: .build)),
                .target(name: "DependentLibMock", condition: .when(configuration: .test)),
            ]

but only platform-specific conditionals exist now. What could be solutions to this problem?

Graphical representation of the desired result:

enter image description here

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!