Migrate WriteCodeFragment task to use TaskEnvironment API#13169
Migrate WriteCodeFragment task to use TaskEnvironment API#13169JanProvaznik merged 9 commits intomainfrom
Conversation
|
Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo. |
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Migrates WriteCodeFragment to the TaskEnvironment API so relative paths are resolved against the project directory (not process current directory), enabling thread-safe execution in multithreaded MSBuild mode.
Changes:
- Mark
WriteCodeFragmentas thread-safe ([MSBuildMultiThreadableTask]) and implementIMultiThreadableTaskwith aTaskEnvironmentproperty. - Resolve output paths via
TaskEnvironment.GetAbsolutePath()and use the resolved absolute path for directory creation, file writes, and lock diagnostics. - Update
WriteCodeFragmentunit tests to initializeTaskEnvironmentviaTaskEnvironmentHelper.CreateForTest().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Tasks/WriteCodeFragment.cs | Adds multithreadable task support and uses TaskEnvironment to resolve paths before I/O. |
| src/Tasks.UnitTests/WriteCodeFragment_Tests.cs | Updates tests to set TaskEnvironment on task instances. |
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Co-authored-by: Rainer Sigwald <raines@microsoft.com>
|
@copilot create a test for the case when OutputDirectory is relative, OutputFile is not passed in that in that case we receive a relative OutputFile |
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Added test |
|
@copilot Stack trace |
…codefragment-task
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
... Fixed in 5e4d0d4. The |
### Context Thread-safe tasks must avoid global process state (working directory, environment variables). `WriteCodeFragment` uses relative paths resolved against the current working directory, making it unsafe for multithreaded execution. ### Changes Made - Add `[MSBuildMultiThreadableTask]` attribute and implement `IMultiThreadableTask` - Use `TaskEnvironment.GetAbsolutePath()` to resolve paths before file operations: ```csharp outputFilePath = TaskEnvironment.GetAbsolutePath(OutputFile.ItemSpec); ``` - Pass absolute paths to `FileUtilities.GetTemporaryFile()`, `FileUtilities.EnsureDirectoryExists()`, `File.WriteAllText()`, and `LockCheck.GetLockedFileMessage()` - Update all test instantiations to set `TaskEnvironment = TaskEnvironmentHelper.CreateForTest()` - Preserve behavior: when `OutputDirectory` is relative and `OutputFile` is not specified, the resulting `OutputFile` output remains relative (matching original behavior) ### Testing All 52 WriteCodeFragment tests pass, including: - A new test `RelativeOutputDirectoryProducesRelativeOutputFile` that verifies when `OutputDirectory` is relative and `OutputFile` is not passed, the resulting `OutputFile` is also relative - The `CommentIsInvariantCulture` tests (added in main) now have `TaskEnvironment` initialized ### Notes None. <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Migrate the WriteCodeFragment task to use TaskEnvironment API. > Make sure to preserve correctness, performance and that the tests pass </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/dotnet/msbuild/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Jan Provazník <janprovaznik@microsoft.com>
### Context Thread-safe tasks must avoid global process state (working directory, environment variables). `WriteCodeFragment` uses relative paths resolved against the current working directory, making it unsafe for multithreaded execution. ### Changes Made - Add `[MSBuildMultiThreadableTask]` attribute and implement `IMultiThreadableTask` - Use `TaskEnvironment.GetAbsolutePath()` to resolve paths before file operations: ```csharp outputFilePath = TaskEnvironment.GetAbsolutePath(OutputFile.ItemSpec); ``` - Pass absolute paths to `FileUtilities.GetTemporaryFile()`, `FileUtilities.EnsureDirectoryExists()`, `File.WriteAllText()`, and `LockCheck.GetLockedFileMessage()` - Update all test instantiations to set `TaskEnvironment = TaskEnvironmentHelper.CreateForTest()` - Preserve behavior: when `OutputDirectory` is relative and `OutputFile` is not specified, the resulting `OutputFile` output remains relative (matching original behavior) ### Testing All 52 WriteCodeFragment tests pass, including: - A new test `RelativeOutputDirectoryProducesRelativeOutputFile` that verifies when `OutputDirectory` is relative and `OutputFile` is not passed, the resulting `OutputFile` is also relative - The `CommentIsInvariantCulture` tests (added in main) now have `TaskEnvironment` initialized ### Notes None. <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Migrate the WriteCodeFragment task to use TaskEnvironment API. > Make sure to preserve correctness, performance and that the tests pass </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/dotnet/msbuild/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Jan Provazník <janprovaznik@microsoft.com>
Context
Thread-safe tasks must avoid global process state (working directory, environment variables).
WriteCodeFragmentuses relative paths resolved against the current working directory, making it unsafe for multithreaded execution.Changes Made
[MSBuildMultiThreadableTask]attribute and implementIMultiThreadableTaskTaskEnvironment.GetAbsolutePath()to resolve paths before file operations:FileUtilities.GetTemporaryFile(),FileUtilities.EnsureDirectoryExists(),File.WriteAllText(), andLockCheck.GetLockedFileMessage()TaskEnvironment = TaskEnvironmentHelper.CreateForTest()OutputDirectoryis relative andOutputFileis not specified, the resultingOutputFileoutput remains relative (matching original behavior)Testing
All 52 WriteCodeFragment tests pass, including:
RelativeOutputDirectoryProducesRelativeOutputFilethat verifies whenOutputDirectoryis relative andOutputFileis not passed, the resultingOutputFileis also relativeCommentIsInvariantCulturetests (added in main) now haveTaskEnvironmentinitializedNotes
None.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.