Skip to content

Commit ffc096c

Browse files
authored
Revert "Another attempt to move MiscellaneousFilesWorkspace to a bg thread (#77983)
This reverts commit 2e8fcbe.
1 parent ad1c386 commit ffc096c

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

src/VisualStudio/Core/Def/LanguageService/AbstractPackage`2.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ protected override void RegisterInitializeAsyncWork(PackageLoadTasks packageInit
3939
base.RegisterInitializeAsyncWork(packageInitializationTasks);
4040

4141
packageInitializationTasks.AddTask(isMainThreadTask: true, task: PackageInitializationMainThreadAsync);
42-
packageInitializationTasks.AddTask(isMainThreadTask: false, task: PackageInitializationBackgroundThreadAsync);
4342
}
4443

4544
private async Task PackageInitializationMainThreadAsync(PackageLoadTasks packageInitializationTasks, CancellationToken cancellationToken)
@@ -59,33 +58,35 @@ private async Task PackageInitializationMainThreadAsync(PackageLoadTasks package
5958
RegisterEditorFactory(editorFactory);
6059
}
6160

61+
// Misc workspace has to be up and running by the time our package is usable so that it can track running
62+
// doc events and appropriately map files to/from it and other relevant workspaces (like the
63+
// metadata-as-source workspace).
64+
var miscellaneousFilesWorkspace = this.ComponentModel.GetService<MiscellaneousFilesWorkspace>();
65+
6266
// awaiting an IVsTask guarantees to return on the captured context
6367
await shell.LoadPackageAsync(Guids.RoslynPackageId);
64-
}
6568

66-
private Task PackageInitializationBackgroundThreadAsync(PackageLoadTasks packageInitializationTasks, CancellationToken cancellationToken)
67-
{
68-
RegisterLanguageService(typeof(TLanguageService), async cancellationToken =>
69-
{
70-
// Ensure we're on the BG when creating the language service.
71-
await TaskScheduler.Default;
72-
73-
// Create the language service, tell it to set itself up, then store it in a field
74-
// so we can notify it that it's time to clean up.
75-
_languageService = CreateLanguageService();
76-
await _languageService.SetupAsync(cancellationToken).ConfigureAwait(false);
69+
packageInitializationTasks.AddTask(
70+
isMainThreadTask: false,
71+
task: (PackageLoadTasks packageInitializationTasks, CancellationToken cancellationToken) =>
72+
{
73+
RegisterLanguageService(typeof(TLanguageService), async cancellationToken =>
74+
{
75+
// Ensure we're on the BG when creating the language service.
76+
await TaskScheduler.Default;
7777

78-
return _languageService.ComAggregate!;
79-
});
78+
// Create the language service, tell it to set itself up, then store it in a field
79+
// so we can notify it that it's time to clean up.
80+
_languageService = CreateLanguageService();
81+
await _languageService.SetupAsync(cancellationToken).ConfigureAwait(false);
8082

81-
// Misc workspace has to be up and running by the time our package is usable so that it can track running
82-
// doc events and appropriately map files to/from it and other relevant workspaces (like the
83-
// metadata-as-source workspace).
84-
var miscellaneousFilesWorkspace = this.ComponentModel.GetService<MiscellaneousFilesWorkspace>();
83+
return _languageService.ComAggregate!;
84+
});
8585

86-
RegisterMiscellaneousFilesWorkspaceInformation(miscellaneousFilesWorkspace);
86+
RegisterMiscellaneousFilesWorkspaceInformation(miscellaneousFilesWorkspace);
8787

88-
return Task.CompletedTask;
88+
return Task.CompletedTask;
89+
});
8990
}
9091

9192
protected override void RegisterOnAfterPackageLoadedAsyncWork(PackageLoadTasks afterPackageLoadedTasks)

src/VisualStudio/Core/Def/ProjectSystem/MiscellaneousFilesWorkspace.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
3131
private readonly IThreadingContext _threadingContext;
3232
private readonly IVsService<IVsTextManager> _textManagerService;
3333
private readonly OpenTextBufferProvider _openTextBufferProvider;
34-
private readonly Lazy<IMetadataAsSourceFileService> _fileTrackingMetadataAsSourceService;
34+
private readonly IMetadataAsSourceFileService _fileTrackingMetadataAsSourceService;
3535

3636
private readonly ConcurrentDictionary<Guid, LanguageInformation> _languageInformationByLanguageGuid = [];
3737

@@ -47,24 +47,24 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
4747
/// </summary>
4848
private readonly Dictionary<string, (ProjectId projectId, SourceTextContainer textContainer)> _monikersToProjectIdAndContainer = [];
4949

50-
private readonly Lazy<ImmutableArray<MetadataReference>> _metadataReferences;
50+
private readonly ImmutableArray<MetadataReference> _metadataReferences;
5151

5252
[ImportingConstructor]
5353
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
5454
public MiscellaneousFilesWorkspace(
5555
IThreadingContext threadingContext,
5656
IVsService<SVsTextManager, IVsTextManager> textManagerService,
5757
OpenTextBufferProvider openTextBufferProvider,
58-
Lazy<IMetadataAsSourceFileService> fileTrackingMetadataAsSourceService,
59-
Composition.ExportProvider exportProvider)
60-
: base(VisualStudioMefHostServices.Create(exportProvider), WorkspaceKind.MiscellaneousFiles)
58+
IMetadataAsSourceFileService fileTrackingMetadataAsSourceService,
59+
VisualStudioWorkspace visualStudioWorkspace)
60+
: base(visualStudioWorkspace.Services.HostServices, WorkspaceKind.MiscellaneousFiles)
6161
{
6262
_threadingContext = threadingContext;
6363
_textManagerService = textManagerService;
6464
_openTextBufferProvider = openTextBufferProvider;
6565
_fileTrackingMetadataAsSourceService = fileTrackingMetadataAsSourceService;
6666

67-
_metadataReferences = new(() => [.. CreateMetadataReferences()]);
67+
_metadataReferences = [.. CreateMetadataReferences()];
6868

6969
_openTextBufferProvider.AddListener(this);
7070
}
@@ -122,10 +122,6 @@ public void RegisterLanguage(Guid languageGuid, string languageName, string scri
122122

123123
private IEnumerable<MetadataReference> CreateMetadataReferences()
124124
{
125-
// VisualStudioMetadataReferenceManager construction requires the main thread
126-
// TODO: Determine if main thread affinity can be removed: https://github.com/dotnet/roslyn/issues/77791
127-
_threadingContext.ThrowIfNotOnUIThread();
128-
129125
var manager = this.Services.GetService<VisualStudioMetadataReferenceManager>();
130126
var searchPaths = VisualStudioMetadataReferenceManager.GetReferencePaths();
131127

@@ -265,7 +261,7 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
265261
{
266262
_threadingContext.ThrowIfNotOnUIThread();
267263

268-
if (_fileTrackingMetadataAsSourceService.Value.TryAddDocumentToWorkspace(moniker, textBuffer.AsTextContainer(), out var _))
264+
if (_fileTrackingMetadataAsSourceService.TryAddDocumentToWorkspace(moniker, textBuffer.AsTextContainer(), out var _))
269265
{
270266
// We already added it, so we will keep it excluded from the misc files workspace
271267
return;
@@ -286,24 +282,20 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
286282
/// </summary>
287283
private ProjectInfo CreateProjectInfoForDocument(string filePath)
288284
{
289-
// Potential calculation of _metadataReferences requires being on the main thread
290-
// TODO: Determine if main thread affinity can be removed: https://github.com/dotnet/roslyn/issues/77791
291-
_threadingContext.ThrowIfNotOnUIThread();
292-
293285
// This should always succeed since we only got here if we already confirmed the moniker is acceptable
294286
var languageInformation = TryGetLanguageInformation(filePath);
295287
Contract.ThrowIfNull(languageInformation);
296288

297289
var checksumAlgorithm = SourceHashAlgorithms.Default;
298290
var fileLoader = new WorkspaceFileTextLoader(Services.SolutionServices, filePath, defaultEncoding: null);
299291
return MiscellaneousFileUtilities.CreateMiscellaneousProjectInfoForDocument(
300-
this, filePath, fileLoader, languageInformation, checksumAlgorithm, Services.SolutionServices, _metadataReferences.Value);
292+
this, filePath, fileLoader, languageInformation, checksumAlgorithm, Services.SolutionServices, _metadataReferences);
301293
}
302294

303295
private void DetachFromDocument(string moniker)
304296
{
305297
_threadingContext.ThrowIfNotOnUIThread();
306-
if (_fileTrackingMetadataAsSourceService.Value.TryRemoveDocumentFromWorkspace(moniker))
298+
if (_fileTrackingMetadataAsSourceService.TryRemoveDocumentFromWorkspace(moniker))
307299
{
308300
return;
309301
}

0 commit comments

Comments
 (0)