-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add multithreaded task migration agent skill #13131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| --- | ||
| name: multithreaded-task-migration | ||
| description: Guide for migrating MSBuild tasks to the multithreaded mode support. Use this when asked to convert tasks to thread-safe versions, implement IMultiThreadableTask, or add TaskEnvironment support to tasks. | ||
| --- | ||
|
|
||
| # Migrating MSBuild Tasks to Multithreaded API | ||
|
|
||
| This skill guides you through migrating MSBuild tasks to support multithreaded execution by implementing `IMultiThreadableTask` and using `TaskEnvironment`. | ||
|
|
||
| ## Overview | ||
|
|
||
| MSBuild's multithreaded execution model requires tasks to avoid global process state (working directory, environment variables). Thread-safe tasks declare this capability by annotating with `MSBuildMultiThreadableTask` and use `TaskEnvironment` provided by `IMultiThreadableTask` for safe alternatives. | ||
|
|
||
| ## Migration Steps | ||
|
|
||
| ### Step 1: Update Task Class Declaration | ||
|
|
||
| a. add the attribute | ||
| b. AND implement the interface if it's necessary to use TaskEnvironment APIs. | ||
|
|
||
| ```csharp | ||
| [MSBuildMultiThreadableTask] | ||
| public class MyTask : Task, IMultiThreadableTask | ||
| { | ||
| public TaskEnvironment TaskEnvironment { get; set; } | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| ### Step 2: Replace Path Resolution APIs | ||
|
|
||
| **Critical**: absolutize all path strings with `TaskEnvironment.GetAbsolutePath()` before their use. | ||
| `GetAbsolutePath()` throws `ArgumentNullException` for null inputs and throws for empty inputs. Callers are responsible for validating inputs before calling absolutization APIs. | ||
|
|
||
| ```csharp | ||
| // BEFORE - Uses working directory (UNSAFE) | ||
| string absolutePath = Path.GetFullPath(relativePath); | ||
|
|
||
| // AFTER - Uses project directory (SAFE) | ||
| AbsolutePath absolutePath = TaskEnvironment.GetAbsolutePath(relativePath); | ||
| ``` | ||
|
|
||
| The `AbsolutePath` struct: | ||
| - Has `Value` property returning the absolute path string | ||
| - Has `OriginalValue` property preserving the input path | ||
| - Is implicitly convertible to `string` for File/Directory API compatibility | ||
| - **Throws `ArgumentNullException` for null paths** | ||
| - **Throws for empty paths** | ||
|
JanProvaznik marked this conversation as resolved.
Outdated
|
||
|
|
||
| #### Note: | ||
| GetFullPath also resolves relative segments so it should be called after absolutization if that behavior is required, and if a method is explicitly fixing directory separators it should be also preserved instead of replacing with GetAbsolutePath directly. | ||
|
JanProvaznik marked this conversation as resolved.
Outdated
|
||
| The goal is MAXIMUM compatibility so think about these edge cases so it behaves the same as before. | ||
|
|
||
| ### Step 3: Replace Environment Variable APIs | ||
|
|
||
| ```csharp | ||
| // BEFORE (UNSAFE) | ||
| string value = Environment.GetEnvironmentVariable("VAR"); | ||
| Environment.SetEnvironmentVariable("VAR", "value"); | ||
|
|
||
| // AFTER (SAFE) | ||
| string value = TaskEnvironment.GetEnvironmentVariable("VAR"); | ||
| TaskEnvironment.SetEnvironmentVariable("VAR", "value"); | ||
| ``` | ||
|
|
||
| ### Step 4: Replace Process Start APIs | ||
|
|
||
| ```csharp | ||
| // BEFORE (UNSAFE - inherits process state) | ||
| var psi = new ProcessStartInfo("tool.exe"); | ||
|
|
||
| // AFTER (SAFE - uses task's isolated environment) | ||
| var psi = TaskEnvironment.GetProcessStartInfo(); | ||
| psi.FileName = "tool.exe"; | ||
| ``` | ||
|
|
||
| ### Step 5: Use Absolute Paths with File APIs | ||
|
|
||
| All file system operations must use absolute paths. The analyzer (MSB9997) will warn on potentially relative paths: | ||
|
|
||
| ```csharp | ||
| // UNSAFE - may resolve relative to wrong directory | ||
| File.Exists(somePath); | ||
|
|
||
| // SAFE - explicitly absolutized | ||
| File.Exists(TaskEnvironment.GetAbsolutePath(somePath)); | ||
|
|
||
| // ALSO SAFE - already absolute (e.g., from FileInfo.FullName) | ||
| File.Exists(fileInfo.FullName); | ||
|
JanProvaznik marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| ## Updating Unit Tests | ||
|
|
||
| **Every test creating a task instance must set TaskEnvironment.** Use `TaskEnvironmentHelper.CreateForTest()`: | ||
|
|
||
| ```csharp | ||
| // BEFORE | ||
| var task = new Copy | ||
| { | ||
| BuildEngine = new MockEngine(true), | ||
| SourceFiles = sourceFiles, | ||
| DestinationFolder = new TaskItem(destFolder), | ||
| }; | ||
|
|
||
| // AFTER | ||
| var task = new Copy | ||
| { | ||
| TaskEnvironment = TaskEnvironmentHelper.CreateForTest(), | ||
| BuildEngine = new MockEngine(true), | ||
| SourceFiles = sourceFiles, | ||
| DestinationFolder = new TaskItem(destFolder), | ||
| }; | ||
| ``` | ||
|
|
||
| ### Testing Exception Cases | ||
|
|
||
| Tasks must handle null/empty path inputs properly. | ||
|
|
||
| ```csharp | ||
| [Fact] | ||
| public void Task_WithNullPath_Throws() | ||
| { | ||
| var task = CreateTask(); | ||
|
|
||
| Should.Throw<ArgumentNullException>(() => task.ProcessPath(null!)); | ||
| } | ||
| ``` | ||
|
|
||
| ## APIs to Avoid | ||
|
|
||
| ### Critical Errors (No Alternative) | ||
| - `Environment.Exit()`, `Environment.FailFast()` - Return false or throw instead | ||
| - `Process.GetCurrentProcess().Kill()` - Never terminate process | ||
| - `ThreadPool.SetMinThreads/MaxThreads` - Process-wide settings | ||
| - `CultureInfo.DefaultThreadCurrentCulture` (setter) - Affects all threads | ||
| - `Console.*` - Interferes with logging | ||
|
|
||
| ### Requires TaskEnvironment | ||
| - `Environment.CurrentDirectory` → `TaskEnvironment.ProjectDirectory` | ||
| - `Environment.GetEnvironmentVariable` → `TaskEnvironment.GetEnvironmentVariable` | ||
| - `Environment.SetEnvironmentVariable` → `TaskEnvironment.SetEnvironmentVariable` | ||
| - `Path.GetFullPath` → `TaskEnvironment.GetAbsolutePath` | ||
| - `Process.Start`, `ProcessStartInfo` → `TaskEnvironment.GetProcessStartInfo` | ||
|
|
||
| ### File APIs Need Absolute Paths | ||
| - `File.*`, `Directory.*`, `FileInfo`, `DirectoryInfo`, `FileStream`, `StreamReader`, `StreamWriter` | ||
| - All path parameters must be absolute | ||
|
|
||
| ### Potential Issues (Review Required) | ||
| - `Assembly.Load*`, `LoadFrom`, `LoadFile` - Version conflicts | ||
| - `Activator.CreateInstance*` - Version conflicts | ||
|
|
||
| ## Practical Notes from Implementation Experience | ||
|
|
||
|
|
||
| ### Null/Empty Path Handling Evolution | ||
|
JanProvaznik marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| ```csharp | ||
| // Current behavior - always throws for null/empty | ||
| TaskEnvironment.GetAbsolutePath(null); // Throws ArgumentNullException | ||
| TaskEnvironment.GetAbsolutePath(string.Empty); // Throws | ||
|
JanProvaznik marked this conversation as resolved.
Outdated
JanProvaznik marked this conversation as resolved.
Outdated
|
||
|
|
||
|
JanProvaznik marked this conversation as resolved.
Outdated
|
||
| // Caller responsibility to validate | ||
| if (!string.IsNullOrEmpty(path)) | ||
| { | ||
| AbsolutePath absolute = TaskEnvironment.GetAbsolutePath(path); | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| ### TaskEnvironment is Not Thread-Safe | ||
|
|
||
| If your task spawns multiple threads internally, you must synchronize access to `TaskEnvironment`. However, each task instance gets its own environment, so no synchronization with other tasks is needed. | ||
|
|
||
| ## Checklist | ||
|
|
||
| - [ ] Task is annotated with `MSBuildMultiThreadableTask` attribute and implements `IMultiThreadableTask` if TaskEnvironment APIs are required | ||
| - [ ] All environment variable access uses `TaskEnvironment` APIs | ||
| - [ ] All process spawning uses `TaskEnvironment.GetProcessStartInfo()` | ||
| - [ ] All file system APIs receive absolute paths | ||
| - [ ] No use of `Environment.CurrentDirectory` | ||
| - [ ] All tests set `TaskEnvironment = TaskEnvironmentHelper.CreateForTest()` | ||
| - [ ] Tests verify exception behavior for null/empty paths | ||
| - [ ] No use of forbidden APIs (Environment.Exit, etc.) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.