How to use a custom build configuration in a Swift Package?

The reason for this feature is security. For a banking application it is very important that production env variables are not in debug build. They also have to come from a different source. So now I created 2 target libraries EnvironmentProduction and EnvironmentSandbox. I want both to be set as a dependency only when in release or in debug.

As a workaround I can now do

let dependencies: [Target.Dependency] = ["Environment", "EnvironmentProduction", "EnvironmentSandbox"]

 targets.append(
        .target(name: "Run",
                dependencies: dependencies,
                swiftSettings: [
                    .define("PRODUCTION", .when(configuration: .release)),
                    .define("SANDBOX", .when(configuration: .debug))
                ]
        )
    )

And in code then main.swift

#if PRODUCTION
import EnvironmentProduction
#else
import EnvironmentSandbox
#endif

...

But this would be more secure if I could do, as then the EnvironmentProduction does not have to be stated as a dependency, only for release builds.

#if canImport(EnvironmentProduction)
import EnvironmentProduction
#else
import EnvironmentSandbox
2 Likes