微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🔍

精准追踪 Swift 符号引用

swiftfindrefs · v1.0.4
开发 安全 S+ 级 作者 michaelversus 公开目录收录
2.4k 下载 1 安装 8.8k 收藏 16 浏览

简介

基于 Xcode IndexStoreDB 精准定位 Swift 符号引用,杜绝文本搜索遗漏,确保重构安全完整。

原始描述

Use swiftfindrefs (IndexStoreDB) to list every Swift source file referencing a symbol. Mandatory for “find references”, “fix missing imports”, and cross-module refactors. Do not replace with grep/rg or IDE search.

通过你的 Agent 添加

安装目录:swiftfindrefs/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。

点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。

或者手动安装

Claude Code~/.claude/skills/swiftfindrefs/
Codex CLI~/.codex/skills/swiftfindrefs/
DeepSeek Harness~/.agents/skills/swiftfindrefs/

包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可; 目录名不用换,也不要把包直接解压到 skills 根目录。 技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。

中文使用说明 来自收录源

核心用法

SwiftFindRefs 是一个面向 Swift 开发者的代码引用查找技能,专门用于解决传统文本搜索(grep/rg/IDE 搜索)在跨模块场景下的不完整性问题。该技能通过调用 swiftfindrefs 命令行工具,直接查询 Xcode 的 IndexStore(位于 DerivedData 中)来获取符号的精确引用关系。

核心命令结构为:swiftfindrefs --projectName <项目名> --symbolName <符号名> --symbolType <类型>,支持 class/struct/enum/protocol/function/variable 等符号类型。可选参数包括 --derivedDataPath 指定自定义 DerivedData 路径,以及 -v 开启详细输出用于诊断。

该技能定义了三种标准工作流:A)基础引用查找——获取符号的完整引用文件清单;B)修复缺失导入——在符号跨模块移动后,精准定位需要添加 import 语句的文件;C)API 审计——在删除或重命名符号前,验证其实际使用情况。

显著优点

语义级精确性:不同于文本搜索可能误匹配注释、字符串或相似命名,IndexStoreDB 基于编译器索引提供准确的语义引用关系,确保跨模块依赖不遗漏。

重构安全性:强制约束"仅编辑 swiftfindrefs 返回的文件",从源头避免不完整重构导致的编译失败或运行时异常。

自动化友好:输出格式设计为脚本友好型——每行一个绝对路径、自动去重、可直接管道传递,便于集成到 CI/CD 或批量处理脚本中。

工程规范导向:明确禁止手动扩展文件集合,培养开发者依赖编译器索引而非直觉判断的工程习惯。

潜在缺点与局限性

平台绑定严重:完全依赖 macOS + Xcode 生态,Linux 或其他 Swift 开发环境无法使用。Windows 开发者、服务器端 Swift(Swift on Server)场景均不适用。

前置条件苛刻:要求项目至少完成一次成功构建以生成 DerivedData/IndexStore,对于全新克隆的仓库或构建失败状态无法立即工作。

工具链依赖:swiftfindrefs 需单独安装并位于 PATH 中,未随 Xcode 默认安装,增加了环境配置复杂度。

无实时增量更新:IndexStore 的更新滞后于代码修改,需重新构建才能反映最新引用关系,不适合与文件系统监听工具搭配实现实时重构辅助。

符号类型强制指定:使用时必须提供准确的 symbolType,对泛型、类型别名等复杂场景的分类判断可能增加认知负担。

适合的目标群体

iOS/macOS 原生开发团队:特别是维护大型多模块 Xcode 项目、需要频繁进行跨模块重构的工程师。

Swift 框架维护者:需要保证公共 API 变更时完整评估影响范围的库作者。

代码审查与架构治理角色:负责制定团队编码规范、审查大规模重构方案的技术负责人。

CI/CD 流水线设计者:希望将引用分析自动化集成到预提交检查或发布流程中的 DevOps 工程师。

不适合:跨平台 Swift 开发者、纯 SPM 命令行工具开发者(无 Xcode 环境)、以及追求实时 IDE 体验的开发者。

使用风险

性能风险:首次查询或大型项目可能因 IndexStore 规模导致响应延迟,建议在非阻塞流程中使用。

环境漂移风险:团队成员 Xcode 版本、DerivedData 路径配置不一致可能导致结果差异,需统一工程规范。

索引过期风险:若依赖缓存的 IndexStore 数据而未重新构建,可能基于过时信息做出错误编辑决策。

工具链缺失风险:swiftfindrefs 未广泛普及,新成员 onboarding 时需额外安装指导。

误删风险:输出为空时虽可标记符号为"未使用",但技能明确建议仍需通过构建/测试验证,不可完全依赖静态分析结果执行删除操作。

技能正文


name: swiftfindrefs description: Use swiftfindrefs (IndexStoreDB) to list every Swift source file referencing a symbol. Mandatory for “find references”, “fix missing imports”, and cross-module refactors. Do not replace with grep/rg or IDE search.

SwiftFindRefs

Purpose

Use swiftfindrefs to locate every Swift source file that references a given symbol by querying Xcode’s IndexStore (DerivedData). This skill exists to prevent incomplete refactors caused by text search or heuristics.

Rules

  • Always run swiftfindrefs before editing any files.
  • Only edit files returned by swiftfindrefs.
  • Do not substitute grep, rg, IDE search, or filesystem heuristics for reference discovery.
  • Do not expand the file set manually.
  • If IndexStore/DerivedData resolution fails, stop and report the error. Do not guess.

Preconditions

  • macOS with Xcode installed
  • Project has been built at least once (DerivedData exists)
  • swiftfindrefs available in PATH

Canonical command

Prefer providing --projectName and --symbolType when possible.

swiftfindrefs \
  --projectName <XcodeProjectName> \
  --symbolName <SymbolName> \
  --symbolType <class|struct|enum|protocol|function|variable>

Optional flags:

  • --derivedDataPath <path>: explicit DerivedData (or IndexStoreDB) path; skips discovery
  • -v, --verbose: enables verbose output for diagnostic purposes (flag, no value required)

Output contract

  • One absolute file path per line
  • Deduplicated
  • Script-friendly (safe to pipe line-by-line)
  • Ordering is not semantically meaningful

Standard workflows

Workflow A: Find all references

  1. Run swiftfindrefs for the symbol.
  2. Treat the output as the complete reference set.
  3. If more detail is needed, open only the returned files.

Workflow B: Fix missing imports after moving a symbol

Use swiftfindrefs to restrict scope, then add imports only where needed.

swiftfindrefs -p <Project> -n <Symbol> -t <Type> | while read file; do
  if ! grep -q "^import <ModuleName>$" "$file"; then
    echo "$file"
  fi
done

Then for each printed file:

  • Insert import <ModuleName> in the imports block at the top.
  • Preserve existing import ordering/grouping.
  • Never add duplicate imports.
  • Do not reformat unrelated code.

Workflow C: Audit usage before deleting or renaming a symbol

  1. Run swiftfindrefs for the symbol.
  2. If output is empty, treat the symbol as unused (still validate via build/tests if needed).
  3. If non-empty, review the listed files before changing public API.

References

  • CLI details: references/cli.md
  • Playbooks: references/workflows.md
  • Troubleshooting: references/troubleshooting.md

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

标签

swiftrefactoringstatic-analysisxcodeindexstoredbclicode-search

同类 Skill