SwiftUI previews fail with a very simple SwiftPM package

I already filed feedback FB13777941 for this, but wanted to ask if anyone has the same problem, since I can't find anything on google or SO.

I created a new package in Xcode (15.3) and added this to it:

import SwiftUI

struct Test: View {
    var body: some View {
        Text("Hello world")
    }
}

#Preview {
    Test()
}

The package builds, tests can run. Previews don't work.

This is more or less the error I get from the diagnostics button (formatted for readability):

LoadingError: failed to load library at path ”PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct”: Optional(dlopen(PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct, 0x0000):

    tried:

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),

    '/System/Volumes/Preboot/Cryptexes/OSPATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),

    '/System/Volumes/Preboot/Cryptexes/OSPATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),

    'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')))

I don't remember ever having this problem. The arm64 vs arm64e makes no sense to me - why would Xcode get that wrong... and only for previews?

I've tried restarting, deleting derived data, the usual rain dances.

Any ideas?

Answered by Aurelian in 788079022

Well, I updated to Xcode 15.4 and it works now. Don't see anything about this in the changelog, but I'm glad it's sorted.

Review the architecture value under build settings and double-check any Rosetta settings, such as the sim selected. Create a new project and compare the architecture settings between projects.

Thanks, but this is a Swift Package. There are no build settings except for Package.swift.

Try wrapping the preview code in the following or something like below.

#if arch(arm64) && os(macOS) || os(iOS)

import SwiftUI

struct Test: View {
    var body: some View {
        Text("Hello world")
    }
}

#Preview {
    Test()
}

#endif

Read here: https://forums.swift.org/t/swift-package-is-built-as-a-universal-binary-in-main-project/54075

or replace preview macro with

struct Test_Previews: PreviewProvider {
    static var previews: some View {
       Test()
    }
}

Nope, neither of those work.

Creating an Xcode project as a wrapper that only exists to hold the package is one workaround.

But this is clearly some kind of bug, because it worked in previous Xcode releases. It also works with some of my other packages (with no Xcode project).

Accepted Answer

Well, I updated to Xcode 15.4 and it works now. Don't see anything about this in the changelog, but I'm glad it's sorted.

SwiftUI previews fail with a very simple SwiftPM package
 
 
Q