Migrate WriteCodeFragment task to use TaskEnvironment API#13169
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 |
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.