SwiftUI debug preview attach failed (Not allowed to attach to process

Hi there, for some time now I'm having trouble debugging SwiftUI previews in Xcode. Whenever I select "Debug Preview" it fails with this error message:

error: attach by pid '12009' failed -- attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries when the attached failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.)

When I look into the console messages, it says:

macOSTaskPolicy: (com.apple.debugserver) may not get the task control port of (XCPreviewAgent) (pid: 12009): (XCPreviewAgent) is hardened, (XCPreviewAgent) doesn't have get-task-allow, (com.apple.debugserver) is a declared debugger(com.apple.debugserver) is not a declared read-only debugger

Running the preview in non-debug mode ("Live Preview") works absolutely fine. It happens always and with an absolute minimal SwiftUI example:

Package.swift:

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

import PackageDescription

let package = Package(
    name: "SwiftuiDebugIssue",
    platforms: [.iOS(.v14)],
    products: [
        .library(
            name: "SwiftuiDebugIssue",
            targets: ["SwiftuiDebugIssue"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "SwiftuiDebugIssue",
            dependencies: []),
    ]
)

Sources/SwiftuiDebugIssue/MyView.swift:

import SwiftUI

struct MyView: View {
    var body: some View {
        Text("Hello, world!")
    }
}

struct MyView_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
    }
}

Relevant versions:

  • macOS 11.5.2 (20G95)
  • Xcode 12.5.1 (12E507)

Thanks for any hints/help

Hi,

Sorry to hear you are having problems with debugging previews. This feature has actually been removed from the most recent Xcode release as detailed in its release notes:

Previews

<…>

Deprecations

Xcode 13 no longer includes a menu item in the Previews canvas for debugging a preview. Instead, use the Debug > Attach to Process menu item to attach the debugger to your previewed app. (73981969)

So I'd suggest either updating to the latest release, or attempting the workaround mentioned in the deprecations note.

When running a preview on an app target, the target is used as the preview's host. When running a preview on non-app targets, Xcode is using XCPreviewAgent as the host. The problem is that XCPreviewAgent process does not allow debugging. But we can workaround this.

The XCPreviewAgent is just a dummy iOS app without any logic, so we can create our own app with same name and bundle identifier, build it with debug capabilities and install it on the simulator used for running the preview. To do that, first install the app on normal simulator by running the app, then copy the bundle to the preview simulator (you can find the path by checking the issue report in Xcode's preview canvas). After that we will be able to attach debugger to now our custom XCPreviewAgent and debug our previews.

I'm using this hack to develop SwifUI views in isolation as a separate Swift Package without a real need for a project file. Running SwiftUI previews on the main app is very inefficient as the whole project has to be rebuilt every time you run a preview.

@tarbayev

I know this has been a while but does this workaround still work? I found the real XCPreviewAgent for the active simulator, copied its bundle id com.apple.dt.XCPreviewAgent and created a new iOS app on the same simulator with the same bundle id. Built and ran it on the simulator but it didn't appear to use it when running previews. Also tried swapping out the XCPreviewAgent app bundle with my version in the Preview device folder and this didn't work either.

I've got a but that happens on Previews but doesn't happen on the exact same code on Simulator and I'm wanting to debug the Preview to see what is going on. This is quite frustrating.

SwiftUI debug preview attach failed (Not allowed to attach to process
 
 
Q