From c0d099bd093ab84c14668a9732310925bb8e3225 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 27 Dec 2021 15:10:09 -0800 Subject: [PATCH 1/2] Use project path instead of working directory to resolve launchSettings.json Fixes https://github.com/dotnet/aspnetcore/issues/35393 --- .../WatchAppWithLaunchSettings/Program.cs | 2 + .../Properties/launchSettings.json | 10 +++++ .../WatchAppWithLaunchSettings.csproj | 10 +++++ src/BuiltInTools/dotnet-watch/Program.cs | 2 +- .../dotnet-watch.Tests/DotNetWatcherTests.cs | 39 +++++++++++++++++++ .../Utilities/WatchableApp.cs | 4 +- 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/Assets/TestProjects/WatchAppWithLaunchSettings/Program.cs create mode 100644 src/Assets/TestProjects/WatchAppWithLaunchSettings/Properties/launchSettings.json create mode 100644 src/Assets/TestProjects/WatchAppWithLaunchSettings/WatchAppWithLaunchSettings.csproj diff --git a/src/Assets/TestProjects/WatchAppWithLaunchSettings/Program.cs b/src/Assets/TestProjects/WatchAppWithLaunchSettings/Program.cs new file mode 100644 index 000000000000..143f5183dfec --- /dev/null +++ b/src/Assets/TestProjects/WatchAppWithLaunchSettings/Program.cs @@ -0,0 +1,2 @@ +Console.WriteLine("Started"); +Console.WriteLine($"Environment: {Environment.GetEnvironmentVariable("EnvironmentFromProfile")}"); diff --git a/src/Assets/TestProjects/WatchAppWithLaunchSettings/Properties/launchSettings.json b/src/Assets/TestProjects/WatchAppWithLaunchSettings/Properties/launchSettings.json new file mode 100644 index 000000000000..f8b1abd49b2b --- /dev/null +++ b/src/Assets/TestProjects/WatchAppWithLaunchSettings/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "app": { + "commandName": "Project", + "environmentVariables": { + "EnvironmentFromProfile": "Development" + } + } + } + } diff --git a/src/Assets/TestProjects/WatchAppWithLaunchSettings/WatchAppWithLaunchSettings.csproj b/src/Assets/TestProjects/WatchAppWithLaunchSettings/WatchAppWithLaunchSettings.csproj new file mode 100644 index 000000000000..74abf5c97664 --- /dev/null +++ b/src/Assets/TestProjects/WatchAppWithLaunchSettings/WatchAppWithLaunchSettings.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/src/BuiltInTools/dotnet-watch/Program.cs b/src/BuiltInTools/dotnet-watch/Program.cs index a60b3de5d031..246f07e76291 100644 --- a/src/BuiltInTools/dotnet-watch/Program.cs +++ b/src/BuiltInTools/dotnet-watch/Program.cs @@ -278,7 +278,7 @@ private async Task MainInternalAsync(IReporter reporter, CommandLineOptions _reporter.Output("Polling file watcher is enabled"); } - var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(_workingDirectory, reporter) ?? new(); + var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(processInfo.WorkingDirectory, reporter) ?? new(); var context = new DotNetWatchContext { diff --git a/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs b/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs index fc71b74b3f3b..0e35cae38d70 100644 --- a/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs +++ b/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs @@ -129,5 +129,44 @@ public async Task RunsWithRestoreIfCsprojChanges() message = await app.Process.GetOutputLineStartsWithAsync(messagePrefix, TimeSpan.FromMinutes(2)); Assert.Equal(messagePrefix + " --no-restore -- wait", message.Trim()); } + + [Fact] + public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings() + { + var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings") + .WithSource() + .Path; + + using var app = new WatchableApp(testAsset, _logger); + + app.DotnetWatchArgs.Add("--verbose"); + + await app.StartWatcherAsync(); + + await app.Process.GetOutputLineAsync("Environment: Development", TimeSpan.FromSeconds(10)); + } + + [Fact] + public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings_WhenUsingProjectOption() + { + var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings") + .WithSource() + .Path; + + var directoryInfo = new DirectoryInfo(testAsset); + using var app = new WatchableApp(testAsset, _logger) + { + // Configure the working directory to be one level above the test app directory. + WorkingDirectory = Path.GetFullPath(directoryInfo.Parent.FullName), + }; + + app.DotnetWatchArgs.Add("--verbose"); + app.DotnetWatchArgs.Add("--project"); + app.DotnetWatchArgs.Add(Path.Combine(directoryInfo.Name, "WatchAppWithLaunchSettings.csproj")); + + await app.StartWatcherAsync(); + + await app.Process.GetOutputLineAsync("Environment: Development", TimeSpan.FromSeconds(10)); + } } } diff --git a/src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs b/src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs index d8a036559988..415f9b802219 100644 --- a/src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs +++ b/src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs @@ -38,6 +38,8 @@ public WatchableApp(string sourceDirectory, ITestOutputHelper logger) public string SourceDirectory { get; } + public string WorkingDirectory { get; set; } + public Task HasRestarted() => HasRestarted(DefaultMessageTimeOut); @@ -76,7 +78,7 @@ public void Start(IEnumerable arguments, [CallerMemberName] string name var commandSpec = new DotnetCommand(_logger, args.ToArray()) { - WorkingDirectory = SourceDirectory, + WorkingDirectory = WorkingDirectory ?? SourceDirectory, }; commandSpec.WithEnvironmentVariable("DOTNET_USE_POLLING_FILE_WATCHER", "true"); commandSpec.WithEnvironmentVariable("__DOTNET_WATCH_RUNNING_AS_TEST", "true"); From 66eb5f5a054c04407b42ed53679fa89a7985a66e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 31 Dec 2021 05:59:59 -0800 Subject: [PATCH 2/2] Update DotNetWatcherTests.cs --- src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs b/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs index 0e35cae38d70..e1eb948f67e8 100644 --- a/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs +++ b/src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs @@ -130,7 +130,7 @@ public async Task RunsWithRestoreIfCsprojChanges() Assert.Equal(messagePrefix + " --no-restore -- wait", message.Trim()); } - [Fact] + [CoreMSBuildOnlyFact] public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings() { var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings") @@ -146,7 +146,7 @@ public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings() await app.Process.GetOutputLineAsync("Environment: Development", TimeSpan.FromSeconds(10)); } - [Fact] + [CoreMSBuildOnlyFact] public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings_WhenUsingProjectOption() { var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings")