Skip to content

Migrate FindAssembliesWithReferencesTo to IMultiThreadableTask - #54748

Merged
OvesN merged 6 commits into
mainfrom
dev/veronikao/migrate-FindAssembliesWithReferencesTo-to-mt
Jun 22, 2026
Merged

OvesN merged 6 commits into
mainfrom
dev/veronikao/migrate-FindAssembliesWithReferencesTo-to-mt

Conversation

@OvesN

@OvesN OvesN commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes dotnet/msbuild#14051

What & why

MSBuild is moving toward running multiple tasks in parallel within a single process. To be safe under that model, a task must not depend on process-global state — in particular the current working directory, which a concurrently running task could change at any time. Any task that resolves relative file paths against the current directory is therefore not safe to run this way.

FindAssembliesWithReferencesTo is the Razor SDK task that figures out which of a project's referenced assemblies depend (directly or transitively) on ASP.NET Core MVC. It does this by opening each referenced assembly file on disk and reading its metadata.

What changed

  • Declares the task thread-safe ([MSBuildMultiThreadableTask] / IMultiThreadableTask) so the MSBuild engine may run it in parallel; it now receives a per-task TaskEnvironment from the engine.
  • Resolves relative assembly paths against the project directory (via TaskEnvironment.GetAbsolutePath) instead of the ambient process working directory. This is the one change needed to make the file access safe under parallel execution.

