Skip to content

Conversation

@JanProvaznik
Copy link
Member

@JanProvaznik JanProvaznik commented Dec 10, 2025

fixes #12484
This PR migrates several file I/O tasks to use the new multithreadable task API by implementing the \IMultiThreadableTask\ interface and using \TaskEnvironment\ for thread-safe path resolution. The aim is to be maximally compatible with prior behavior in terms of where it would throw

Changes

The following tasks now implement \IMultiThreadableTask\ and use \TaskEnvironment.GetAbsolutePath()\ instead of \Path.GetFullPath():

  • Copy.cs
  • Delete.cs
  • MakeDir.cs
  • RemoveDir.cs
  • Touch.cs

Why

\Path.GetFullPath()\ uses process-global state (current working directory) which is not thread-safe when multiple tasks run in parallel. The \TaskEnvironment.GetAbsolutePath()\ method provides thread-safe path resolution by using the task's execution context.

This enables these tasks to be safely executed in parallel when MSBuild's multithreading is enabled.

Copilot AI and others added 6 commits December 3, 2025 16:40
…n status

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
…s section

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
- Updated FileState.cs to accept TaskEnvironment parameter
- Migrated 7 File I/O tasks to IMultiThreadableTask:
  * Copy - added attribute, interface, TaskEnvironment property and updated path/environment access
  * Delete - migrated with proper path resolution
  * MakeDir - migrated with path deduplication
  * RemoveDir - migrated with directory operations
  * Touch - migrated with file timestamp operations
  * ReadLinesFromFile - migrated for file reading
  * WriteLinesToFile - migrated for file writing
- All tasks use MSBuildMultiThreadableTaskAttribute for routing
- All tasks implement IMultiThreadableTask for TaskEnvironment access
- Tasks use TaskEnvironment.GetAbsolutePath() for thread-safe path resolution
- Tasks use TaskEnvironment.GetEnvironmentVariable() for thread-safe environment access

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Copilot AI review requested due to automatic review settings December 10, 2025 16:21
@JanProvaznik JanProvaznik changed the title Remove null-conditional operators for TaskEnvironment in file I/O tasks Migrate file I/O tasks to multithreadable task API Dec 10, 2025
@JanProvaznik JanProvaznik requested a review from AR-May December 10, 2025 16:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR converts several file I/O tasks to support multi-threaded execution by implementing IMultiThreadableTask and using TaskEnvironment for thread-safe path resolution. The changes replace static path resolution methods with instance-based TaskEnvironment.GetAbsolutePath() calls to ensure proper path handling in concurrent scenarios.

Key Changes:

  • Added IMultiThreadableTask interface and [MSBuildMultiThreadableTask] attribute to 7 file I/O task classes
  • Introduced TaskEnvironment property to all modified tasks for thread-safe operations
  • Modified FileState constructor to require TaskEnvironment parameter and use it for path resolution

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Tasks/Touch.cs Added multithreading support; uses TaskEnvironment for path resolution in TouchFile method
src/Tasks/RemoveDir.cs Added multithreading support; resolves paths using TaskEnvironment in Execute and RemoveDirectory methods
src/Tasks/MakeDir.cs Added multithreading support; uses TaskEnvironment for path resolution and deduplication
src/Tasks/FileState.cs Modified constructor to require TaskEnvironment parameter; uses it for absolute path resolution in Lazy initialization
src/Tasks/FileIO/WriteLinesToFile.cs Added multithreading support; uses TaskEnvironment for path resolution instead of FileUtilities.NormalizePath
src/Tasks/FileIO/ReadLinesFromFile.cs Added multithreading support; resolves file paths using TaskEnvironment before file operations
src/Tasks/Delete.cs Added multithreading support; uses TaskEnvironment for path resolution in delete operations
src/Tasks/Copy.cs Added multithreading support; uses TaskEnvironment for path resolution, environment variables, and FileState construction; changed PathsAreIdentical from static to instance method

@AR-May AR-May self-assigned this Dec 12, 2025
Copy link
Member

@AR-May AR-May left a comment

Choose a reason for hiding this comment

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

LGTM, but I would like one more review to ensure there is no behavior change in multi process mode.

@JanProvaznik JanProvaznik requested a review from AR-May January 20, 2026 13:13
AR-May added a commit that referenced this pull request Jan 21, 2026
### Context
PRs #12914 and #12868 showed that we need to be able to fix and
normalize `AbsolutePath`. Corresponding methods for strings live in
`FileUtilities.cs` that is a Shared file and cannot reference Framework
classes like `AbsolutePath`.

### Changes Made
- Moved some related functions to FrameworkFileUtilities class.
- Added the needed functions for `AbsolutePath`

### Testing
unit tests
johnazule pushed a commit to johnazule/msbuild that referenced this pull request Jan 22, 2026
…#13079)

### Context
PRs dotnet#12914 and dotnet#12868 showed that we need to be able to fix and
normalize `AbsolutePath`. Corresponding methods for strings live in
`FileUtilities.cs` that is a Shared file and cannot reference Framework
classes like `AbsolutePath`.

### Changes Made
- Moved some related functions to FrameworkFileUtilities class.
- Added the needed functions for `AbsolutePath`

### Testing
unit tests

[Theory]
[MemberData(nameof(EnvironmentTypes))]
public void TaskEnvironment_GetAbsolutePath_WithEmptyPath_ReturnsProjectDirectory(string environmentType)
Copy link
Member

@AR-May AR-May Jan 23, 2026

Choose a reason for hiding this comment

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

I am not sure this is wanted behavior.

Copy link
Member Author

@JanProvaznik JanProvaznik Jan 23, 2026

Choose a reason for hiding this comment

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

I'd say it's up for discussion behavior I'm just adding tests for here (probably makes sense to changewave argumentexception) that is tangential to this PR (perhaps we should have resolved it in the previous one though I was pushing that fast to unblock you - alas we may need to revisit it)

/// <summary>
/// The task environment for thread-safe operations.
/// </summary>
public TaskEnvironment TaskEnvironment { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Would it be problematic to put this in the common base class TaskExtension to reduce some of the duplication? It's unfortunate that that is public rather than internal :(

Copy link
Member Author

Choose a reason for hiding this comment

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

One thing that comes to mind is forward compat which I don't know if we care about.

Task author releases new version of their task library consuming new msbuild libs. Their task implements TaskExtension.
Someone tries to use it in their older msbuild engine and it can't find the TaskEnvironment property at runtime.

If we don't care about that then the other concern was inheriting marking tasks as multithreadable when they are not which we mitigated by requiring the attribute 🤔 @AR-May is there something I forgot?

Copy link
Member

Choose a reason for hiding this comment

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

Task author releases new version of their task library consuming new msbuild libs. Their task implements TaskExtension. Someone tries to use it in their older msbuild engine and it can't find the TaskEnvironment property at runtime.

That's an excellent point. That is technically against the rules: if you reference a specific MSBuild version, we expect compatibility only from that point forward. But people do it, sometimes inadvertently because of Dependabot updates and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate Copy task to the new task API

5 participants