Skip to content
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

Backport changes from main #23229

Merged
merged 2 commits into from
Jan 3, 2022
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>
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ExternalAccess.Watch.Api;
using Microsoft.Extensions.HotReload;
using Microsoft.Extensions.Tools.Internal;

namespace Microsoft.DotNet.Watcher.Tools
{
internal class BlazorWebAssemblyDeltaApplier : IDeltaApplier
{
private static Task<ImmutableArray<string>>? _cachedCapabilties;
private static readonly ImmutableArray<string> _baselineCapabilities = ImmutableArray.Create<string>("Baseline");
private readonly IReporter _reporter;
private int _sequenceId;

Expand All @@ -48,7 +47,7 @@ async Task<ImmutableArray<string>> GetApplyUpdateCapabilitiesCoreAsync()
{
if (context.BrowserRefreshServer is null)
{
return ImmutableArray<string>.Empty;
return _baselineCapabilities;
}

await context.BrowserRefreshServer.WaitForClientConnectionAsync(cancellationToken);
Expand All @@ -58,14 +57,14 @@ async Task<ImmutableArray<string>> GetApplyUpdateCapabilitiesCoreAsync()
var buffer = ArrayPool<byte>.Shared.Rent(32 * 1024);
try
{
// We'll query the browser and ask it send capabilities. If the browser does not respond in 10s, we'll assume something is amiss and return
// no capabilities. This should give you baseline hot reload capabilties.
// We'll query the browser and ask it send capabilities. If the browser does not respond in a short duration, we'll assume something is amiss and return
// baseline capabilities.
var response = await context.BrowserRefreshServer.ReceiveAsync(buffer, cancellationToken)
.AsTask()
.WaitAsync(TimeSpan.FromSeconds(10), cancellationToken);
.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken);
if (!response.HasValue || !response.Value.EndOfMessage || response.Value.MessageType != WebSocketMessageType.Text)
{
return ImmutableArray<string>.Empty;
return _baselineCapabilities;
}

var values = Encoding.UTF8.GetString(buffer.AsSpan(0, response.Value.Count));
Expand All @@ -83,7 +82,7 @@ async Task<ImmutableArray<string>> GetApplyUpdateCapabilitiesCoreAsync()
ArrayPool<byte>.Shared.Return(buffer);
}

return ImmutableArray<string>.Empty;
return _baselineCapabilities;
}
}

Expand Down
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