Skip to content

Commit 921ae7e

Browse files
committed
Use project path instead of working directory to resolve launchSettings.json
Fixes dotnet/aspnetcore#35393
1 parent d0f9531 commit 921ae7e

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Console.WriteLine("Started");
2+
Console.WriteLine($"Environment: {Environment.GetEnvironmentVariable("EnvironmentFromProfile")}");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"app": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"EnvironmentFromProfile": "Development"
7+
}
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/BuiltInTools/dotnet-watch/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private async Task<int> MainInternalAsync(IReporter reporter, CommandLineOptions
278278
_reporter.Output("Polling file watcher is enabled");
279279
}
280280

281-
var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(_workingDirectory, reporter) ?? new();
281+
var defaultProfile = LaunchSettingsProfile.ReadDefaultProfile(processInfo.WorkingDirectory, reporter) ?? new();
282282

283283
var context = new DotNetWatchContext
284284
{

src/Tests/dotnet-watch.Tests/DotNetWatcherTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,46 @@ public async Task RunsWithRestoreIfCsprojChanges()
129129
message = await app.Process.GetOutputLineStartsWithAsync(messagePrefix, TimeSpan.FromMinutes(2));
130130
Assert.Equal(messagePrefix + " --no-restore -- wait", message.Trim());
131131
}
132+
133+
[Fact]
134+
public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings()
135+
{
136+
var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings")
137+
.WithSource()
138+
.Path;
139+
140+
using var app = new WatchableApp(testAsset, _logger);
141+
142+
app.DotnetWatchArgs.Add("--verbose");
143+
144+
await app.StartWatcherAsync();
145+
146+
await app.Process.GetOutputLineAsync("Environment: Development", TimeSpan.FromSeconds(10));
147+
}
148+
149+
[Fact]
150+
public async Task Run_WithHotReloadEnabled_ReadsLaunchSettings_WhenUsingProjectOption()
151+
{
152+
var testAsset = _testAssetsManager.CopyTestAsset("WatchAppWithLaunchSettings")
153+
.WithSource()
154+
.Path;
155+
156+
var directoryInfo = new DirectoryInfo(testAsset);
157+
var appName = Path.Combine(directoryInfo.Name, "WatchAppWithLaunchSettings.csproj");
158+
159+
using var app = new WatchableApp(testAsset, _logger)
160+
{
161+
// Configure the working directory to be one level above the test app directory.
162+
WorkingDirectory = Path.GetFullPath(directoryInfo.Parent.FullName),
163+
};
164+
165+
app.DotnetWatchArgs.Add("--verbose");
166+
app.DotnetWatchArgs.Add("--project");
167+
app.DotnetWatchArgs.Add(appName);
168+
169+
await app.StartWatcherAsync();
170+
171+
await app.Process.GetOutputLineAsync("Environment: Development", TimeSpan.FromSeconds(10));
172+
}
132173
}
133174
}

src/Tests/dotnet-watch.Tests/Utilities/WatchableApp.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public WatchableApp(string sourceDirectory, ITestOutputHelper logger)
3838

3939
public string SourceDirectory { get; }
4040

41+
public string WorkingDirectory { get; set; }
42+
4143
public Task HasRestarted()
4244
=> HasRestarted(DefaultMessageTimeOut);
4345

@@ -76,7 +78,7 @@ public void Start(IEnumerable<string> arguments, [CallerMemberName] string name
7678

7779
var commandSpec = new DotnetCommand(_logger, args.ToArray())
7880
{
79-
WorkingDirectory = SourceDirectory,
81+
WorkingDirectory = WorkingDirectory ?? SourceDirectory,
8082
};
8183
commandSpec.WithEnvironmentVariable("DOTNET_USE_POLLING_FILE_WATCHER", "true");
8284
commandSpec.WithEnvironmentVariable("__DOTNET_WATCH_RUNNING_AS_TEST", "true");

0 commit comments

Comments
 (0)