Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Tasks/Common/ProcessTaskEnvironmentDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public ProcessStartInfo GetProcessStartInfo()
var startInfo = new ProcessStartInfo
{
WorkingDirectory = _projectDirectory.Value,
UseShellExecute = false,
};

// Populate environment from the scoped environment dictionary
Expand Down
26 changes: 26 additions & 0 deletions src/Tasks/Common/TaskEnvironmentDefaults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Provides a default TaskEnvironment for single-threaded MSBuild execution.
// When MSBuild supports IMultiThreadableTask, it sets TaskEnvironment directly.
// This fallback ensures tasks work with older MSBuild versions that do not set it.

#if NETFRAMEWORK

using System;

namespace Microsoft.Build.Framework
{
internal static class TaskEnvironmentDefaults
{
/// <summary>
/// Creates a default TaskEnvironment backed by the current process environment.
/// Uses Environment.CurrentDirectory as the project directory, which in single-threaded
/// MSBuild is set to the project directory before task execution.
/// </summary>
internal static TaskEnvironment Create() =>
new TaskEnvironment(new ProcessTaskEnvironmentDriver(Environment.CurrentDirectory));
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using FluentAssertions;
using Microsoft.Build.Framework;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.NET.Build.Tasks.UnitTests
{
[Collection("CWD-Dependent")]

public class GivenAFilterResolvedFilesMultiThreading
{
private const string AssetsJson = """
{
"version": 3,
"targets": { ".NETCoreApp,Version=v8.0": {} },
"libraries": {},
"packageFolders": {},
"projectFileDependencyGroups": { ".NETCoreApp,Version=v8.0": [] },
"project": {
"version": "1.0.0",
"frameworks": { "net8.0": {} }
}
}
""";

[Fact]
public void FilterResolvedFiles_HasMultiThreadableAttribute()
{
typeof(FilterResolvedFiles).GetCustomAttribute<MSBuildMultiThreadableTaskAttribute>()
.Should().NotBeNull("task must be decorated with [MSBuildMultiThreadableTask]");
}

[Fact]
public void AssetsFilePath_IsResolvedRelativeToProjectDirectory()
{
var projectDir = Path.GetFullPath(Path.Combine(Path.GetTempPath(), $"filter-mt-{Guid.NewGuid():N}"));
Directory.CreateDirectory(projectDir);
try
{
var assetsDir = Path.Combine(projectDir, "obj");
Directory.CreateDirectory(assetsDir);
File.WriteAllText(Path.Combine(assetsDir, "project.assets.json"), AssetsJson);

var task = new FilterResolvedFiles
{
BuildEngine = new MockBuildEngine(),
AssetsFilePath = Path.Combine("obj", "project.assets.json"),
ResolvedFiles = Array.Empty<ITaskItem>(),
PackagesToPrune = Array.Empty<ITaskItem>(),
TargetFramework = ".NETCoreApp,Version=v8.0",
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(projectDir),
};
Comment on lines +51 to +58
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssetsFilePath is set to a Windows-specific relative string ("obj\project.assets.json"). On non-Windows this becomes a filename containing a backslash, so TaskEnvironment.GetAbsolutePath will resolve to a path that doesn’t exist and the test will fail. Use Path.Combine("obj", "project.assets.json") (or "obj/project.assets.json") so the test is OS-agnostic.

Copilot uses AI. Check for mistakes.

var result = task.Execute();
result.Should().BeTrue("task should succeed when assets file is found via TaskEnvironment");
}
finally
{
Directory.Delete(projectDir, true);
}
}

private static (bool result, MockBuildEngine engine) RunTask(
string assetsRelPath, string projectDir)
{
var engine = new MockBuildEngine();
var task = new FilterResolvedFiles
{
BuildEngine = engine,
AssetsFilePath = assetsRelPath,
ResolvedFiles = Array.Empty<ITaskItem>(),
PackagesToPrune = Array.Empty<ITaskItem>(),
TargetFramework = ".NETCoreApp,Version=v8.0",
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(projectDir),
};

var result = task.Execute();
return (result, engine);
}

[Fact]
public void ItProducesSameResultsInMultiProcessAndMultiThreadedEnvironments()
{
var projectDir = Path.GetFullPath(Path.Combine(Path.GetTempPath(), $"filter-parity-{Guid.NewGuid():N}"));
var otherDir = Path.GetFullPath(Path.Combine(Path.GetTempPath(), $"filter-decoy-{Guid.NewGuid():N}"));
Directory.CreateDirectory(projectDir);
Directory.CreateDirectory(otherDir);
var savedCwd = Directory.GetCurrentDirectory();
try
{
var objDir = Path.Combine(projectDir, "obj");
Directory.CreateDirectory(objDir);
File.WriteAllText(Path.Combine(objDir, "project.assets.json"), AssetsJson);

var assetsRelPath = Path.Combine("obj", "project.assets.json");

// --- Multiprocess mode: CWD == projectDir ---
Directory.SetCurrentDirectory(projectDir);
var (mpResult, mpEngine) = RunTask(assetsRelPath, projectDir);

// --- Multithreaded mode: CWD == otherDir ---
Directory.SetCurrentDirectory(otherDir);
var (mtResult, mtEngine) = RunTask(assetsRelPath, projectDir);

mpResult.Should().Be(mtResult,
"task should return the same success/failure in both environments");
mpEngine.Errors.Count.Should().Be(mtEngine.Errors.Count,
"error count should be the same in both environments");
mpEngine.Warnings.Count.Should().Be(mtEngine.Warnings.Count,
"warning count should be the same in both environments");
}
finally
{
Directory.SetCurrentDirectory(savedCwd);
Directory.Delete(projectDir, true);
if (Directory.Exists(otherDir)) Directory.Delete(otherDir, true);
}
}

[Theory]
[InlineData(4)]
[InlineData(16)]
public async System.Threading.Tasks.Task FilterResolvedFiles_ConcurrentExecution(int parallelism)
{
var projectDir = Path.GetFullPath(Path.Combine(Path.GetTempPath(), $"filter-concurrent-{Guid.NewGuid():N}"));
Directory.CreateDirectory(projectDir);
try
{
var objDir = Path.Combine(projectDir, "obj");
Directory.CreateDirectory(objDir);
File.WriteAllText(Path.Combine(objDir, "project.assets.json"), AssetsJson);

var errors = new ConcurrentBag<string>();
using var startGate = new ManualResetEventSlim(false);
var tasks = new System.Threading.Tasks.Task[parallelism];
for (int i = 0; i < parallelism; i++)
{
int idx = i;
tasks[idx] = System.Threading.Tasks.Task.Run(() =>
{
try
{
var task = new FilterResolvedFiles
{
BuildEngine = new MockBuildEngine(),
AssetsFilePath = Path.Combine("obj", "project.assets.json"),
ResolvedFiles = Array.Empty<ITaskItem>(),
PackagesToPrune = Array.Empty<ITaskItem>(),
TargetFramework = ".NETCoreApp,Version=v8.0",
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(projectDir),
};
startGate.Wait();
var result = task.Execute();
if (!result) errors.Add($"Thread {idx}: Execute returned false");
}
catch (Exception ex) { errors.Add($"Thread {idx}: {ex.Message}"); }
});
}
startGate.Set();
await System.Threading.Tasks.Task.WhenAll(tasks);

errors.Should().BeEmpty();
}
finally
{
Directory.Delete(projectDir, true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void ItSelectsCompatibleItems()
TargetRuntimeIdentifier = "ubuntu.18.04-x64",
Items = items,
RuntimeIdentifierGraphPath = testRuntimeGraphPath,
BuildEngine = new MockBuildEngine()
BuildEngine = new MockBuildEngine(),
TaskEnvironment = TaskEnvironmentHelper.CreateForTest()
};

// Act
Expand Down Expand Up @@ -59,7 +60,8 @@ public void ItSelectsItemsWithExactMatch()
TargetRuntimeIdentifier = "win-x64",
Items = items,
RuntimeIdentifierGraphPath = testRuntimeGraphPath,
BuildEngine = new MockBuildEngine()
BuildEngine = new MockBuildEngine(),
TaskEnvironment = TaskEnvironmentHelper.CreateForTest()
};

// Act
Expand Down Expand Up @@ -88,7 +90,8 @@ public void ItSkipsItemsWithoutRuntimeIdentifierMetadata()
TargetRuntimeIdentifier = "linux-x64",
Items = items,
RuntimeIdentifierGraphPath = testRuntimeGraphPath,
BuildEngine = new MockBuildEngine()
BuildEngine = new MockBuildEngine(),
TaskEnvironment = TaskEnvironmentHelper.CreateForTest()
};

// Act
Expand All @@ -114,7 +117,8 @@ public void ItUsesCustomRuntimeIdentifierMetadata()
Items = new[] { item },
RuntimeIdentifierItemMetadata = "CustomRID",
RuntimeIdentifierGraphPath = testRuntimeGraphPath,
BuildEngine = new MockBuildEngine()
BuildEngine = new MockBuildEngine(),
TaskEnvironment = TaskEnvironmentHelper.CreateForTest()
};

// Act
Expand All @@ -137,7 +141,8 @@ public void ItReturnsEmptyArrayWhenNoItemsProvided()
TargetRuntimeIdentifier = "linux-x64",
Items = new ITaskItem[0],
RuntimeIdentifierGraphPath = testRuntimeGraphPath,
BuildEngine = new MockBuildEngine()
BuildEngine = new MockBuildEngine(),
TaskEnvironment = TaskEnvironmentHelper.CreateForTest()
};

// Act
Expand Down
Loading
Loading