Skip to content

Commit 3a3970f

Browse files
committed
Reverting changes in msbuild initialization.
1 parent cf2bc5a commit 3a3970f

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/PostSharp.Engineering.BuildTools/BaseCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public sealed override int Execute( CommandContext context, T settings )
4141
buildContext = buildContext.WithUseProjectDirectoryAsWorkingDirectory( true );
4242
}
4343

44+
MSBuildHelper.InitializeLocator();
45+
4446
if ( DockerHelper.IsDockerBuild() )
4547
{
4648
buildContext.Console.WriteMessage( "Docker detected." );

src/PostSharp.Engineering.BuildTools/MSBuildHelper.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2-
3-
using Microsoft.Build.Locator;
1+
using Microsoft.Build.Locator;
42
using Microsoft.VisualStudio.Setup.Configuration;
53
using System;
64
using System.Collections.Generic;
@@ -20,17 +18,17 @@ internal static class MSBuildHelper
2018

2119
public static void InitializeLocator()
2220
{
23-
if ( !_isInitialized )
21+
if (!_isInitialized)
2422
{
25-
if ( MSBuildLocator.CanRegister )
23+
if (MSBuildLocator.CanRegister)
2624
{
2725
try
2826
{
2927
MSBuildLocator.RegisterDefaults();
3028

3129
_isInitialized = true;
3230
}
33-
catch ( Exception e )
31+
catch (Exception e)
3432
{
3533
throw new InvalidOperationException(
3634
$"Cannot find a suitable version of MSBuild for "
@@ -47,7 +45,7 @@ public static void InitializeLocator()
4745
{
4846
var directory = FindLatestMSBuildDirectory( context );
4947

50-
if ( directory == null )
48+
if (directory == null)
5149
{
5250
return directory;
5351
}
@@ -59,16 +57,14 @@ public static void InitializeLocator()
5957

6058
private static string? FindLatestMSBuildDirectory( BuildContext context )
6159
{
62-
InitializeLocator();
63-
6460
var instances = GetVisualStudioInstances( context ).OrderByDescending( i => i.Version );
6561

66-
foreach ( var instance in instances )
62+
foreach (var instance in instances)
6763
{
6864
// We got a Visual Studio instance but not all of them have an MSBuild instance. For instance, a Test Agent instance does not have.
6965
var directory = Path.Combine( instance.Path, "MSBuild", "Current", "Bin" );
7066

71-
if ( Directory.Exists( directory ) )
67+
if (Directory.Exists( directory ))
7268
{
7369
return directory;
7470
}
@@ -79,9 +75,7 @@ public static void InitializeLocator()
7975

8076
public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildContext context )
8177
{
82-
InitializeLocator();
83-
84-
if ( !RuntimeInformation.IsOSPlatform( OSPlatform.Windows ) )
78+
if (!RuntimeInformation.IsOSPlatform( OSPlatform.Windows ))
8579
{
8680
return [];
8781
}
@@ -101,9 +95,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
10195
{
10296
enumInstances.Next( 1, instances, out fetched );
10397

104-
if ( fetched > 0 )
98+
if (fetched > 0)
10599
{
106-
var instance = (ISetupInstance2) instances[0];
100+
var instance = (ISetupInstance2)instances[0];
107101

108102
list.Add(
109103
new VisualStudioInstance(
@@ -112,9 +106,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
112106
instance.GetInstallationPath() ) );
113107
}
114108
}
115-
while ( fetched > 0 );
109+
while (fetched > 0);
116110
}
117-
catch ( COMException exception )
111+
catch (COMException exception)
118112
{
119113
context.Console.WriteWarning( $"Cannot find VS instances: {exception.Message}" );
120114

0 commit comments

Comments
 (0)