Skip to content

Commit e82de11

Browse files
authored
Remove the use of MSBuildLocator from dotnet-format (#48658)
dotnet-format uses the Roslyn MSBuildWorkspace for loading projects. Since Roslyn 4.9.0, the MSBuildWorkspace has used a separate BuildHost process for project loading which handles loading MSBuild.
2 parents dc91edd + 171339d commit e82de11

File tree

6 files changed

+0
-74
lines changed

6 files changed

+0
-74
lines changed

src/BuiltInTools/dotnet-format/Commands/FormatCommandCommon.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,6 @@ internal static async Task<int> FormatAsync(FormatOptions formatOptions, ILogger
100100
{
101101
var runtimeVersion = GetRuntimeVersion();
102102
logger.LogDebug(Resources.The_dotnet_runtime_version_is_0, runtimeVersion);
103-
104-
if (!TryLoadMSBuild(out var msBuildPath))
105-
{
106-
logger.LogError(Resources.Unable_to_locate_MSBuild_Ensure_the_NET_SDK_was_installed_with_the_official_installer);
107-
return UnableToLocateMSBuildExitCode;
108-
}
109-
110-
logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath);
111103
}
112104

113105
var formatResult = await CodeFormatter.FormatWorkspaceAsync(
@@ -345,34 +337,6 @@ private static string EnsureTrailingSlash(string path)
345337
?.InformationalVersion;
346338
}
347339

348-
internal static bool TryLoadMSBuild([NotNullWhen(returnValue: true)] out string? msBuildPath)
349-
{
350-
try
351-
{
352-
// Get the global.json pinned SDK or latest instance.
353-
var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances()
354-
.Where(instance => instance.Version.Major >= 6)
355-
.FirstOrDefault();
356-
if (msBuildInstance is null)
357-
{
358-
msBuildPath = null;
359-
return false;
360-
}
361-
362-
msBuildPath = Path.EndsInDirectorySeparator(msBuildInstance.MSBuildPath)
363-
? msBuildInstance.MSBuildPath
364-
: msBuildInstance.MSBuildPath + Path.DirectorySeparatorChar;
365-
366-
Build.Locator.MSBuildLocator.RegisterMSBuildPath(msBuildPath);
367-
return true;
368-
}
369-
catch
370-
{
371-
msBuildPath = null;
372-
return false;
373-
}
374-
}
375-
376340
internal static string GetRuntimeVersion()
377341
{
378342
var pathParts = typeof(string).Assembly.Location.Split('\\', '/');

src/BuiltInTools/dotnet-format/dotnet-format.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
</PropertyGroup>
3131

3232
<ItemGroup>
33-
<PackageReference Include="Microsoft.Build" ExcludeAssets="Runtime" PrivateAssets="All" />
34-
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="Runtime" PrivateAssets="All" />
35-
<PackageReference Include="Microsoft.Build.Locator" />
36-
3733
<PackageReference Include="Microsoft.CodeAnalysis" />
3834
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
3935
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" />

test/dotnet-format.UnitTests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public async Task InitializeAsync()
3737

3838
// Load the analyzer_project into a MSBuildWorkspace.
3939
var workspacePath = Path.Combine(TestProjectsPathHelper.GetProjectsDirectory(), s_analyzerProjectFilePath);
40-
41-
MSBuildRegistrar.RegisterInstance();
4240
var analyzerWorkspace = await MSBuildWorkspaceLoader.LoadAsync(workspacePath, WorkspaceType.Project, binaryLogPath: null, logWorkspaceWarnings: true, logger, CancellationToken.None);
4341

4442
TestOutputHelper.WriteLine(logger.GetLog());

test/dotnet-format.UnitTests/CodeFormatterTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,6 @@ internal async Task<string> TestFormatWorkspaceAsync(
660660
}
661661

662662
var logger = new TestLogger();
663-
var msBuildPath = MSBuildRegistrar.RegisterInstance();
664-
665-
logger.LogTrace(Resources.Using_msbuildexe_located_in_0, msBuildPath);
666663

667664
var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude);
668665
var formatOptions = new FormatOptions(

test/dotnet-format.UnitTests/MSBuild/MSBuildWorkspaceLoaderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ private static async Task AssertProjectLoadsCleanlyAsync(string projectFilePath,
133133
{
134134
var binaryLogPath = Path.ChangeExtension(projectFilePath, ".binlog");
135135

136-
MSBuildRegistrar.RegisterInstance();
137136
using var workspace = (MSBuildWorkspace)await MSBuildWorkspaceLoader.LoadAsync(projectFilePath, WorkspaceType.Project, binaryLogPath, logWorkspaceWarnings: true, logger, CancellationToken.None);
138137

139138
Assert.Empty(workspace.Diagnostics);

test/dotnet-format.UnitTests/Utilities/MSBuildRegistrar.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)