Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

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:
    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.

Original prompt

Migrate the WriteCodeFragment task to use TaskEnvironment API.
Make sure to preserve correctness, performance and that the tests pass


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@dotnet-policy-service
Copy link
Contributor

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>
Copilot AI changed the title [WIP] Migrate WriteCodeFragment task to use TaskEnvironment API Migrate WriteCodeFragment task to use TaskEnvironment API Feb 2, 2026
Copilot AI requested a review from JanProvaznik February 2, 2026 11:54
Copy link
Member

@JanProvaznik JanProvaznik left a comment

Choose a reason for hiding this comment

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

Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
@JanProvaznik JanProvaznik marked this pull request as ready for review February 4, 2026 16:19
Copilot AI review requested due to automatic review settings February 4, 2026 16:19
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

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 WriteCodeFragment as thread-safe ([MSBuildMultiThreadableTask]) and implement IMultiThreadableTask with a TaskEnvironment property.
  • Resolve output paths via TaskEnvironment.GetAbsolutePath() and use the resolved absolute path for directory creation, file writes, and lock diagnostics.
  • Update WriteCodeFragment unit tests to initialize TaskEnvironment via TaskEnvironmentHelper.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>
JanProvaznik and others added 2 commits February 6, 2026 16:56
@JanProvaznik
Copy link
Member

@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>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

@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

Added test RelativeOutputDirectoryProducesRelativeOutputFile in fba0cca. The test verifies that when OutputDirectory is relative and OutputFile is not passed, the resulting OutputFile is also relative.

@JanProvaznik
Copy link
Member

@copilot
System.NullReferenceException : Object reference not set to an instance of an object.

Stack trace
at Microsoft.Build.Tasks.WriteCodeFragment.Execute() in /_/src/Tasks/WriteCodeFragment.cs:line 124
at Microsoft.Build.UnitTests.WriteCodeFragment_Tests.CommentIsInvariantCulture(String cultureName) in D:\a\1\s\src\Tasks.UnitTests\WriteCodeFragment_Tests.cs:line 1306

Copilot AI and others added 2 commits February 9, 2026 09:25
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 9, 2026

@copilot
System.NullReferenceException : Object reference not set to an instance of an object.

...

Fixed in 5e4d0d4. The CommentIsInvariantCulture test from main was missing TaskEnvironment = TaskEnvironmentHelper.CreateForTest(). After merging main and adding the required TaskEnvironment initialization, all 52 WriteCodeFragment tests now pass.

Copilot AI requested a review from JanProvaznik February 9, 2026 09:30
@JanProvaznik JanProvaznik enabled auto-merge (squash) February 9, 2026 09:32
@JanProvaznik JanProvaznik disabled auto-merge February 9, 2026 09:32
@JanProvaznik JanProvaznik enabled auto-merge (squash) February 9, 2026 09:32
@JanProvaznik JanProvaznik merged commit f3a5f8b into main Feb 9, 2026
10 checks passed
@JanProvaznik JanProvaznik deleted the copilot/migrate-writecodefragment-task branch February 9, 2026 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants