Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using Microsoft.DotNet.HotReload;
using Microsoft.VisualStudio.Debugger.Contracts.EditAndContinue;
Expand Down Expand Up @@ -72,16 +71,6 @@ internal static string GetInjectedAssemblyPath(string targetFramework, string as
public IDeltaApplier? DeltaApplier
=> _lazyDeltaApplier;

[MemberNotNull(nameof(_lazyDeltaApplier))]
private void RequireActiveSession()
{
if (!_sessionActive)
throw new InvalidOperationException($"Hot Reload session has not started");

if (_lazyDeltaApplier is null)
throw new InvalidOperationException();
}

public async Task ApplyChangesAsync(CancellationToken cancellationToken)
{
if (_sessionActive)
Expand Down Expand Up @@ -199,19 +188,29 @@ public async Task StartSessionAsync(CancellationToken cancellationToken)

public async Task StopSessionAsync(CancellationToken cancellationToken)
{
RequireActiveSession();

_sessionActive = false;
_lazyDeltaApplier.Dispose();
_lazyDeltaApplier = null;
if (_sessionActive && _lazyDeltaApplier is not null)
{
_sessionActive = false;
_lazyDeltaApplier.Dispose();
_lazyDeltaApplier = null;

await _hotReloadAgentManagerClient.Value.AgentTerminatedAsync(this, cancellationToken);
WriteToOutputWindow(Resources.HotReloadStopSession, default);
await _hotReloadAgentManagerClient.Value.AgentTerminatedAsync(this, cancellationToken);
WriteToOutputWindow(Resources.HotReloadStopSession, default);
}
}

public async ValueTask ApplyUpdatesAsync(ImmutableArray<ManagedHotReloadUpdate> updates, CancellationToken cancellationToken)
{
RequireActiveSession();
// A stricter check for session active could be done here, like raise an exception when not active session or no delta applier
// But sometimes debugger would call ApplyUpdatesAsync even when there is not active session
// e.g. when user restarts on rude edits
// We need to talk to debugger team to see if we can avoid such calls in the future
// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2581474
if (_sessionActive is false || _lazyDeltaApplier is null)
{
DebugTrace($"{nameof(ApplyUpdatesAsync)} called but the session is not active.");
return;
}

try
{
Expand Down Expand Up @@ -248,11 +247,15 @@ private bool LogAndPropagate(Exception e, CancellationToken cancellationToken)
return false;
}

public ValueTask<ImmutableArray<string>> GetCapabilitiesAsync(CancellationToken cancellationToken)
public async ValueTask<ImmutableArray<string>> GetCapabilitiesAsync(CancellationToken cancellationToken)
{
RequireActiveSession();
// Delegate to the delta applier for the session
if (_lazyDeltaApplier is not null)
{
return await _lazyDeltaApplier.GetCapabilitiesAsync(cancellationToken);
}

return _lazyDeltaApplier.GetCapabilitiesAsync(cancellationToken);
return [];
}

public ValueTask ReportDiagnosticsAsync(ImmutableArray<ManagedHotReloadDiagnostic> diagnostics, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public async Task StopSessionAsync_WhenSessionNotActive_DoesNotCallAgentTerminat
// Session is not started/active

// Act
await Assert.ThrowsAsync<InvalidOperationException>(() => session.StopSessionAsync(CancellationToken.None));
await session.StopSessionAsync(CancellationToken.None);

// Assert
hotReloadAgentManagerClient.Verify(
Expand Down Expand Up @@ -381,7 +381,7 @@ public async Task ApplyUpdatesAsync_WhenSessionNotActive_DoesNotCallDeltaApplier
var updates = ImmutableArray.Create<ManagedHotReloadUpdate>();

// Act
await Assert.ThrowsAsync<InvalidOperationException>(() => session.ApplyUpdatesAsync(updates, CancellationToken.None).AsTask());
await session.ApplyUpdatesAsync(updates, CancellationToken.None);

// Assert
deltaApplier.Verify(
Expand Down