Add code if a SwiftPM project is being run from Xcode

I want to be able to conditionally add an expression to an app if it's being built/run from Xcode, what's the best way to do that?


For context, since Xcode 16, Xcode will launch multiple copies of an app at once when running (I've seen this with SDL, SwiftUI, Raylib, both in Swift & C++). I'm working on a project with Raylib and it will open about four instances of the process.

For whatever reason, adding a small delay before the window is created works around the issue. This is at the top of my main function:

    /// Application main entry point.
    static func main() throws {
        // If a window opens too fast, Xcode (since 16) will open up to three or
        // four copies of it. For whatever reason, putting a sleep up front
        // works around this bug.
        sleep(1)
        // ...
    }

I have opened a feedback item for this, but in the meantime the workaround is useful while working in Xcode.

But I don't really want to have a second delay on starting where I don't need to. #if DEBUG would work in a pinch, but is there a way for to detect Xcode in the Package.swift, and impact it from there? Or detect that the process was launched from Xcode at runtime?

1 Like