通过SPM集成SwiftGen插件实现本地化时遇权限错误求助
SwiftGen Plugin集成SPM本地化项目权限问题排查与解决
问题描述
在多本地化包的SPM项目中集成SwiftGen Plugin,已完成官方配置步骤并编写如下swiftgen.yml:
input_dir: Sources/Example/Resources output_dir: Sources/Example/Generated strings: inputs: - en.lproj outputs: - templateName: structured-swift5 output: Strings.generated.swift
运行时报错:
Plug-in ended with non-zero exit code: 1
Error: You don’t have permission to save the file “Strings.generated.swift” in the folder “Generated”.
修改输出目录后问题依旧。
解决步骤
1. 检查Generated目录的系统权限
- 右键项目中的
Generated文件夹,打开「显示简介」,确认「共享与权限」板块中当前登录用户拥有「读与写」权限 - 若权限不足,点击锁图标解锁,修改权限后重新尝试构建
2. 调整输出目录为SPM插件兼容路径
SPM插件运行时的工作目录并非项目根目录,直接写入Sources子目录易触发权限限制。修改配置为输出到DerivedData缓存目录,后续再手动关联文件:
input_dir: Sources/Example/Resources # 改用相对插件工作目录的路径,避免直接写入源码目录 output_dir: ../Generated strings: inputs: - en.lproj outputs: - templateName: structured-swift5 output: Strings.generated.swift
完成配置后,右键目标SPM包选择「Add Files to [包名]」,选中生成的Strings.generated.swift,勾选「Copy items if needed」并确认文件归属当前包。
3. 验证Plugin在Package.swift中的关联配置
确保SwiftGen Plugin仅绑定到需要的目标包,而非全局项目:
.target( name: "Example", dependencies: [], plugins: [ .plugin(name: "SwiftGenPlugin", package: "SwiftGenPlugin") ] )
同时确认swiftgen.yml放置在SPM包的根目录(与Package.swift同级),而非Sources子目录下。
4. 清理缓存后重试
- 执行Xcode菜单「Product」→「Clean Build Folder」(快捷键Cmd+Shift+K)
- 删除DerivedData目录(可通过Xcode偏好设置→Locations找到对应路径)
- 重启Xcode后重新构建项目
内容的提问来源于stack exchange,提问作者planet-cheese
相关产品推荐
相关产品推荐

