|
| 1 | +--- |
| 2 | +name: swiftfindrefs |
| 3 | +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. |
| 4 | +--- |
| 5 | + |
| 6 | +# SwiftFindRefs |
| 7 | + |
| 8 | +## Purpose |
| 9 | +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. |
| 10 | + |
| 11 | +## Rules |
| 12 | +- Always run `swiftfindrefs` before editing any files. |
| 13 | +- Only edit files returned by `swiftfindrefs`. |
| 14 | +- Do not substitute `grep`, `rg`, IDE search, or filesystem heuristics for reference discovery. |
| 15 | +- Do not expand the file set manually. |
| 16 | +- If IndexStore/DerivedData resolution fails, stop and report the error. Do not guess. |
| 17 | + |
| 18 | +## Preconditions |
| 19 | +- macOS with Xcode installed |
| 20 | +- Project has been built at least once (DerivedData exists) |
| 21 | +- `swiftfindrefs` available in PATH |
| 22 | + |
| 23 | +## Canonical command |
| 24 | +Prefer providing `--projectName` and `--symbolType` when possible. |
| 25 | + |
| 26 | +```bash |
| 27 | +swiftfindrefs \ |
| 28 | + --projectName <XcodeProjectName> \ |
| 29 | + --symbolName <SymbolName> \ |
| 30 | + --symbolType <class|struct|enum|protocol|function|variable> |
| 31 | +``` |
| 32 | + |
| 33 | +Optional flags: |
| 34 | +- `--derivedDataPath <path>`: explicit DerivedData (or IndexStoreDB) path; skips discovery |
| 35 | +- `--verbose`: prints discovery steps, resolved paths, and diagnostics |
| 36 | + |
| 37 | +## Output contract |
| 38 | +- One absolute file path per line |
| 39 | +- Deduplicated |
| 40 | +- Script-friendly (safe to pipe line-by-line) |
| 41 | +- Ordering is not semantically meaningful |
| 42 | + |
| 43 | +## Standard workflows |
| 44 | + |
| 45 | +### Workflow A: Find all references |
| 46 | +1. Run `swiftfindrefs` for the symbol. |
| 47 | +2. Treat the output as the complete reference set. |
| 48 | +3. If more detail is needed, open only the returned files. |
| 49 | + |
| 50 | +### Workflow B: Fix missing imports after moving a symbol |
| 51 | +Use `swiftfindrefs` to restrict scope, then add imports only where needed. |
| 52 | + |
| 53 | +```bash |
| 54 | +swiftfindrefs -p <Project> -n <Symbol> -t <Type> | while read file; do |
| 55 | + if ! grep -q "^import <ModuleName>$" "$file"; then |
| 56 | + echo "$file" |
| 57 | + fi |
| 58 | +done |
| 59 | +``` |
| 60 | + |
| 61 | +Then for each printed file: |
| 62 | +- Insert `import <ModuleName>` in the imports block at the top. |
| 63 | +- Preserve existing import ordering/grouping. |
| 64 | +- Never add duplicate imports. |
| 65 | +- Do not reformat unrelated code. |
| 66 | + |
| 67 | +### Workflow C: Audit usage before deleting or renaming a symbol |
| 68 | +1. Run `swiftfindrefs` for the symbol. |
| 69 | +2. If output is empty, treat the symbol as unused (still validate via build/tests if needed). |
| 70 | +3. If non-empty, review the listed files before changing public API. |
| 71 | + |
| 72 | +## References |
| 73 | +- CLI details: references/cli.md |
| 74 | +- Playbooks: references/workflows.md |
| 75 | +- Troubleshooting: references/troubleshooting.md |
0 commit comments