Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

780
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda