Skip to content

File watcher: ignore directories, allow watching non-recursively #49150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025

Conversation

tmat
Copy link
Member

@tmat tmat commented May 26, 2025

Preparing directory watcher for watching individual files in some cases when we don't want to watch the entire directory tree (e.g. targets and props files outside of the project dir).

Fixes #49307

@github-actions github-actions bot added the Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch label May 26, 2025
Copy link
Contributor

Thanks for your PR, @@tmat.
To learn about the PR process and branching schedule of this repo, please take a look at the SDK PR Guide.

@tmat
Copy link
Member Author

tmat commented Jun 5, 2025

@phil-allen-msft @DustinCampbell ptal

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extends the file-watching subsystem to distinguish between recursive and non-recursive watching and to ignore directory events, while centralizing path adjustments in PathUtilities and updating corresponding tests.

  • Add an includeSubdirectories flag throughout the watcher factory, polling and event-based watchers.
  • Filter out directory change events in both polling and file‐system–based implementations.
  • Introduce PathUtilities.EnsureTrailingSlash and NormalizeDirectorySeparators.
  • Update tests to drive the new behavior and adjust assertion ordering.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/dotnet-watch.Tests/FileWatcherTests.cs Pass watchSubdirectories to tests and simplify assertion sorting
src/BuiltInTools/dotnet-watch/Utilities/PathUtilities.cs Add trailing-slash and separator-normalization helpers
src/BuiltInTools/dotnet-watch/Internal/FileWatcher/PollingDirectoryWatcher.cs Add _includeSubdirectories, rename and scope file dictionaries
src/BuiltInTools/dotnet-watch/Internal/FileWatcher/EventBasedDirectoryWatcher.cs Honor IncludeSubdirectories and ignore directory events
src/BuiltInTools/dotnet-watch/Internal/FileWatcher/IDirectoryWatcher.cs Switch to file-scoped namespace and add summary doc
src/BuiltInTools/dotnet-watch/Internal/FileWatcher/FileWatcherFactory.cs Overload CreateWatcher to accept includeSubdirectories
src/BuiltInTools/dotnet-watch/Internal/FileWatcher/FileWatcher.cs Split recursive vs. non-recursive watchers and update disposal
src/BuiltInTools/dotnet-watch/HotReloadDotNetWatcher.cs Propagate includeSubdirectories when watching containing dirs
src/BuiltInTools/dotnet-watch/DotNetWatcher.cs Propagate includeSubdirectories for root project directory watch
Comments suppressed due to low confidence (2)

src/BuiltInTools/dotnet-watch/Internal/FileWatcher/FileWatcher.cs:30

  • Dispose only iterates _directoryTreeWatchers; non-recursive watchers in _directoryWatchers remain undisposed and can leak events—include both collections in disposal.
foreach (var (_, watcher) in _directoryTreeWatchers)

src/BuiltInTools/dotnet-watch/Internal/FileWatcher/FileWatcher.cs:78

  • When registering a recursive watcher, you remove redundant recursive watchers but leave non-recursive watchers intact; also clear entries in _directoryWatchers under the new root to avoid duplicate event streams.
var watchersToRemove = _directoryTreeWatchers

@tmat tmat force-pushed the FileWatcher-Dirs branch from 4d1fb20 to 16bf014 Compare June 12, 2025 20:32
@tmat tmat merged commit 49dade0 into dotnet:main Jun 13, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking: dotnet-watch test failures in AutoRestartOnRudeEdit and IgnoredChange
2 participants