SwiftUI Previewer crashes while in swift package that depends on another's packages Bundle.module reference

On Xcode 12.1, SwiftUI Previewer crashes when it references another swift package it depends on and that swift package uses Bundle.module.

Basic code snippet that crashes:

import SwiftUI
import Theme // Import package that uses Bundle.module

/// This view will crash the previewer
struct ThisViewCrashesPreview: View {
    var body: some View {
        Text("Hello, World!")
            // If you comment out the following line, the previewer will render
            .foregroundColor(Color.themeGreenFromXCAssets)
            // Or use this line, the preview will also start to work
            .foregroundColor(Color.colorThatDoesNotUseModuleReference)
    }
}

struct ThisViewCrashesPreview_Previews: PreviewProvider {
    static var previews: some View {
        ThisViewCrashesPreview()
    }
}

Theme Code:

import SwiftUI

public extension Color {
    
    static let themeGreenFromXCAssets = Color("ThemeGreen", bundle: .module)
    
    static let colorThatDoesNotUseModuleReference = Color.init(red: 0.3, green: 0.6, blue: 0.9)
    
}

Crash Log States:

Application Specific Information:
Fatal error: unable to find bundle named Theme_Theme: file Theme/resource_bundle_accessor.swift, line 27

Other developers with the same issue:


To reproduce:

  1. Download code at: https://github.com/ryanholden8/SwiftUI-Preview-Failing-Test-Project
  2. Open PreviewFailingTestProject.xcodeproj
  3. Change Target at the top of the Xcode window from PreviewFailingTestProject to MyUICode
  4. Change deployment device to iPhone 12 mini (or any iOS device)
  5. Open file: LocalPackages > MyUICode > Sources > MyUICode > ThisViewCrashesPreview.swift
  6. Try to use the SwiftUI Previewer
  7. Crash will occur and preview will not render, see lines 8-11 of ThisViewCrashesPreview.swift for more details
  8. NOTE: See file: PreviewFailingTestProject > PreviewFailingTestProjectApp.swift > MainWindow View on lines 13-19. This renders fine on both the SwiftUI Previewer and when deployed to device.
8 Likes

This post is a bit old but I've just been trying to see if this has been fixed in the latest Xcode 13 beta and sadly it has not.

The FixedBundleAccessor.swift hack still works but it feels very prone to breaking in a future release.

I have filed feedback - FB9728781.

Terms of Service

Privacy Policy

Cookie Policy