Skip to content

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

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

Merged
merged 2 commits into from
Dec 31, 2021
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
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