Skip to content

Commit cf2bc5a

Browse files
committed
Fixing issues on Ubuntu and with .NET 9.
1 parent d7b8cd5 commit cf2bc5a

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/PostSharp.Engineering.BuildTools/BaseCommand.cs

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

44-
MSBuildHelper.InitializeLocator();
45-
4644
if ( DockerHelper.IsDockerBuild() )
4745
{
4846
buildContext.Console.WriteMessage( "Docker detected." );

src/PostSharp.Engineering.BuildTools/MSBuildHelper.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Build.Locator;
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;
24
using Microsoft.VisualStudio.Setup.Configuration;
35
using System;
46
using System.Collections.Generic;
@@ -18,17 +20,17 @@ internal static class MSBuildHelper
1820

1921
public static void InitializeLocator()
2022
{
21-
if (!_isInitialized)
23+
if ( !_isInitialized )
2224
{
23-
if (MSBuildLocator.CanRegister)
25+
if ( MSBuildLocator.CanRegister )
2426
{
2527
try
2628
{
2729
MSBuildLocator.RegisterDefaults();
2830

2931
_isInitialized = true;
3032
}
31-
catch (Exception e)
33+
catch ( Exception e )
3234
{
3335
throw new InvalidOperationException(
3436
$"Cannot find a suitable version of MSBuild for "
@@ -45,7 +47,7 @@ public static void InitializeLocator()
4547
{
4648
var directory = FindLatestMSBuildDirectory( context );
4749

48-
if (directory == null)
50+
if ( directory == null )
4951
{
5052
return directory;
5153
}
@@ -57,14 +59,16 @@ public static void InitializeLocator()
5759

5860
private static string? FindLatestMSBuildDirectory( BuildContext context )
5961
{
62+
InitializeLocator();
63+
6064
var instances = GetVisualStudioInstances( context ).OrderByDescending( i => i.Version );
6165

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

67-
if (Directory.Exists( directory ))
71+
if ( Directory.Exists( directory ) )
6872
{
6973
return directory;
7074
}
@@ -75,7 +79,9 @@ public static void InitializeLocator()
7579

7680
public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildContext context )
7781
{
78-
if (!RuntimeInformation.IsOSPlatform( OSPlatform.Windows ))
82+
InitializeLocator();
83+
84+
if ( !RuntimeInformation.IsOSPlatform( OSPlatform.Windows ) )
7985
{
8086
return [];
8187
}
@@ -95,9 +101,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
95101
{
96102
enumInstances.Next( 1, instances, out fetched );
97103

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

102108
list.Add(
103109
new VisualStudioInstance(
@@ -106,9 +112,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
106112
instance.GetInstallationPath() ) );
107113
}
108114
}
109-
while (fetched > 0);
115+
while ( fetched > 0 );
110116
}
111-
catch (COMException exception)
117+
catch ( COMException exception )
112118
{
113119
context.Console.WriteWarning( $"Cannot find VS instances: {exception.Message}" );
114120

src/PostSharp.Engineering.BuildTools/Utilities/ToolInvocationHelper.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,18 @@ void FilterProcessOutput( string output )
374374
using ( ManualResetEvent exitedEvent = new( false ) )
375375
{
376376
process.EnableRaisingEvents = true;
377-
process.Exited += ( _, _ ) => exitedEvent.Set();
377+
378+
process.Exited += ( _, _ ) =>
379+
{
380+
try
381+
{
382+
exitedEvent.Set();
383+
}
384+
catch ( ObjectDisposedException )
385+
{
386+
// This happens on Ubuntu.
387+
}
388+
};
378389

379390
using ( cancellationToken.Register( () => cancelledEvent.Set() ) )
380391
{

0 commit comments

Comments
 (0)