Skip to content

Commit

Permalink
Fix marshalability of IWorkspaceProject.StartBatchAsync
Browse files Browse the repository at this point in the history
We have to make this return an interface marked with [RpcMarshalable],
and since that can't really use IAsyncDisposable to say when it's done,
I'm adding an explicit Apply method to make up for it.
  • Loading branch information
jasonmalinowski committed Feb 25, 2023
1 parent 3aa2c51 commit 18feba0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,32 @@ public Task SetProjectHasAllInformationAsync(bool hasAllInformation, Cancellatio
return Task.CompletedTask;
}

public Task<IAsyncDisposable> StartBatchAsync(CancellationToken cancellationToken)
public Task<IWorkspaceProjectBatch> StartBatchAsync(CancellationToken cancellationToken)
{
return Task.FromResult(_project.CreateBatchScope());
return Task.FromResult<IWorkspaceProjectBatch>(new WorkspaceProjectBatch(_project.CreateBatchScope()));
}

private class WorkspaceProjectBatch : IWorkspaceProjectBatch
{
private IAsyncDisposable? _batch;

public WorkspaceProjectBatch(IAsyncDisposable batch)
{
_batch = batch;
}

public async Task ApplyAsync(CancellationToken cancellationToken)
{
if (_batch == null)
throw new InvalidOperationException("The batch has already been applied.");

await _batch.DisposeAsync().ConfigureAwait(false);
_batch = null;
}

public void Dispose()
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<RestrictedInternalsVisibleTo Include="Microsoft.CodeAnalysis.UnitTesting.SourceBasedTestDiscovery.Core" Partner="UnitTesting" Key="$(UnitTestingKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.LanguageServer" Key="$(MicrosoftCodeAnalysisLanguageServerKey)" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.LanguageServer.UnitTests" Key="$(MicrosoftCodeAnalysisLanguageServerKey)" />
<InternalsVisibleTo Include="Roslyn.VisualStudio.DiagnosticsWindow" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.CodeLens" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ internal interface IWorkspaceProject : IDisposable

Task SetProjectHasAllInformationAsync(bool hasAllInformation, CancellationToken cancellationToken);

Task<IAsyncDisposable> StartBatchAsync(CancellationToken cancellationToken);
Task<IWorkspaceProjectBatch> StartBatchAsync(CancellationToken cancellationToken);
}
16 changes: 16 additions & 0 deletions src/Workspaces/Remote/Core/ProjectSystem/IWorkspaceProjectBatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using StreamJsonRpc;

namespace Microsoft.CodeAnalysis.Remote.ProjectSystem;

[RpcMarshalable]
internal interface IWorkspaceProjectBatch : IDisposable
{
Task ApplyAsync(CancellationToken cancellationToken);
}

0 comments on commit 18feba0

Please sign in to comment.