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:
https://developer.apple.com/forums/thread/664295
To reproduce:
- Download code at: GitHub - ryanholden8/SwiftUI-Preview-Failing-Test-Project
- Open
PreviewFailingTestProject.xcodeproj
- Change Target at the top of the Xcode window from
PreviewFailingTestProject
toMyUICode
- Change deployment device to iPhone 12 mini (or any iOS device)
- Open file: LocalPackages > MyUICode > Sources > MyUICode > ThisViewCrashesPreview.swift
- Try to use the SwiftUI Previewer
- Crash will occur and preview will not render, see lines 8-11 of
ThisViewCrashesPreview.swift
for more details - 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.