Skip to content

Commit

Permalink
Use project path instead of working directory to resolve launchSettin…
Browse files Browse the repository at this point in the history
…gs.json (#23184)

* Use project path instead of working directory to resolve launchSettings.json

Fixes dotnet/aspnetcore#35393

* Update DotNetWatcherTests.cs
  • Loading branch information
pranavkm authored Dec 31, 2021
1 parent 00b7afe commit 023b715
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Assets/TestProjects/WatchAppWithLaunchSettings/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Console.WriteLine("Started");
Console.WriteLine($"Environment: {Environment.GetEnvironmentVariable("EnvironmentFromProfile")}");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"app": {
"commandName": "Project",
"environmentVariables": {
"EnvironmentFromProfile": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion src/BuiltInTools/dotnet-watch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private async Task<int> 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
{
Expand Down
39 changes: 39 additions & 0 deletions src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

[CoreMSBuildOnlyFact]
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));
}

[CoreMSBuildOnlyFact]
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));
}
}
}
4 changes: 3 additions & 1 deletion src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public WatchableApp(string sourceDirectory, ITestOutputHelper logger)

public string SourceDirectory { get; }

public string WorkingDirectory { get; set; }

public Task HasRestarted()
=> HasRestarted(DefaultMessageTimeOut);

Expand Down Expand Up @@ -76,7 +78,7 @@ public void Start(IEnumerable<string> 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");
Expand Down

0 comments on commit 023b715

Please sign in to comment.