fix(locations): register watcher on location add, canonicalize paths#3043
fix(locations): register watcher on location add, canonicalize paths#3043slvnlrt wants to merge 2 commits intospacedriveapp:mainfrom
Conversation
Two upstream bugs fixed: 1. LocationManager::add_location() stored paths as-is without canonicalization. Relative paths (e.g. from cwd-dependent contexts) broke the watcher, volume manager, and indexer pipelines. Now calls tokio::fs::canonicalize() on local physical paths before storing, with UNC prefix stripping on Windows. 2. LocationAddAction::execute() never registered new locations with the FsWatcherService. The watcher only discovered locations at startup via load_library_locations(). Any location added at runtime had no filesystem monitoring — creates, deletes, and renames went undetected. Now calls fs_watcher.watch_location() after successful creation.
WalkthroughAdds async normalization of local physical SD paths (including Windows \?\ prefix stripping) when adding locations, and registers newly added locations with the filesystem watcher; watcher registration failures are logged and do not change the action result. Changes
Sequence DiagramsequenceDiagram actor User participant Action as LocationAddAction participant Manager as LocationManager participant FS as Filesystem participant Watcher as FileSystemWatcher User->>Action: execute(add location) Action->>Manager: add_location(sd_path) alt sd_path is local physical Manager->>FS: tokio::fs::canonicalize(path) FS-->>Manager: canonical_path / error opt canonical_path (Windows) Manager->>Manager: strip "\\?\" / "\\?\\UNC\\" prefixes end end Manager-->>Action: location_id opt action has local path Action->>FS: tokio::fs::canonicalize(local_path) FS-->>Action: canonical_path / error opt canonical_path (Windows) Action->>Action: strip "\\?\" / "\\?\\UNC\\" prefixes end Action->>Watcher: watch_location(LocationMeta{id, library_id, root_path, rule_toggles}) alt watch success Watcher-->>Action: ok else watch error Watcher-->>Action: error (logged, non-fatal) end end Action-->>User: LocationAddOutput Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@core/src/location/manager.rs`: - Around line 61-71: The Windows UNC handling in the cfg(windows) block around the canonical variable must recognize the UNC form "\\?\UNC\server\share\..." and reconstruct it as a valid UNC path "\\server\share\..." before falling back to stripping a local-drive "\\?\" prefix; update the logic in the canonical normalization (the block that currently uses canonical.to_string_lossy() and strip_prefix(r"\\?\")) to first check strip_prefix(r"\\?\UNC\") and if present create a PathBuf from "\\" + stripped, otherwise strip r"\\?\" for local drives, else return canonical; mirror the approach used in core/src/volume/fs/refs.rs to ensure starts_with("\\\\") checks in classification.rs work correctly. In `@core/src/ops/locations/add/action.rs`: - Around line 108-126: The watcher is being registered with a canonicalized path that still may contain the Windows extended prefix, causing mismatches with the persisted path; update the block that builds root_path (inside the if let Some(local_path) ... and before creating LocationMeta and calling fs_watcher.watch_location) to apply the same Windows path normalization used by location_manager.add_location() — i.e., strip the Windows "\\?\" extended prefix (or otherwise normalize to the exact form saved to the DB) after tokio::fs::canonicalize returns and before constructing LocationMeta, so that LocationMeta.root_path matches the normalized DB path used by the persistent handler. ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 386339bf-614c-4c9f-8672-fbf9d51dbaa0
📒 Files selected for processing (2)
core/src/location/manager.rscore/src/ops/locations/add/action.rs
canonicalize() on Windows can produce \?\UNC\server\share\... for network paths. The previous strip_prefix(r"\?\") would produce UNC\server\share\... which is invalid. Now handles both forms: - \?\UNC\server\share\... → \server\share\... (network UNC) - \?\C:\... → C:\... (local drive) Applied in both manager.rs (add_location) and add/action.rs (watcher registration) to ensure consistency. Same normalization as volume/fs/refs.rs:contains_path(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
Locations added at runtime are never registered with the
FsWatcherService. The watcher only discovers locations at startup viaload_library_locations(). This means no filesystem events (creates, deletes, renames) are detected for any location added through the UI until the app is restarted.Additionally,
LocationManager::add_location()stores paths without canonicalization, so relative paths (from cwd-dependent contexts) break the watcher, volume manager, and indexer.Changes
1. Register watcher on location add (
locations/add/action.rs)After
LocationManager::add_location()succeeds, callfs_watcher.watch_location(meta)to start OS-level filesystem monitoring immediately. This mirrors whatload_library_locations()does at startup.2. Canonicalize paths before storing (
location/manager.rs)Call
tokio::fs::canonicalize()on local physical paths before storing in DB...components\?\UNC prefix on Windows (same pattern asvolume/manager.rs)Test plan
Watching location <id> at <path>after addingRelated