Skip to content

Commit dcdf256

Browse files
Copilotbaronfel
andcommitted
Fix MSB1025 error when using DistributedFileLogger with null central logger
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent 3dcb329 commit dcdf256

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/MSBuild.UnitTests/XMake_Tests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,44 @@ public void TestProcessFileLoggerSwitch5()
22942294
distributedLoggerRecords.Count.ShouldBe(0); // "Expected no distributed loggers to be attached"
22952295
loggers.Count.ShouldBe(0); // "Expected no central loggers to be attached"
22962296
}
2297+
2298+
/// <summary>
2299+
/// Verify that DistributedLoggerRecords with null CentralLogger don't cause exceptions when creating ProjectCollection
2300+
/// This is a regression test for the issue where -dfl flag caused MSB1025 error due to null logger not being filtered.
2301+
/// </summary>
2302+
[Fact]
2303+
public void TestNullCentralLoggerInDistributedLoggerRecord()
2304+
{
2305+
// Simulate the scenario when using -dfl flag
2306+
// ProcessDistributedFileLogger creates a DistributedLoggerRecord with null CentralLogger
2307+
var distributedLoggerRecords = new List<DistributedLoggerRecord>();
2308+
bool distributedFileLogger = true;
2309+
string[] fileLoggerParameters = null;
2310+
2311+
MSBuildApp.ProcessDistributedFileLogger(
2312+
distributedFileLogger,
2313+
fileLoggerParameters,
2314+
distributedLoggerRecords);
2315+
2316+
// Verify that we have a distributed logger record with null central logger
2317+
distributedLoggerRecords.Count.ShouldBe(1);
2318+
distributedLoggerRecords[0].CentralLogger.ShouldBeNull();
2319+
2320+
// This should not throw ArgumentNullException when creating ProjectCollection
2321+
// The fix filters out null central loggers from the evaluationLoggers array
2322+
var loggers = Array.Empty<ILogger>();
2323+
Should.NotThrow(() =>
2324+
{
2325+
using var projectCollection = new ProjectCollection(
2326+
new Dictionary<string, string>(),
2327+
loggers: [.. loggers, .. distributedLoggerRecords.Select(d => d.CentralLogger).Where(l => l is not null)],
2328+
remoteLoggers: null,
2329+
toolsetDefinitionLocations: ToolsetDefinitionLocations.Default,
2330+
maxNodeCount: 1,
2331+
onlyLogCriticalEvents: false,
2332+
loadProjectsReadOnly: true);
2333+
});
2334+
}
22972335
#endregion
22982336

22992337
#region ProcessConsoleLoggerSwitches

src/MSBuild/XMake.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,8 +1392,8 @@ internal static bool BuildProject(
13921392
// all of the loggers that are single-node only
13931393
.. loggers,
13941394
// all of the central loggers for multi-node systems. These need to be resilient to multiple calls
1395-
// to Initialize
1396-
.. distributedLoggerRecords.Select(d => d.CentralLogger)
1395+
// to Initialize. Filter out null loggers (e.g., DistributedFileLogger uses null central logger).
1396+
.. distributedLoggerRecords.Select(d => d.CentralLogger).Where(l => l is not null)
13971397
];
13981398

13991399
projectCollection = new ProjectCollection(

0 commit comments

Comments
 (0)