Skip to content

Commit fb5bf2a

Browse files
One way of solving test race condition
1 parent 231b325 commit fb5bf2a

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

CodeConverter/Shared/CompilationOptionsExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
43
using ICSharpCode.CodeConverter.CSharp;
54
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.Host.Mef;
66

77
namespace ICSharpCode.CodeConverter.Shared
88
{
99
internal static class CompilationOptionsExtensions
1010
{
11-
private static Lazy<Workspace> Workspace = new Lazy<Workspace>(() => new AdhocWorkspace());
12-
1311
public static Document AddDocumentFromTree(this Project project, SyntaxTree tree)
1412
{
1513
return project.AddDocument("CodeToConvert", tree.GetRoot(), filePath: Path.Combine(Directory.GetCurrentDirectory(), "TempCodeToConvert.txt"));
@@ -21,7 +19,7 @@ public static Project CreateProject(this CompilationOptions options, IEnumerable
2119

2220
string projFileExtension = parseOptions.Language == LanguageNames.CSharp ? ".csproj" : ".vbproj";
2321
var projectFilePath = Path.Combine(Directory.GetCurrentDirectory() + singleDocumentAssemblyName + projFileExtension);
24-
var solution = Workspace.Value.CurrentSolution.AddProject(projectId, singleDocumentAssemblyName,
22+
var solution = WorkspaceFactory.AdhocSolution.AddProject(projectId, singleDocumentAssemblyName,
2523
singleDocumentAssemblyName, options.Language)
2624
.WithProjectFilePath(projectId, projectFilePath);
2725

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace ICSharpCode.CodeConverter.Shared
5+
{
6+
public static class WorkspaceFactory
7+
{
8+
/// <summary>
9+
/// Library consumers must use this to guard any non-VS workspace creation (i.e. adhoc or msbuild)
10+
/// Know MEF bug means creating multiple workspaces outside VS context in parallel has race conditions: https://github.com/dotnet/roslyn/issues/24260
11+
/// </summary>
12+
public static object WorkspaceCreationLock = new object();
13+
private static Lazy<Solution> LazyAdhocSolution = new Lazy<Solution>(() => {
14+
lock (WorkspaceCreationLock) {
15+
return new AdhocWorkspace().CurrentSolution;
16+
}
17+
});
18+
public static Solution AdhocSolution => LazyAdhocSolution.Value;
19+
}
20+
}

CommandLine/CodeConv.Shared/MSBuildWorkspaceConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ private static MSBuildWorkspace CreateWorkspace(Dictionary<string, string> build
119119
MSBuildLocator.RegisterInstance(instance);
120120
AppDomain.CurrentDomain.UseVersionAgnosticAssemblyResolution();
121121
}
122-
return MSBuildWorkspace.Create(buildProps);
122+
lock (WorkspaceFactory.WorkspaceCreationLock) {
123+
return MSBuildWorkspace.Create(buildProps);
124+
}
123125
}
124126

125127
public void Dispose()

0 commit comments

Comments
 (0)