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.
11 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.

1 Like

This appears to be an issue still in Xcode 13.2.1 — what's the path forward here? Have no Apple teams needed the ability to preview SwiftUI from a package that relies on other (local) packages? It's a bit discouraging in that it's a major obstacle to modularizing large SwiftUI projects, no?

1 Like

Still an issue in Xcode 13.3. Previews crash as soon as I add a view modifier that applies a font from a dependency's Bundle.module. It's basically made the previews useless.

Apparently there is a macro that indicates 'building for previews': ENABLE_PREVIEWS. So return a valid value if it's true.

#if ENABLE_PREVIEWS
    static let themeGreenFromXCAssets = Color.green
#else 
    static let themeGreenFromXCAssets = Color("ThemeGreen", bundle: .module)
#endif

There is a similar issue with theme colors that don't work with IBDesignable.

Apple just updated my ticket (FB8880328)

From "Recent Similar Reports: Less to a few"
To "Recent Similar Reports: None"

Unfortunately, it's gaining less traction.

If we want to see Apple address this issue please take a few minutes to submit a ticket via there feedback channel here: https://feedbackassistant.apple.com It's pretty easy :)