使用Tuist集成Periphery时,@testable导入主目标模块报错“No such module 'NameOfMyMainTarget'”
使用Tuist集成Periphery时,@testable导入主目标模块报错“No such module 'NameOfMyMainTarget'”
嘿,我之前也踩过这个坑!用Tuist给Periphery单独建Target的时候,很容易因为依赖配置、脚本参数或者构建设置的问题导致找不到主模块。下面是我亲测有效的几个解决办法,你可以挨个试试:
给Periphery Target添加主模块依赖
Periphery要分析主模块的代码,就必须让它明确依赖你的主目标。在你的.target配置里补上dependencies参数,把主模块加进去:.target( name: "\(TargetName.periphery)", destinations: .macOS, product: .framework, bundleId: "", sources: [], dependencies: [ .target(name: "\(TargetName.yourMainTarget)") // 替换成你的主目标实际名称 ], scripts: [ .pre( script: "./Tools/periphery/run_periphery.sh", name: "Run Periphery", basedOnDependencyAnalysis: false ) ], settings: .settings(base: [:]) )这样Tuist会保证主模块先编译完成,Periphery Target就能找到它的模块了。
检查Periphery脚本的参数配置
脚本里如果没正确指定主模块的信息,也会触发这个错误。打开run_periphery.sh,确保scan命令里的--targets、--schemes和你的主模块名称完全一致(大小写也要匹配),比如:./Tools/periphery/periphery scan \ --project ./YourProject.xcodeproj \ --targets "NameOfMyMainTarget" \ --schemes "NameOfMyMainTarget" \ --format xcode确保主模块开启测试可访问性
主目标的ENABLE_TESTABILITY必须设为YES,@testable import才能生效。你可以在主模块的Tuist配置里加上这个设置:.target( name: "\(TargetName.yourMainTarget)", // 其他配置... settings: .settings( base: [ "ENABLE_TESTABILITY": true ] ) )要是只想在Debug环境开启,也可以针对性配置:
settings: .settings( base: [:], configurations: [ .debug(name: .debug, settings: ["ENABLE_TESTABILITY": true]) ] )清理缓存后重建项目
有时候Xcode或Tuist的缓存会搞出奇怪的问题,试试执行这些命令重置一下:tuist clean tuist generate xcodebuild clean build -workspace YourProject.xcworkspace -scheme NameOfMyMainTarget再重新运行Periphery脚本,大概率能解决问题。
备注:内容来源于stack exchange,提问作者Alexander Germek
相关产品推荐
相关产品推荐