@OvesN
OvesN requested review from a team and Copilot June 12, 2026 15:29
@OvesN
OvesN marked this pull request as draft June 12, 2026 15:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 migrates the Razor SDK task FindAssembliesWithReferencesTo to MSBuild’s IMultiThreadableTask model (per dotnet/msbuild#14051), flowing TaskEnvironment through to reference scanning so file I/O resolves relative paths correctly under multi-threaded builds.

Changes:

  • Mark FindAssembliesWithReferencesTo as [MSBuildMultiThreadableTask], implement IMultiThreadableTask, and pass TaskEnvironment into the resolver.
  • Update ReferenceResolver to resolve assembly paths via TaskEnvironment.GetAbsolutePath(...) before performing File.Exists / File.OpenRead.
  • Add a new agency.toml configuration file at repo root.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/RazorSdk/Tasks/ReferenceResolver.cs Accepts TaskEnvironment and uses it to absolutize paths; refactors lookup dictionary initialization.
src/RazorSdk/Tasks/FindAssembliesWithReferencesTo.cs Migrates the task to IMultiThreadableTask and flows TaskEnvironment into ReferenceResolver.
agency.toml Introduces an MCP server configuration entry (appears unrelated to the PR’s stated purpose).

Comment thread src/RazorSdk/Tasks/ReferenceResolver.cs Resolved
Comment thread agency.toml Resolved
@OvesN

OvesN commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Expert review — FindAssembliesWithReferencesTo → IMultiThreadableTask

Verdict: approve-with-comments. No bugs; production change is mechanical and conforms to the established [MSBuildMultiThreadableTask] + TaskEnvironment.GetAbsolutePath(...) pattern used by ~20 other migrated tasks. All substantive findings are about the new test file; none should block merge.

Substantive findings

1. Dead helper in test file  ·  cleanup
test/Microsoft.NET.Sdk.Razor.Tests/FindAssembliesWithReferencesToMultiThreadingTest.cs lines 73–74 — private static TaskEnvironment CreateMultiThreadedEnvironment() is never referenced. Looks like a leftover from an earlier iteration. Remove it, or wire it into a real parity test (see #2).

2. Missing multi-process / multi-threaded parity coverage  ·  test gap
Both new tests only exercise the multi-threaded path (TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...) with CWD elsewhere). Neither runs against TaskEnvironment.Fallback. The whole purpose of this migration is "behaves identically in both modes," and the canonical sibling test — test/Microsoft.NET.Build.Tasks.Tests/GivenAGenerateClsidMapMultiThreading.cs — asserts result/error equivalence across both modes precisely so a future regression in GetAbsolutePath semantics is caught. A drop-in of that ItProducesSameErrorsInMultiProcessAndMultiThreadedEnvironments / ItProducesSame…InMultiProcessAndMultiThreadedEnvironments pattern (run twice: once with Fallback + Directory.SetCurrentDirectory(temp.Path), once with the multi-threaded environment + CWD elsewhere, restore CWD in finally, assert outputs identical) would meaningfully harden this PR.

3. Tests never exercise the success path of GetReferences  ·  test gap (low)
Both tests write a 1-byte 0x00 stub, which makes PEReader throw BadImageFormatException; the catch returns an empty list, so the code that reads metadata, mutates Lookup, and classifies assemblies is not covered. Path-resolution wiring is proven (good), but the metadata-read step isn't. The Clsid test handles this by File.Copying typeof(GenerateClsidMap).Assembly.Location into the project dir — same technique works here (copy a real managed assembly, FusionName-tag it, assert it classifies against a chosen TargetAssemblyNames set).

4. Test 2's positive assertions are weak  ·  test gap (low)
Execute_PassesTaskEnvironmentToResolver_AndResolvesRelativeAssemblyItemSpecs asserts Execute() == true, no errors, and no RAZORSDK1007 warning, but never inspects task.ResolvedAssemblies. Combined with #3, the test would still pass if ResolveAssemblies silently returned something wrong as long as it didn't throw. Subsumed by fixing #3 — once a real assembly is in play, assert task.ResolvedAssemblies content.

Verified non-findings (earlier bot comments)

  • "ToDictionary throws on duplicates" — incorrect; the diff keeps foreach { Lookup[item.AssemblyName] = item; } (indexer assignment, last-write-wins) at ReferenceResolver.cs:43-46. No ToDictionary call exists.
  • "New agency.toml at repo root" — incorrect; no such file in the diff or repo. The PR changes exactly the 3 files listed.
  • "No tests for FindAssembliesWithReferencesTo" — was valid for the first two commits; addressed by 80951c48 add tests.
  • Thread safety of ReferenceResolver.Lookup under IMultiThreadableTask — safe. Each FindAssembliesWithReferencesTo.Execute() instantiates its own ReferenceResolver, so the per-GetReferences mutation of Lookup is per-task-instance; concurrent task instances do not share state.

Optional / cosmetic

  • ReferenceResolver.cs:33-36 — if (taskEnvironment != null) { _taskEnvironment = taskEnvironment; } could be _taskEnvironment = taskEnvironment ?? TaskEnvironment.Fallback; to make the default explicit rather than relying on the field initializer. Equally, the dual constructors (2-arg delegating to 3-arg with null) are justified for binary-compat of a public class, but the "null means Fallback" semantic in the 3-arg overload is undocumented — a one-line XML doc would help.
  • FindAssembliesWithReferencesToMultiThreadingTest.cs:6 — using System.Reflection; is unused.

@OvesN
OvesN marked this pull request as ready for review June 15, 2026 10:12

@jankratochvilcz jankratochvilcz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! The main bit for me is the architectural comment

Comment thread src/RazorSdk/Tasks/ReferenceResolver.cs Resolved
@DustinCampbell
DustinCampbell requested a review from chsienki June 17, 2026 23:56
@DustinCampbell

Copy link
Copy Markdown
Member

Adding @chsienki and @davidwengier to review from the Razor Tooling side.

@davidwengier davidwengier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO the asp.net runtime team owns all of this, but it is entirely possible that I'm wrong, or that nobody owns them, but I have no problem approving the PR regardless. Approval does not imply acceptance of ownership. You touched it last :)

@jankratochvilcz

jankratochvilcz commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

@davidwengier The owner seems to be Sam Patel, but his team has no capacity to help with this it seems unfortunately

@jankratochvilcz jankratochvilcz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for making the changes! 👍

Btw. regarding the layering - for me it's less about what we expose publicaly and more about the code layering - if we can keep the resolutions on a single layer (the tasks) it also creates clear expectations for there the absolutization should happen and it becomes harder to make the wrong assumption

@OvesN

OvesN commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for making the changes! 👍

Btw. regarding the layering - for me it's less about what we expose publicaly and more about the code layering - if we can keep the resolutions on a single layer (the tasks) it also creates clear expectations for there the absolutization should happen and it becomes harder to make the wrong assumption

Got it. But I would assume that that helper class was on the task level, it just decoupled the logic from task class. 🤔

@OvesN
OvesN merged commit 2ce4e43 into main Jun 22, 2026
25 checks passed
@OvesN
OvesN deleted the dev/veronikao/migrate-FindAssembliesWithReferencesTo-to-mt branch June 22, 2026 10:28
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 24, 2026
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.

[Multithreaded] Migrate FindAssembliesWithReferencesTo in SDK

5 participants