Skip to content

Add analyzer to steer task authors toward constructor-based TaskEnvironment injection #14378

Closed

Description

@baronfel

Background

PR #14315 adds support for injecting TaskEnvironment into task constructors, so tasks can compute environment-dependent property defaults during construction (something the property-set path cannot do, because property initializers run before the engine assigns the TaskEnvironment property).

During review, @jankratochvilcz suggested discouraging the older property-set pattern in favor of the new constructor-injection path. Marking IMultiThreadableTask.TaskEnvironment [Obsolete]/[Deprecated] outright is not viable — it is still the primary supported API, and many 1P and 3P builds compile with /warnAsError, so the resulting warnings would be a hard build break for every task that sets it (ToolTask and all built-in tasks). An analyzer is a better, opt-in fit and was deliberately kept out of #14315 to avoid inflating its scope.

Proposal

Add a Roslyn analyzer that nudges task authors toward constructor-based TaskEnvironment injection:

  • Surface an informational/warning diagnostic (green squiggle) when a task consumes TaskEnvironment only via the post-construction property-set path where constructor injection would be preferable.
  • Ensure it does not fire on existing built-in tasks in a way that breaks /warnAsError builds (ship as an opt-in/info-severity diagnostic, or scope it appropriately).
  • Include analyzer unit tests and documentation for the recommended pattern.

Notes

Activity

  1. self-assigned this
    on Jul 15, 2026
  2. added a commit that references this issue on Jul 20, 2026
    eae5402
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions