This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
make build- Build the Swift packagemake test- Run tests with filtered output (uses custom script to reduce noise)make format- Format Swift code in Sources/ and Tests/ directoriesmake clean- Clean build artifacts
swift run watch <path>- Monitor file system changes in real-timeswift run watch <path1> <path2>- Monitor multiple paths simultaneouslyswift run watch- Show CLI usage help
AsyncFileMonitor is a modern Swift 6 package that provides async/await file system monitoring using Apple's FSEvents API. The architecture is built around these key components:
- AsyncFileMonitor (
AsyncFileMonitor.swift) - Main convenience API providing staticmonitor()functions - FolderContentMonitor (
FolderContentMonitor.swift) - Core implementation containing the actual FSEventStream management - FolderContentChangeEvent (
FolderContentChangeEvent.swift) - Event data structure representing file system changes - Change (
Change.swift) - OptionSet wrapper around FSEventStreamEventFlags providing type-safe change detection
- AsyncStream-based: All monitoring returns
AsyncStream<FolderContentChangeEvent>for natural async/await integration - RAII Resource Management: Each call to
monitor()creates an independentStreamHandlerthat manages its own FSEventStream lifecycle - Zero Dependencies: Pure Swift implementation using only Foundation and CoreFoundation
- Independent Streams: Multiple concurrent streams can monitor the same path independently, each with their own FSEventStream
The StreamHandler class (private) manages FSEventStream lifecycle:
- Creates FSEventStream with file-level events enabled (
kFSEventStreamCreateFlagFileEvents) - Uses main dispatch queue for event delivery
- Properly handles stream creation, start, stop, invalidation, and release
- Converts raw FSEventStreamEventFlags to typed Change options
Uses Swift Testing (not XCTest):
- Tests are written with
@Testattributes - Async tests use
confirmation()helper for event verification - TestHelpers.swift provides
matches()extension for event filtering in tests - Integration tests create temporary directories and verify actual file system events
The library handles complex FSEvents patterns including:
- Atomic Saves: Applications like TextEdit create temporary files and rename them, generating multiple events
- Metadata Changes: Tracks extended attributes, Finder info, inode metadata changes
- File Type Detection: Distinguishes between files, directories, symlinks, hardlinks
- Event Coalescing: Latency parameter allows grouping rapid successive changes
- Minimum Platform: macOS 14.0+ (Swift 6 concurrency requirement)
- Swift Version: 6.0+
- Package Type: Library with CLI tool
- Dependencies: swift-collections (for internal use)
- Targets:
AsyncFileMonitor- Main library targetwatch- CLI tool executable (internal, not exposed as product)AsyncFileMonitorTests- Test suite
The watch CLI tool (Sources/watch/main.swift) provides a practical demonstration of AsyncFileMonitor usage:
- Real-time Monitoring: Uses AsyncFileMonitor.monitor() to watch file changes
- Logger Integration: Enables AsyncFileMonitorLogger for debugging FSEvent activity
- Multi-path Support: Can monitor multiple directories simultaneously
- User-friendly Output: Shows timestamps, file paths, change types, and event IDs
- Path Validation: Verifies paths exist before starting monitoring
- Graceful Handling: Provides clear usage messages and error handling