Hello all!
I'm scratching my head with the problem:
Is it possible to import a module that contains a sample preview data, let's call it Mocks, using #if canImport(Mocks)
but only in DEBUG?
Modules are setup using package.swift like so:
import PackageDescription
let package = Package(
name: "Module",
platforms: [
.macOS(.v10_15), .iOS(.v15)
],
products: [
.library(
name: "Module",
targets: ["Module"]
),
.library(
name: "ModuleInterface",
targets: ["ModuleInterface"]
),
.library(
name: "ModuleMocks",
targets: ["ModuleMocks"]
)
],
targets: [
.target(
name: "Module",
dependencies: [
"ModuleInterface"
]
),
.target(
name: "ModuleInterface"
),
.target(
name: "ModuleMocks",
dependencies: [
"ModuleInterface"
]
),
.testTarget(
name: "ModuleTests",
dependencies: [
"Module",
"ModuleInterface",
"ModuleMocks"
]
),
]
)
code:
#if canImport(ModuleMocks)
import ModuleMocks
#Preview {
Text(MyModuleMock().method().name)
}
#endif
Idea is to have a reusable set of components that can provide test data for testing and previews, but with a guarantee that it will not be shipped in the production project when compiled in Release config.
What is blocking me atm, is linker error when build in the preview panel in Xcode, error is:
== PREVIEW UPDATE ERROR:
SchemeBuildError: Failed to build the scheme ”Submodules”
linker command failed with exit code 1 (use -v to see invocation)
Link Module_1A39B208D6406C_PackageProduct (arm64):
ld: Undefined symbols:
ModuleMocks.MyModuleMock.__allocating_init() -> ModuleMocks.MyModuleMock, referenced from:
closure #1 @Swift.MainActor () -> SwiftUI.View in static Module.$s6Module33_FEB919C066B33920805A0FAA8DF556D4Ll7PreviewfMf_15PreviewRegistryfMu_.makePreview() throws -> DeveloperToolsSupport.Preview in Module.o
type metadata accessor for ModuleMocks.MyModuleMock, referenced from:
closure #1 @Swift.MainActor () -> SwiftUI.View in static Module.$s6Module33_FEB919C066B33920805A0FAA8DF556D4Ll7PreviewfMf_15PreviewRegistryfMu_.makePreview() throws -> DeveloperToolsSupport.Preview in Module.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Link Submodules (arm64):
clang: error: no such file or directory: '/Users/mega/Library/Developer/Xcode/DerivedData/Submodules-enrfgjxdlhzoadgdtvdcfvzpnhgj/Build/Intermediates.noindex/Previews/Submodules/Products/Debug-iphonesimulator/PackageFrameworks/Module_1A39B208D6406C_PackageProduct.framework/Module_1A39B208D6406C_PackageProduct'
here's an example project, file of interest is Module.swift