Repository navigation
Migrate ResolveTargetingPackAssets to IMultiThreadableTask - #53944
Conversation
There was a problem hiding this comment.
Pull request overview
Migrates ResolveTargetingPackAssets to participate in MSBuild’s multi-threaded task execution model by implementing IMultiThreadableTask and introducing TaskEnvironment-based behavior, with accompanying unit test updates/additions.
Changes:
- Mark
ResolveTargetingPackAssetsas[MSBuildMultiThreadableTask]and implementIMultiThreadableTaskwith aTaskEnvironmentproperty. - Make targeting-pack caching behavior depend on
TaskEnvironment(instead of a static, process-environment lookup) and thread the decision through resolve helpers. - Update/add unit tests to set/exercise
TaskEnvironmentand validate basic concurrency/caching behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Tasks/Microsoft.NET.Build.Tasks/ResolveTargetingPackAssets.cs | Implements IMultiThreadableTask, introduces TaskEnvironment, and refactors caching toggle/calls to be task-environment driven. |
| src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolveTargetingPackAssetsTask.cs | Updates task initialization to set TaskEnvironment; adjusts hashing test to ignore TaskEnvironment. |
| src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolveTargetingPackAssetsMultiThreading.cs | Adds new tests covering TaskEnvironment-controlled caching and basic concurrent execution behavior. |
Comments suppressed due to low confidence (1)
src/Tasks/Microsoft.NET.Build.Tasks/ResolveTargetingPackAssets.cs:331
AddItemsFromFrameworkListpopulatesanalyzerItems/referenceItemsfrom a cachedFrameworkList, but then continues to load and parseFrameworkList.xmlanyway. This defeats the purpose of caching and may also re-register the same task object / add duplicates when a framework-list cache hit occurs without an overall cache hit. After a cache hit, return early to avoid extra I/O and duplicate work.
if (allowCacheLookup &&
buildEngine4?.GetRegisteredTaskObject(
frameworkListKey,
RegisteredTaskObjectLifetime.AppDomain)
is FrameworkList cacheEntry)
{
// As above, we are not even checking timestamps here
// and instead assuming that the targeting pack folder
// is fully immutable.
analyzerItems.AddRange(cacheEntry.Analyzers);
referenceItems.AddRange(cacheEntry.References);
}
XDocument frameworkListDoc = XDocument.Load(definition.FrameworkListPath);
|
Friendly ping — the |
faa6746 to
02af6e6
Compare
Replace static Environment.GetEnvironmentVariable with instance-level TaskEnvironment.GetEnvironmentVariable for thread-safe environment variable access. Add [MSBuildMultiThreadableTask] attribute and implement IMultiThreadableTask interface. Thread allowCacheLookup parameter through static helper methods. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
c6ccf8f to
d3d7dbe
Compare
OvesN
left a comment
There was a problem hiding this comment.
So I think after deleting of unnecessary lock and potentially addressing this comment #53944 (comment) it is good to go.
|
/review |
ViktorHofer
left a comment
There was a problem hiding this comment.
Expert MSBuild review — IMultiThreadableTask migration of ResolveTargetingPackAssets
Reviewed the 5-commit range across ResolveTargetingPackAssets.cs and GivenAResolveTargetingPackAssetsTask.cs against the 24 review dimensions plus the multithreaded-task-migration "6 deadly compatibility sins" checklist. I traced the full post-migration source and the supporting framework types (AbsolutePath, both TaskEnvironment drivers).
The migration itself is faithful and well-tested:
Value/OriginalValuesplit is applied correctly — file I/O uses absoluteValue(Directory.Exists/GetFiles,File.Exists),[Output]itemspecs use relativeOriginalValue. No output contamination (Sin 1) or error-message inflation (Sin 2).default(AbsolutePath).Value == null, so emptyPathmetadata preserves the missing-targeting-pack error branch bit-for-bit — no NRE, no swallowed exception (Sins 3 & 6 clean).- No canonicalization regression (Sin 5): pre-migration code never called
Path.GetFullPatheither, so cache/dedup keys see the same non-canonical strings. - Per-key locking is deadlock-free: lock order is always top-level → framework-list (a leaf), key namespaces are disjoint by class-name prefix, so no cycle is constructible.
- Moving
allowCacheLookupfrom a process-staticfield to a per-ExecuteTaskEnvironment.GetEnvironmentVariableread actually fixes a latent multithreading bug. - The shared-
TaskItem[]-on-cache-hit behavior is pre-existing (AppDomain cache + node reuse), not introduced here.
| # | Dimension | Verdict |
|---|---|---|
| 3 | Performance & Allocation | 🟢 1 NIT (s_cacheLocks unbounded growth) |
| 10 / 20 | Design / Scope | 🟡 1 MODERATE (RuntimeFramework.CacheKey rewrite — unrelated cache-correctness change) |
✅ 21/24 dimensions clean — all 6 compatibility sins clean, concurrency clean, cache-key isolation test-proven.
- Scope — call out (or split) the
RuntimeFramework.CacheKeychange - Performance (NIT) — note intentional process-lifetime growth of
s_cacheLocks
Verdict: COMMENT — nothing blocks merge. The only substantive ask is to surface the RuntimeFramework.CacheKey change so it gets reviewed on its own merits.
Copilot analysis of an allocation regression when inserting .NET SDK 11 into Visual Studio and building OrchardCore pointed to a 43× increase in `Slot[TaskItem][]` allocations in `ResolveTargetingPackAssets`. The cache key introduced in dotnet#53944 added _all_ metadata to the cache identity. That includes the computed metadatum `DefiningProjectFullPath` that's distinct for every project, which caused spurious cache invalidation. Just reverting it didn't look quite right as another possibly-relevant metadatum was added in dotnet#39402. Added that and put comments in both places to hopefully avoid future drift.
Copilot analysis of an allocation regression when inserting .NET SDK 11 into Visual Studio and building OrchardCore pointed to a 43× increase in `Slot[TaskItem][]` allocations in `ResolveTargetingPackAssets`. The cache key change in #53944 added _all_ metadata to the cache identity. That includes the computed metadatum `DefiningProjectFullPath` that's distinct for every project, which caused spurious cache invalidation. Just reverting it didn't look quite right as another possibly-relevant metadatum was added in #39402. Added that and put comments in both places to hopefully avoid future drift.
Fixes dotnet/msbuild#13776
Migrate ResolveTargetingPackAssets to IMultiThreadableTask
Migrates
ResolveTargetingPackAssetsto support multi-threaded MSBuild execution.Why
From OrchardCore build profiling (see gist):
Supersedes the earlier exploratory branch at SimaTian#49.
What changed
[MSBuildMultiThreadableTask]and implementsIMultiThreadableTaskwithTaskEnvironmentproperty.TaskEnvironment.GetAbsolutePath+AbsolutePath.Value.AbsolutePath.OriginalValue.RuntimeFramework.CacheKeyto hash all item metadata (sorted), not just Name/FrameworkName/Version. The old key was incomplete vs the cached output (rf.Item); the multithreaded cache lifetime makes that latent collision more reachableTests
GivenAResolveTargetingPackAssetsMultiThreading.csbehavioural tests: decoy-CWD resolution, multi-process parity, output relativity, concurrent execution.GivenAResolveTargetingPackAssetsTask.csupdated:InitializeTask()now setsTaskEnvironmentfor correctness.Review status
Internally reviewed via 3-round playbook (merge-group-review → expert-review-msbuild-migration → opus-4.7 adversarial). All rounds PASS after test-strengthening fixes.
Depends on polyfills infrastructure (already in
mainvia #52909).