Skip to content

Commit eec63a8

Browse files
committed
Add a lock around loading a MSBuild workspace
1 parent dbc1651 commit eec63a8

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

src/Workspaces/MSBuildWorkspaceLoader.cs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Microsoft.CodeAnalysis.Tools.Workspaces
1313
{
1414
internal static class MSBuildWorkspaceLoader
1515
{
16+
private static readonly SemaphoreSlim s_guard = new SemaphoreSlim(1, 1);
17+
1618
public static async Task<Workspace?> LoadAsync(
1719
string solutionOrProjectPath,
1820
WorkspaceType workspaceType,
@@ -29,35 +31,45 @@ internal static class MSBuildWorkspaceLoader
2931
{ "AlwaysCompileMarkupFilesInSeparateDomain", bool.FalseString },
3032
};
3133

32-
var workspace = MSBuildWorkspace.Create(properties);
34+
MSBuildWorkspace workspace;
3335

34-
Build.Framework.ILogger? binlog = null;
35-
if (createBinaryLog)
36+
await s_guard.WaitAsync();
37+
try
3638
{
37-
binlog = new Build.Logging.BinaryLogger()
39+
workspace = MSBuildWorkspace.Create(properties);
40+
41+
Build.Framework.ILogger? binlog = null;
42+
if (createBinaryLog)
3843
{
39-
Parameters = Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog"),
40-
Verbosity = Build.Framework.LoggerVerbosity.Diagnostic,
41-
};
42-
}
44+
binlog = new Build.Logging.BinaryLogger()
45+
{
46+
Parameters = Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog"),
47+
Verbosity = Build.Framework.LoggerVerbosity.Diagnostic,
48+
};
49+
}
4350

44-
if (workspaceType == WorkspaceType.Solution)
45-
{
46-
await workspace.OpenSolutionAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
47-
}
48-
else
49-
{
50-
try
51+
if (workspaceType == WorkspaceType.Solution)
5152
{
52-
await workspace.OpenProjectAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
53+
await workspace.OpenSolutionAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
5354
}
54-
catch (InvalidOperationException)
55+
else
5556
{
56-
logger.LogError(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, solutionOrProjectPath);
57-
workspace.Dispose();
58-
return null;
57+
try
58+
{
59+
await workspace.OpenProjectAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
60+
}
61+
catch (InvalidOperationException)
62+
{
63+
logger.LogError(Resources.Could_not_format_0_Format_currently_supports_only_CSharp_and_Visual_Basic_projects, solutionOrProjectPath);
64+
workspace.Dispose();
65+
return null;
66+
}
5967
}
6068
}
69+
finally
70+
{
71+
s_guard.Release();
72+
}
6173

6274
LogWorkspaceDiagnostics(logger, logWorkspaceWarnings, workspace.Diagnostics);
6375

0 commit comments

Comments
 (0)