Skip to content

fix(locations): register watcher on location add, canonicalize paths#3043

Merged
jamiepine merged 4 commits intospacedriveapp:mainfrom
slvnlrt:fix-location-watcher
Mar 26, 2026
Merged

fix(locations): register watcher on location add, canonicalize paths#3043
jamiepine merged 4 commits intospacedriveapp:mainfrom
slvnlrt:fix-location-watcher

Conversation

@slvnlrt
Copy link
Copy Markdown
Contributor

@slvnlrt slvnlrt commented Mar 24, 2026

Summary

Locations added at runtime are never registered with the FsWatcherService. The watcher only discovers locations at startup via load_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, call fs_watcher.watch_location(meta) to start OS-level filesystem monitoring immediately. This mirrors what load_library_locations() does at startup.

  • Only registers local physical paths (remote/cloud skipped)
  • Graceful fallback if watcher not available (warns, doesn't fail)
  • Uses canonical path to match DB storage

2. Canonicalize paths before storing (location/manager.rs)

Call tokio::fs::canonicalize() on local physical paths before storing in DB.

  • Converts relative paths to absolute
  • Resolves symlinks and .. components
  • Strips \?\ UNC prefix on Windows (same pattern as volume/manager.rs)
  • Only for local device paths — remote paths pass through unchanged

Test plan

  • Add a location via the UI, then copy/delete/rename a file inside it — changes should appear in the UI immediately without restart
  • Verify the watcher log shows Watching location <id> at <path> after adding
  • macOS/Linux: verify no regression (location add + file operations)
  • Remote locations (if testable): verify no canonicalization attempted on remote paths

Related

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.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: af4da2c2-6993-476b-baef-8ceec3638726

📥 Commits

Reviewing files that changed from the base of the PR and between 1d7097d and 7b867ef.

📒 Files selected for processing (1)
  • core/src/common/utils.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/src/common/utils.rs

Walkthrough

Canonicalizes local physical SD paths asynchronously when adding locations (Windows extended-prefix stripped) and registers newly added locations with the filesystem watcher; watcher (un)watch errors are logged and do not change action results.

Changes

Cohort / File(s) Summary
Path canonicalization
core/src/location/manager.rs
When sd_path.is_local() and variant is SdPath::Physical, asynchronously canonicalizes the filesystem path; canonicalization failures map to LocationError::InvalidPath. On Windows the \\?\ / \\?\UNC\... prefixes are stripped from the canonical path before downstream use. Non-local sd_path values unchanged.
Filesystem watcher registration (add)
core/src/ops/locations/add/action.rs
After LocationManager::add_location(...) returns, derives a local path, canonicalizes it (falls back to original on failure), applies Windows prefix normalization, builds LocationMeta (id, library_id, root_path, default rule_toggles) and calls fs_watcher.watch_location(meta).await. Watcher failures are logged with tracing::warn! and do not affect the returned LocationAddOutput.
Filesystem watcher unregistration (remove)
core/src/ops/locations/remove/action.rs
After removing a location, attempts to unwatch the location_id via the filesystem watcher; unwatch failures are logged with tracing::warn! and do not alter the LocationRemoveOutput.
Windows path normalization util
core/src/common/utils.rs
Added exported pub fn strip_windows_extended_prefix(PathBuf) -> PathBuf. On Windows removes \\?\/\\?\UNC\... canonicalize-style prefixes; on non-Windows it is a no-op returning the input.
Refs containment normalization
core/src/volume/fs/refs.rs
Replaced inline Windows-specific prefix handling with call to crate::common::utils::strip_windows_extended_prefix(path.to_path_buf()) before containment checks against volume.mount_point(s).

Sequence Diagram

sequenceDiagram
    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_windows_extended_prefix(canonical_path)
        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_windows_extended_prefix(canonical_path)
        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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hopped the paths to make them neat,

Stripped the quirks where Windows and canon meet,
A watcher listens, gentle and bright,
Roots settle in, all tidy tonight,
I twitch my nose—code snug and light.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The pull request description is comprehensive and well-structured, covering the problem, changes, test plan, and related PRs, but does not follow the required template format. Add 'Closes #(issue)' section at the end to follow the repository's standard PR description template.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes: registering the watcher on location add and canonicalizing paths before storage.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@slvnlrt slvnlrt marked this pull request as ready for review March 24, 2026 16:38
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4d87617 and 2e778e9.

📒 Files selected for processing (2)
  • core/src/location/manager.rs
  • core/src/ops/locations/add/action.rs

Comment thread core/src/location/manager.rs Outdated
Comment thread core/src/ops/locations/add/action.rs
slvnlrt and others added 2 commits March 24, 2026 22:07
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>
- Extract duplicated Windows extended path normalization into shared
  `common::utils::strip_windows_extended_prefix()` helper (was copy-pasted
  in location/manager.rs, locations/add/action.rs, volume/fs/refs.rs)
- Add `unwatch_location()` call in LocationRemoveAction to stop the
  filesystem watcher when a location is deleted (symmetric with the
  `watch_location()` added in LocationAddAction)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
core/src/ops/locations/remove/action.rs (1)

46-46: Prefer rationale-focused inline comments (or remove them).

Line 46 and Line 53 comments restate behavior; consider rewriting them to capture intent (why DB removal is authoritative and watcher cleanup is best-effort).

Suggested wording
-		// Remove the location from DB
+		// Keep deletion semantics authoritative in the DB; watcher cleanup is best-effort.

...
-		// Unwatch the location from the filesystem watcher
+		// Avoid turning watcher teardown failures into user-visible delete failures.

As per coding guidelines, "Inline comments should explain WHY decisions were made, not WHAT the code does, and should be kept to one sentence when possible."

Also applies to: 53-53

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/src/ops/locations/remove/action.rs` at line 46, Replace the inline
"what" comments in the remove location flow with one-sentence rationale comments
explaining why DB removal is authoritative and watcher cleanup is best-effort;
specifically update the comment on the DB removal near the
remove_location/remove action (the "Remove the location from DB" comment) and
the watcher cleanup comment (around watcher cleanup logic) to briefly state the
intent (e.g., that persistent state is the source of truth and watcher shutdowns
are attempted best-effort to avoid impacting correctness) in one sentence each.
🤖 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/common/utils.rs`:
- Around line 21-23: The branch that unconditionally strips the verbatim prefix
using s.strip_prefix(r"\\?\") should only apply to drive-letter paths; update
the condition around the existing s.strip_prefix usage so it additionally checks
the remainder begins with a drive-letter form (e.g. matches r"^[A-Za-z]:\\", or
equivalently check stripped.get(0..2) ends with ':' and stripped.get(2..3) ==
"\\"), and only then construct PathBuf::from(stripped); leave other verbatim
forms (like volume GUIDs) untouched so they fall through to the existing else
branch.

---

Nitpick comments:
In `@core/src/ops/locations/remove/action.rs`:
- Line 46: Replace the inline "what" comments in the remove location flow with
one-sentence rationale comments explaining why DB removal is authoritative and
watcher cleanup is best-effort; specifically update the comment on the DB
removal near the remove_location/remove action (the "Remove the location from
DB" comment) and the watcher cleanup comment (around watcher cleanup logic) to
briefly state the intent (e.g., that persistent state is the source of truth and
watcher shutdowns are attempted best-effort to avoid impacting correctness) in
one sentence each.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 16c71f8b-178a-4cb0-b59c-57d4afc45ec4

📥 Commits

Reviewing files that changed from the base of the PR and between 9004d86 and 1d7097d.

📒 Files selected for processing (5)
  • core/src/common/utils.rs
  • core/src/location/manager.rs
  • core/src/ops/locations/add/action.rs
  • core/src/ops/locations/remove/action.rs
  • core/src/volume/fs/refs.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • core/src/location/manager.rs
  • core/src/ops/locations/add/action.rs

Comment thread core/src/common/utils.rs
Comment on lines +21 to +23
} else if let Some(stripped) = s.strip_prefix(r"\\?\") {
std::path::PathBuf::from(stripped)
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Restrict \\?\ stripping to canonical drive-letter paths only.

On Line 21, the generic strip_prefix(r"\\?\") branch also rewrites non-drive verbatim paths (for example \\?\Volume{...}), which can produce invalid relative-like paths. Please gate this branch to drive-letter forms (C:\...) and leave other verbatim forms unchanged.

Proposed fix
-		} else if let Some(stripped) = s.strip_prefix(r"\\?\") {
-			std::path::PathBuf::from(stripped)
+		} else if let Some(stripped) = s.strip_prefix(r"\\?\") {
+			// Only normalize canonical local-drive form (e.g. C:\...)
+			if stripped.len() >= 3
+				&& stripped.as_bytes()[1] == b':'
+				&& stripped.as_bytes()[2] == b'\\'
+			{
+				std::path::PathBuf::from(stripped)
+			} else {
+				path
+			}
 		} else {
 			path
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else if let Some(stripped) = s.strip_prefix(r"\\?\") {
std::path::PathBuf::from(stripped)
} else {
} else if let Some(stripped) = s.strip_prefix(r"\\?\") {
// Only normalize canonical local-drive form (e.g. C:\...)
if stripped.len() >= 3
&& stripped.as_bytes()[1] == b':'
&& stripped.as_bytes()[2] == b'\\'
{
std::path::PathBuf::from(stripped)
} else {
path
}
} else {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/src/common/utils.rs` around lines 21 - 23, The branch that
unconditionally strips the verbatim prefix using s.strip_prefix(r"\\?\") should
only apply to drive-letter paths; update the condition around the existing
s.strip_prefix usage so it additionally checks the remainder begins with a
drive-letter form (e.g. matches r"^[A-Za-z]:\\", or equivalently check
stripped.get(0..2) ends with ':' and stripped.get(2..3) == "\\"), and only then
construct PathBuf::from(stripped); leave other verbatim forms (like volume
GUIDs) untouched so they fall through to the existing else branch.

Volume GUIDs (\?\Volume{...}\) and other verbatim forms are invalid
without the prefix. Only strip when followed by a drive letter (X:\).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jamiepine jamiepine merged commit 7b2ef81 into spacedriveapp:main Mar 26, 2026
1 check passed
@slvnlrt slvnlrt deleted the fix-location-watcher branch March 27, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants