Skip to content
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

[dotnet-watch] Simplify file watching #44603

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading