Search code examples
iosswiftxcodeswiftuiswift-package-manager

"noWorkspaceArena" error with SwiftUI hot-reload in a Swift Package


I'm trying to build a Swift Package that utilizes SwiftUI to create chart data visualizations. My understanding was that I would be able to use SwiftUI's hot-reloading in Xcode to preview the components within my package during development, however when I'm trying to resume the preview, I'm getting the error "Cannot preview in this file - unexpected error occurred" the diagnostics show the following error.

Error:

UVKit.XcodeWorkspaceBuildEnvironment.(unknown context at $141a48360).(unknown context at $141a48368).ValidationError.noWorkspaceArena)


GenericHumanReadableError: unexpected error occurred

noWorkspaceArena

This is what my view looks like.

PieChartView.swift:

import SwiftUI

public struct PieChartView : View {
    public var data: [(Double, Color)]
    public var title: String
    
    public init(data: [(Double, Color)], title: String) {
        self.data = data
        self.title = title
    }
    
    public var body: some View {
        Text(title)
    }
    
}

#if DEBUG
struct PieChartView_Previews : PreviewProvider {
    static var previews: some View {
        PieChartView(data:[(56.0, Color.red),(78, Color.blue),(53, Color.green)], title: "Title")
    }
}
#endif

Here is my package

Package.swift:

import PackageDescription

let package = Package(
    name: "MesmerCharts",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "MesmerCharts",
            targets: ["MesmerCharts"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "MesmerCharts",
            dependencies: []),
        .testTarget(
            name: "MesmerChartsTests",
            dependencies: ["MesmerCharts"]),
    ]
)

Solution

  • To resolve my issue, I created an .xcworkspace to contain my "test application" and my Swift Package. To do this, you can perform the following steps:

    1. Create a new workspace and open it in Xcode.
    2. Within the workspace, create a new project by going to File > New > Project...
    3. Create a single view app and add it to the workspace you created.
    4. Once the test application is added to your workspace, you will need to add the Swift Package, you can do this by going to File > New > Swift Package...
    5. Once again, add the Swift Package to the workspace that you created as you did with the test application as shown in step 3.
    6. You will now see both the application and the package in your workspace, however we still need to modify the application's .xcodeproj to use the package.

    enter image description here.

    1. To do this, select your application, and under targets in the General tab, you will see Frameworks, Libraries, and Embedded Content, click the + symbol and you will be prompted with frameworks and libraries that you can add to the project, select your Swift Package.

    enter image description here

    1. You will now see the Swift Package as an added library.

    enter image description here

    Side Note

    Whenever the Swift Package is updated, you will need to rebuild it before using it in the test application.