Annoying limitation of PackageDescription.BuildConfiguration

I've been converting a large monolithic application to move certain code into packages; mostly this has gone well.

However, one area I am having trouble with is getting various code in my packages to respect an #if DEBUG section. I use these a lot in the debug builds to run all sorts of other diagnostics, performance and logging that I don't even want compiled for release.

i.e in some library code:

#if DEBUG
extension SomeClass {
    public func printTopSecretInternalState {
        ....
    }
}
#endif

and then in the main app:

let someClass = getSomeClass()
#if DEBUG
someClass.printTopSecretInternalState()
#endif
// carry on using someClass

It seemed to me that adding this to my target would do what I want:

    swiftSettings: [.define("DEBUG", .when(configuration: .debug))]

But when I built my app in its debug configuration it complained in the app code that SomeClass doesn't have a printTopSecretInternalState function.

After some experimentation I figured out what was going on, in my Xcode project, the build configurations are not named "Debug" or "Release", but instead we have something similar to these:

If I rename one of the "Dev' configurations to "Debug" then the above #if DEBUG stuff works as expected but that is a pain as:

  1. I'd need to be constantly renaming one of the Dev configurations to Debug depending on which one I want to debug.
  2. There are other parts of the build process (i.e scripts) that rely on the configuration name to decide to do (or not do) things.

Is there no way to designate a configuration as being for 'debug' without having to have the explicit "Debug" name??

Nope.

I feared as much :frowning:. Where's the best place to request such functionality?

bugs.swift.org and / or the Feedback app.

That is redundant. #if DEBUG is a standard condition that works out of the box with SwiftPM.

Your trouble seems to be in correlating your custom Xcode configurations to the SwiftPM --configuration option. I think Xcode tries to be smart with its default configurations (or those that sufficiently resemble them), but defaults to release for anything else. What you need to ask for (in the Feedback Assistant) is a way to explicitly specify which you want in your custom Xcode configuration in order to bypass its heuristics.

Yes, further experimentation indeed showed that everything is release unless it is known to be debug.

Any idea what heuristics Xcode uses for determining debug? Is it really just the Configuration name being "Debug" or is there some combination of resolved build settings that can trigger it. You'd think an optimisation level of none would also count, but apparently not.

It is pretty simplistic and solely based on the names. IIRC, if the name contains "debug" or "development", it is considered a debug configuration.

Bug raised:
https://bugs.swift.org/browse/SR-16060