Skip to content

Commit

Permalink
Simplify file watching (#44603)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Nov 10, 2024
1 parent 98d9d22 commit b60190c
Show file tree
Hide file tree
Showing 23 changed files with 423 additions and 439 deletions.
12 changes: 8 additions & 4 deletions src/BuiltInTools/dotnet-watch/DotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke

using var currentRunCancellationSource = new CancellationTokenSource();
using var combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(shutdownCancellationToken, currentRunCancellationSource.Token);
using var fileSetWatcher = new FileWatcher(evaluationResult.Files, Context.Reporter);
using var fileSetWatcher = new FileWatcher(Context.Reporter);

fileSetWatcher.WatchContainingDirectories(evaluationResult.Files.Keys);

var processTask = ProcessRunner.RunAsync(processSpec, Context.Reporter, isUserApplication: true, launchResult: null, combinedCancellationSource.Token);

Expand All @@ -89,7 +91,7 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke

while (true)
{
fileSetTask = fileSetWatcher.GetChangedFileAsync(startedWatching: null, combinedCancellationSource.Token);
fileSetTask = fileSetWatcher.WaitForFileChangeAsync(evaluationResult.Files, startedWatching: null, combinedCancellationSource.Token);
finishedTask = await Task.WhenAny(processTask, fileSetTask, cancelledTaskSource.Task);

if (staticFileHandler != null && finishedTask == fileSetTask && fileSetTask.Result.HasValue)
Expand Down Expand Up @@ -119,9 +121,11 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke
{
// Process exited. Redo evalulation
buildEvaluator.RequiresRevaluation = true;

// Now wait for a file to change before restarting process
changedFile = await fileSetWatcher.GetChangedFileAsync(
() => Context.Reporter.Report(MessageDescriptor.WaitingForFileChangeBeforeRestarting),
changedFile = await fileSetWatcher.WaitForFileChangeAsync(
evaluationResult.Files,
startedWatching: () => Context.Reporter.Report(MessageDescriptor.WaitingForFileChangeBeforeRestarting),
shutdownCancellationToken);
}
else
Expand Down
6 changes: 4 additions & 2 deletions src/BuiltInTools/dotnet-watch/EnvironmentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal sealed record EnvironmentOptions(
bool SuppressLaunchBrowser = false,
bool SuppressBrowserRefresh = false,
bool SuppressEmojis = false,
TestFlags TestFlags = TestFlags.None)
TestFlags TestFlags = TestFlags.None,
string TestOutput = "")
{
public static EnvironmentOptions FromEnvironment() => new
(
Expand All @@ -46,7 +47,8 @@ internal sealed record EnvironmentOptions(
SuppressLaunchBrowser: EnvironmentVariables.SuppressLaunchBrowser,
SuppressBrowserRefresh: EnvironmentVariables.SuppressBrowserRefresh,
SuppressEmojis: EnvironmentVariables.SuppressEmojis,
TestFlags: EnvironmentVariables.TestFlags
TestFlags: EnvironmentVariables.TestFlags,
TestOutput: EnvironmentVariables.TestOutputDir
);

public bool RunningAsTest { get => (TestFlags & TestFlags.RunningAsTest) != TestFlags.None; }
Expand Down
1 change: 1 addition & 0 deletions src/BuiltInTools/dotnet-watch/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static partial class Names
public static bool SuppressBrowserRefresh => ReadBool("DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH");

public static TestFlags TestFlags => Environment.GetEnvironmentVariable("__DOTNET_WATCH_TEST_FLAGS") is { } value ? Enum.Parse<TestFlags>(value) : TestFlags.None;
public static string TestOutputDir => Environment.GetEnvironmentVariable("__DOTNET_WATCH_TEST_OUTPUT_DIR") ?? "";

public static string? AutoReloadWSHostName => Environment.GetEnvironmentVariable("DOTNET_WATCH_AUTO_RELOAD_WS_HOSTNAME");
public static string? BrowserPath => Environment.GetEnvironmentVariable("DOTNET_WATCH_BROWSER_PATH");
Expand Down
7 changes: 5 additions & 2 deletions src/BuiltInTools/dotnet-watch/Filters/BuildEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ private async ValueTask<EvaluationResult> CreateEvaluationResult(CancellationTok
return result;
}

context.Reporter.Warn("Fix the error to continue or press Ctrl+C to exit.");
await FileWatcher.WaitForFileChangeAsync(rootProjectFileSetFactory.RootProjectFile, context.Reporter, cancellationToken);
await FileWatcher.WaitForFileChangeAsync(
rootProjectFileSetFactory.RootProjectFile,
context.Reporter,
startedWatching: () => context.Reporter.Warn("Fix the error to continue or press Ctrl+C to exit."),
cancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ async Task<ImmutableArray<string>> ConnectAsync()
Reporter.Verbose($"Capabilities: '{capabilities}'");
return capabilities.Split(' ').ToImmutableArray();
}
catch (EndOfStreamException)
{
// process terminated before capabilities sent:
return [];
}
catch (Exception e) when (e is not OperationCanceledException)
{
// pipe might throw another exception when forcibly closed on process termination:
Expand Down
Loading

0 comments on commit b60190c

Please sign in to comment.