Skip to content

Expose public API for TaskEnvironment construction#13383

Merged
AR-May merged 9 commits intodotnet:mainfrom
AR-May:expose-default-task-environment
Mar 30, 2026
Merged

Expose public API for TaskEnvironment construction#13383
AR-May merged 9 commits intodotnet:mainfrom
AR-May:expose-default-task-environment

Conversation

@AR-May
Copy link
Copy Markdown
Member

@AR-May AR-May commented Mar 13, 2026

Context

With the discovery of the "direct instantiation of the task" use case, tasks must explicitly set a default task environment in order to work correctly. Task authors may encounter the same issue, without having benefit of having framework internals visible to them and thus without ability to create one without re-implementing the driver themselves. So, we need to provide a public API for TaskEnvironment construction to enable setting the default task environment. Would be helpful for sdk as well.

Changes Made

Adds two public API members to TaskEnvironment so that task authors using IMultiThreadableTask can obtain or create a TaskEnvironment without depending on internal types:

  • TaskEnvironment.Default: static singleton that delegates to the process-level environment (multi-process mode).
  • TaskEnvironment.CreateMultiThreaded(projectDirectory, environmentVariables?): factory method that creates an isolated environment for multithreaded execution mode.

Testing

Unit tests

Note

Depends on #13380

Copilot AI review requested due to automatic review settings March 13, 2026 15:25
Copy link
Copy Markdown
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 aims to make Microsoft.Build.Framework.TaskEnvironment directly instantiable by task authors by adding a public parameterless constructor that selects a default environment driver.

Changes:

  • Add a public TaskEnvironment() constructor intended to default to the multi-process driver.

@AR-May AR-May changed the title Expose public constructor for TaskEnvironment. Expose public API for TaskEnvironment construction Mar 24, 2026
@AR-May
Copy link
Copy Markdown
Member Author

AR-May commented Mar 24, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@rainersigwald
Copy link
Copy Markdown
Member

  • TaskEnvironment.CreateMultithreaded(projectDirectory, environmentVariables?): factory method that creates an isolated environment for multithreaded execution mode.

Can you elaborate on the use case for this?

Copy link
Copy Markdown
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

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

Should we adjust our internal uses to this?

-         public TaskEnvironment TaskEnvironment { get; set; } = new TaskEnvironment(MultiProcessTaskEnvironmentDriver.Instance);
+         public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Default;

?

@AR-May
Copy link
Copy Markdown
Member Author

AR-May commented Mar 25, 2026

Should we adjust our internal uses to this?

-         public TaskEnvironment TaskEnvironment { get; set; } = new TaskEnvironment(MultiProcessTaskEnvironmentDriver.Instance);
+         public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Default;

?

Yes, for sure, was going to make it in a separate PR as did not want to mix public API change with refactoring.

@AR-May
Copy link
Copy Markdown
Member Author

AR-May commented Mar 25, 2026

  • TaskEnvironment.CreateMultithreaded(projectDirectory, environmentVariables?): factory method that creates an isolated environment for multithreaded execution mode.

Can you elaborate on the use case for this?

The same use as we planning to add soon in msbuild repo - in tests. Allowing customers test the isolation of the task like we plan in #13373.

@AR-May AR-May enabled auto-merge (squash) March 30, 2026 09:31
@AR-May AR-May merged commit 9ff77d7 into dotnet:main Mar 30, 2026
10 checks passed
dfederm pushed a commit to dfederm/msbuild that referenced this pull request Apr 9, 2026
### Context
With the discovery of the "direct instantiation of the task" use case,
tasks must explicitly set a default task environment in order to work
correctly. Task authors may encounter the same issue, without having
benefit of having framework internals visible to them and thus without
ability to create one without re-implementing the driver themselves. So,
we need to provide a public API for `TaskEnvironment` construction to
enable setting the default task environment. Would be helpful for sdk as
well.

### Changes Made
Adds two public API members to `TaskEnvironment` so that task authors
using `IMultiThreadableTask` can obtain or create a `TaskEnvironment`
without depending on internal types:

- `TaskEnvironment.Default`: static singleton that delegates to the
process-level environment (multi-process mode).
- `TaskEnvironment.CreateMultiThreaded(projectDirectory,
environmentVariables?)`: factory method that creates an isolated
environment for multithreaded execution mode.

### Testing
Unit tests

### Note
Depends on dotnet#13380
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.

5 participants