diff --git a/build/repo.props b/build/repo.props index 015f722dd9..9569f0ba08 100644 --- a/build/repo.props +++ b/build/repo.props @@ -2,9 +2,6 @@ - - - diff --git a/src/dotnet-watch/CommandLineOptions.cs b/src/dotnet-watch/CommandLineOptions.cs index e6e23890d5..5cbaab33e9 100644 --- a/src/dotnet-watch/CommandLineOptions.cs +++ b/src/dotnet-watch/CommandLineOptions.cs @@ -53,6 +53,10 @@ Environment variables: DOTNET_WATCH dotnet-watch sets this variable to '1' on all child processes launched. + DOTNET_WATCH_ITERATION + dotnet-watch sets this variable to '1' and increments by one each time + a file is changed and the command is restarted. + Remarks: The special option '--' is used to delimit the end of the options and the beginning of arguments that will be passed to the child dotnet process. diff --git a/src/dotnet-watch/DotNetWatcher.cs b/src/dotnet-watch/DotNetWatcher.cs index 476acfcb43..8431615c1e 100644 --- a/src/dotnet-watch/DotNetWatcher.cs +++ b/src/dotnet-watch/DotNetWatcher.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Globalization; using System.Threading; using System.Threading.Tasks; using Microsoft.DotNet.Watcher.Internal; @@ -32,8 +33,13 @@ namespace Microsoft.DotNet.Watcher cancellationToken.Register(state => ((TaskCompletionSource) state).TrySetResult(null), cancelledTaskSource); + var iteration = 1; + while (true) { + processSpec.EnvironmentVariables["DOTNET_WATCH_ITERATION"] = iteration.ToString(CultureInfo.InvariantCulture); + iteration++; + var fileSet = await fileSetFactory.CreateAsync(cancellationToken); if (fileSet == null) diff --git a/test/dotnet-watch.FunctionalTests/DotNetWatcherTests.cs b/test/dotnet-watch.FunctionalTests/DotNetWatcherTests.cs index d0dc735247..d1dd022a15 100644 --- a/test/dotnet-watch.FunctionalTests/DotNetWatcherTests.cs +++ b/test/dotnet-watch.FunctionalTests/DotNetWatcherTests.cs @@ -3,6 +3,8 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Globalization; using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; @@ -30,6 +32,23 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests Assert.Equal("1", envValue); } + [Fact] + public async Task RunsWithIterationEnvVariable() + { + await _app.StartWatcherAsync(); + var source = Path.Combine(_app.SourceDirectory, "Program.cs"); + const string messagePrefix = "DOTNET_WATCH_ITERATION = "; + for (var i = 1; i <= 4; i++) + { + var message = await _app.Process.GetOutputLineStartsWithAsync(messagePrefix, TimeSpan.FromMinutes(2)); + var count = int.Parse(message.Substring(messagePrefix.Length), CultureInfo.InvariantCulture); + Assert.Equal(i, count); + + File.SetLastWriteTime(source, DateTime.Now); + await _app.HasRestarted(); + } + } + public void Dispose() { _app.Dispose(); diff --git a/test/dotnet-watch.FunctionalTests/TestProjects/KitchenSink/Program.cs b/test/dotnet-watch.FunctionalTests/TestProjects/KitchenSink/Program.cs index 5251cdc1e0..f38dc8231b 100644 --- a/test/dotnet-watch.FunctionalTests/TestProjects/KitchenSink/Program.cs +++ b/test/dotnet-watch.FunctionalTests/TestProjects/KitchenSink/Program.cs @@ -13,6 +13,7 @@ namespace KitchenSink Console.WriteLine("Started"); Console.WriteLine("PID = " + Process.GetCurrentProcess().Id); Console.WriteLine("DOTNET_WATCH = " + Environment.GetEnvironmentVariable("DOTNET_WATCH")); + Console.WriteLine("DOTNET_WATCH_ITERATION = " + Environment.GetEnvironmentVariable("DOTNET_WATCH_ITERATION")); } } }