Sub topic 15
Troubleshooting missing or unexpected results
This chapter assumes you can create tags and smart folders and customise the sidebar, as covered earlier in the tutorial. It also assumes you can open Terminal and run a command. The goal is to work out why a tag search or smart folder shows too few items, too many, or the wrong ones. It also covers sidebar entries that vanish or stop working.
Start from the data flow
Every symptom in this chapter comes from one of four layers. Checking them in order is faster than trying fixes at random.
- The file. A tag is stored on the file itself, in an extended attribute named
com.apple.metadata:_kMDItemUserTags. - The Spotlight index. Spotlight reads that attribute and stores it as the metadata field
kMDItemUserTags. Tag searches and smart folders query the index. They do not scan your disks. - The query. A Finder search or smart folder combines a scope (where to look) with criteria (what to match).
- The sidebar entry. The sidebar only shows a link to a folder, tag, or
.savedSearchfile. It does not hold results.
Check the file and the index
Pick one file that should appear and look at what Spotlight knows about it.
mdls -name kMDItemUserTags -name kMDItemFSContentChangeDate ~/Documents/report.pdfIf kMDItemUserTags prints (null), Spotlight has no tag for that file. Next, read the attribute on disk.
xattr -l ~/Documents/report.pdfHere is how to read the two results together:
- The attribute is on disk but
mdlsshows(null). The index is stale or the file is not being indexed. See the next section. - The attribute is missing on disk. The tag was lost. This happens when a file is copied to a volume that cannot store extended attributes, or when an app saves by writing a new file and drops the old attributes. Re-apply the tag.
- Both agree and show the tag. The index is fine, so the problem is in the query.
Indexing problems
Indexing has not finished
After a macOS update, a restore from backup, or a large file import, Spotlight rebuilds parts of its index in the background. Results stay incomplete until it finishes. Click the Spotlight search field in the menu bar. A progress message there means indexing is still running. Wait, then search again.
Indexing is off for the volume
Check the status of the startup volume or any other mounted volume:
mdutil -s /
mdutil -s /Volumes/ArchiveIndexing enabled. is the healthy answer. Indexing disabled. means nothing on that volume can appear in a smart folder. Some external drives and network shares are not indexed by default. Smart folders cannot find files on them unless the volume is indexed.
The folder is excluded
Open System Settings, go to Spotlight, then Search Privacy. Any folder or disk listed there is excluded from the index. Remove it from the list to include it again. A common mistake is excluding a project folder to reduce noise and then forgetting it, so tag searches silently skip everything inside.
Forcing a rebuild
A rebuild is a last resort for an index that disagrees with the disk. The least invasive way is to add the affected folder to the Privacy list and then remove it. Spotlight discards that folder's entries and indexes it again. You can also rebuild a whole volume from Terminal, which needs administrator rights:
sudo mdutil -E /Searches are incomplete for minutes to hours afterwards, depending on how much data you have. Run it only when the checks above point to a bad index.
Query problems
If mdls shows the tag, ask Spotlight directly whether a search finds the file. This bypasses Finder's window entirely.
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'If the file is listed here but not in Finder, the problem is in the Finder search settings, not the index. These are the usual causes:
- Scope. The search bar scope is set to the current folder when you wanted This Mac, or the reverse. Check the scope buttons under the search field. In a smart folder, check the "Search" setting that restricts it to a folder.
- All versus Any. In the criteria editor, the first menu chooses whether items must match all rules or any rule. Switching from All to Any makes results balloon. Switching from Any to All makes them vanish.
- Kind and file extension rules. A rule such as Kind is PDF excludes images of the same document. A rule on file extension is case-sensitive in some cases. Test with one rule at a time.
- Date rules. Last modified is relative to today. A smart folder saved as "within the last 7 days" changes its results daily. This is expected. The Terminal equivalent shows the same behaviour:
mdfind -onlyin ~/Documents 'kMDItemFSContentChangeDate >= $time.today(-7)'- Hidden and system files. Finder omits system and hidden files from search results unless a rule asks for them. Add the "System files" criterion and set it to "are included" when you need them.
- Tag names that look alike. Tags are matched by name. Two tags named Red with different colors are the same tag to Spotlight. Two tags named Work and Work (with a trailing space) are different. After renaming or merging tags, files tagged before the change may need a re-index before they match the new name.
Cloud and copy effects
Files that macOS stores only in iCloud Drive (shown with a cloud icon) still appear in searches by name and tag, because the metadata is local. Content-based criteria, such as text inside a document, may not match until the file is downloaded. Files copied to FAT or exFAT drives, or to some network shares, may lose their tags. A smart folder that depends on tags then returns nothing for those copies.
Sidebar problems
The sidebar is separate from the search layers, so check it independently.
| Symptom | Likely cause | Fix |
|---|---|---|
| A built-in item such as Documents or AirDrop is gone | It is unchecked in Finder Settings | Finder, Settings, Sidebar tab, check the item |
| A tag does not appear in the sidebar | The tag is not selected in the Tags list of Sidebar settings | Tick it in Finder Settings, Sidebar |
| A smart folder is not in the sidebar | It was saved without "Add To Sidebar", or its sidebar item was removed | Open the `.savedSearch` file and drag it to the sidebar |
| A folder entry is dimmed or opens the wrong place | The target was moved, renamed, or deleted | Remove the entry, then drag the folder in again |
| A smart folder opens but is empty | The file exists, but the query returns nothing | Use the query checks above |
To remove a stale sidebar entry, hold Command and drag it out of the sidebar, or Control-click it and choose Remove from Sidebar. This removes only the shortcut, never the folder itself.
Inspect the saved search itself
A smart folder is a property list file. If the Finder editor shows odd criteria, read the file.
ls ~/Library/Saved\ Searches
plutil -p ~/Library/Saved\ Searches/Recent\ Documents.savedSearchLook at RawQuery for the Spotlight query and at SearchScopes for the folders it searches. A scope that points to a path that no longer exists is a common cause of an empty smart folder. If you saved the smart folder somewhere other than the default location, ls will not show it, so use mdfind 'kMDItemFSName == "*.savedSearch"' to find it.
A repeatable diagnostic script
This script collects the main facts about one file and one tag in a single run.
#!/bin/bash
# usage: ./check.sh /path/to/file TagName
file="$1"
tag="$2"
dir="$(dirname "$file")"
echo "== Index status =="
mdutil -s "$dir"
echo "== Spotlight metadata =="
mdls -name kMDItemUserTags "$file"
echo "== Does a tag query find the file? =="
if mdfind -onlyin "$dir" "kMDItemUserTags == \"$tag\"" | grep -qxF "$file"; then
echo "yes"
else
echo "no"
fiRun it with chmod +x check.sh and then ./check.sh ~/Documents/report.pdf Red. Use a full path for the file, because mdfind prints full paths and the comparison needs an exact match.
Exercise
A smart folder named "Active Projects" matches files tagged Green. A file you just tagged Green does not appear. xattr -l shows the tag on the file, but mdls -name kMDItemUserTags prints (null). What is the most likely cause, and what are two steps to fix it?
Show answer
The file is not in Spotlight's index, so the smart folder cannot see it. This usually means indexing is still running, the volume has indexing disabled, or the folder is in the Search Privacy list. Run mdutil -s on the volume to check its status. Then either wait for indexing, remove the folder from Search Privacy, or add and remove the folder from that list to force a re-index. Rebuilding the whole volume with sudo mdutil -E / is the last resort.
Exercise
A smart folder returns thousands of items after you add a fourth rule. What setting should you check first, and why?
Show answer
Check whether the match menu at the top of the criteria changed from "all" to "any". With "any", an item needs to satisfy only one rule, so adding rules widens the results. Setting it back to "all" makes each added rule narrow the results.