Skip to content

Migrate GenerateStaticWebAssetEndpointsManifest to IMultiThreadableTask - #54706

Merged
jankratochvilcz merged 14 commits into
dotnet:mainfrom
jankratochvilcz:mt/migrate-generate-static-web-asset-endpoints-manifest
Aug 4, 2026
Merged

jankratochvilcz merged 14 commits into
dotnet:mainfrom
jankratochvilcz:mt/migrate-generate-static-web-asset-endpoints-manifest

Conversation

@jankratochvilcz

@jankratochvilcz jankratochvilcz commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Migrates GenerateStaticWebAssetEndpointsManifest (src/StaticWebAssetsSdk/Tasks/GenerateStaticWebAssetEndpointsManifest.cs) to IMultiThreadableTask so MSBuild''s multithreaded mode bypasses the per-task TaskHost wrapper.

Fixes dotnet/msbuild#14034

Perfstar regression (14d, Windows, paired MT vs non-MT, p50 sum across iterations)

Target non-MT (ms) MT (ms) Δ (ms) Δ (%)
GenerateStaticWebAssetsManifest 753 5,294 +4,541 +603 %

Changes

  • [MSBuildMultiThreadableTask] + implements IMultiThreadableTask with default TaskEnvironment.Fallback.
  • ManifestPath, CacheFilePath, and ExclusionPatternsCacheFilePath are absolutized once at the top of Execute via TaskEnvironment.GetAbsolutePath, guarded by string.IsNullOrEmpty so a null/empty value is passed through unchanged. All File.* calls use the absolutized values; the task''s own log messages still emit the original ManifestPath.

Compatibility sins audit (per migration skill)

  • Sin 1 (output contamination): no [Output] property on this task.
  • Sin 2 (error-message inflation): the task''s own Log.LogMessage("Updating manifest timestamp ''{0}''", ManifestPath) calls still use the original property. The low-importance ArtifactWriter diagnostic now prints the absolute path.
  • Sin 3 (?? swallowing exceptions): no new null-coalescing.
  • Sin 4 (try/catch scope): all absolutized paths are hoisted above the try block and accessible in the catch.
  • Sin 5 (canonicalization): no dictionary/set keyed on these paths.
  • Sin 6 (exception type): GetAbsolutePath("") throws ArgumentException. ManifestPath is [Required]; optional CacheFilePath and ExclusionPatternsCacheFilePath are guarded by string.IsNullOrEmpty before calling GetAbsolutePath.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

@jankratochvilcz

Copy link
Copy Markdown
Contributor Author

Follow-up: deeper call-stack audit (call paths into StaticWebAsset)

Tracing all helpers invoked from Execute(), one transitive call-stack hazard is shared with all SDK tasks that hydrate StaticWebAsset items:

StaticWebAsset.FromTaskItem / FromV1TaskItem → StaticWebAsset.Normalize() (instance method, src/StaticWebAssetsSdk/Tasks/Data/StaticWebAsset.cs:1067) calls:

  • NormalizeContentRootPath(ContentRoot) → Path.GetFullPath(path) (:1080)
  • Path.GetFullPath(RelatedAsset) (:1072)

Both Path.GetFullPath calls go through BCL and resolve against Environment.CurrentDirectory, not MSBuild's MT-safe AsyncLocal FileUtilities.CurrentThreadWorkingDirectory.

Why it doesn't bite in practice today: in production the SDK targets that produce these items populate ContentRoot and RelatedAsset with already-absolute paths (e.g. via MSBuild's %(FullPath) well-known modifier, which is MT-safe). Path.GetFullPath(already_absolute) becomes a pure canonicalization that doesn't read Environment.CurrentDirectory. So this PR does not introduce a regression — the latent hazard predates the migration and is exercised by every existing SWA task that consumes StaticWebAsset items.

Why not fix it here: StaticWebAsset is a data type without a TaskEnvironment reference. Plumbing one in would change a widely-used public signature and is well outside the scope of a per-task attribute / IMultiThreadableTask migration. Should be tracked as a separate follow-up.

@jankratochvilcz
jankratochvilcz force-pushed the mt/migrate-generate-static-web-asset-endpoints-manifest branch 2 times, most recently from 70e2f63 to 78804f7 Compare June 12, 2026 16:11
@jankratochvilcz
jankratochvilcz marked this pull request as ready for review June 16, 2026 17:11
Copilot AI review requested due to automatic review settings June 16, 2026 17:11

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

Migrates GenerateStaticWebAssetEndpointsManifest to MSBuild’s multi-threadable task model (IMultiThreadableTask + [MSBuildMultiThreadableTask]) so multithreaded builds can avoid the per-task TaskHost wrapper and correctly root relative paths against TaskEnvironment.ProjectDirectory.

Changes:

  • Implement IMultiThreadableTask on GenerateStaticWebAssetEndpointsManifest and introduce TaskEnvironment with TaskEnvironment.Fallback.
  • Absolutize ManifestPath/CacheFilePath/ExclusionPatternsCacheFilePath once via TaskEnvironment.GetAbsolutePath and route file I/O through those absolute paths.
  • Add a regression test ensuring relative manifest + exclusion-cache writes are rooted to TaskEnvironment.ProjectDirectory (not the process CWD).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/StaticWebAssetsSdk/Tasks/GenerateStaticWebAssetEndpointsManifest.cs Makes the task multi-threadable and redirects file operations to TaskEnvironment-rooted absolute paths.
test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/GenerateStaticWebAssetEndpointsManifestMultiThreadingTest.cs Adds coverage asserting the task’s outputs do not depend on the process current directory.

Jan Kratochvil and others added 3 commits June 17, 2026 10:50
Marks the task with [MSBuildMultiThreadableTask] and implements IMultiThreadableTask. ManifestPath, CacheFilePath, and ExclusionPatternsCacheFilePath are absolutized once at the top of Execute via TaskEnvironment.GetAbsolutePath and threaded into the helper UpdateExclusionPatternsCache (now static). Log messages still emit the original ManifestPath.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Mirrors the StaticWebAssetsGeneratePackagePropsFileMultiThreadingTest pattern: set process CWD to a decoy directory in a different subtree, run the task with TaskEnvironment bound to a separate project directory, and assert the endpoints manifest + exclusion-patterns cache files are written under TaskEnvironment.ProjectDirectory (not the decoy CWD).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Match the defensive pattern used elsewhere in StaticWebAsset (e.g.,
NormalizeContentRootPath, ResolveFile) — skip absolutization when the
input is null or empty instead of letting GetAbsolutePath throw.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jankratochvilcz
jankratochvilcz force-pushed the mt/migrate-generate-static-web-asset-endpoints-manifest branch from 3a56cb8 to a591136 Compare June 17, 2026 08:50
…d, inline bool variables

- Revert UpdateExclusionPatternsCache from static back to instance method,
  using ExclusionPatternsCacheFilePath property directly for the guard check
- Remove hasCacheFile and hasExclusionCache bool variables, inline the checks

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jankratochvilcz and others added 2 commits June 22, 2026 13:14
…ifestPath

The conditional expression's best common type resolves to 'string' when one
branch is the 'string'-typed ManifestPath, because AbsolutePath defines an
implicit conversion to string but not from string. Assigning that string result
back to the AbsolutePath variable fails with CS0029. Use ': default' (matching
the cacheAbsolutePath/exclusionCacheAbsolutePath lines), which resolves the
conditional type to AbsolutePath.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Treat null/empty/whitespace path inputs uniformly by switching the
GetAbsolutePath guards from IsNullOrEmpty to IsNullOrWhiteSpace, so a
whitespace-only ManifestPath/CacheFilePath/ExclusionPatternsCacheFilePath
never reaches TaskEnvironment.GetAbsolutePath (which throws) nor
File.WriteAllText("") in the exclusion cache path.

Addresses review feedback on dotnet#54706.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jankratochvilcz
jankratochvilcz requested a review from OvesN July 7, 2026 13:28
jankratochvilcz added a commit to jankratochvilcz/sdk that referenced this pull request Jul 23, 2026
Treat null/empty/whitespace path inputs uniformly by switching the
GetAbsolutePath guards from IsNullOrEmpty to IsNullOrWhiteSpace, so a
whitespace-only ManifestPath/CacheFilePath/ExclusionPatternsCacheFilePath
never reaches TaskEnvironment.GetAbsolutePath (which throws) nor
File.WriteAllText("") in the exclusion cache path.

Addresses review feedback on dotnet#54706.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f7d855bc-b3c6-4511-b408-f544667cc54e
Preserve whitespace-only Unix paths and keep manifest diagnostics using the original relative path while file I/O uses the task environment's absolute path. Update the regression tests for MSTest after rebasing onto main.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f7d855bc-b3c6-4511-b408-f544667cc54e
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f7d855bc-b3c6-4511-b408-f544667cc54e
@jankratochvilcz
jankratochvilcz requested a review from a team as a code owner July 23, 2026 08:21
@jankratochvilcz
jankratochvilcz force-pushed the mt/migrate-generate-static-web-asset-endpoints-manifest branch 2 times, most recently from acc3440 to c133a29 Compare July 23, 2026 08:46
@jankratochvilcz

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

public override bool Execute()
{
var absoluteManifestPath = !string.IsNullOrEmpty(ManifestPath) ? TaskEnvironment.GetAbsolutePath(ManifestPath) : (AbsolutePath?)null;
var manifestAbsolutePath = absoluteManifestPath?.Value ?? ManifestPath;

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.

I think we do not need this variable, we can use absoluteManifestPath only but make a default value ManifestPath, not null

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.

And I would rename it to "manifestAbsolutePath" to keep naming consistent

@jankratochvilcz jankratochvilcz Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 4f103fd — went with your version.

There's now a single manifestAbsolutePath string, consistent with the two cache paths, and it falls back to ManifestPath instead of null. The branch around PersistFileIfChanged is gone too.

Two knock-on changes:

  • The AbsolutePath overload I had added to ArtifactWriter is unused now, so I reverted that file — the PR is back to one product file.
  • The low-importance Creating artifact because artifact file '...' message now prints the absolute path. That matches how the rest of the file operations in this task behave, and the task's own Generating manifest file '{0}' messages still use the relative ManifestPath. I updated the MT test assertion accordingly.

No functional change — all GenerateStaticWebAssetEndpointsManifest tests pass locally.

};

this.PersistFileIfChanged(manifest, ManifestPath, StaticWebAssetsJsonSerializerContext.RelaxedEscaping.StaticWebAssetEndpointsManifest);
if (absoluteManifestPath is { } path)

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.

We not need this additional check, if this comment https://github.com/dotnet/sdk/pull/54706/changes#r3645107700 will be resolved

@jankratochvilcz jankratochvilcz Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 4f103fd — the branch is gone, there's a single PersistFileIfChanged call now.

Collapse absoluteManifestPath/manifestAbsolutePath into a single string
variable, consistent with the cache paths, and drop the branch around
PersistFileIfChanged. The ArtifactWriter AbsolutePath overload is no
longer used, so revert that file to its original state; the low-importance
"Creating artifact" message now logs the absolute path, matching how the
other file operations in this task are logged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@jankratochvilcz

Copy link
Copy Markdown
Contributor Author

@OvesN applied both suggestions in 4f103fd — single manifestAbsolutePath string falling back to ManifestPath, and the PersistFileIfChanged branch is gone. The AbsolutePath overload I'd added to ArtifactWriter became unused so I reverted that file; the only visible effect is that the low-importance Creating artifact ... diagnostic now prints the absolute path, and I updated the MT test to match. No functional change, tests pass locally. Ready for another look whenever you have a minute.

Jan Kratochvil and others added 2 commits August 3, 2026 16:08
The multithreading test nested <ClassName>/<guid>/decoy/spawn under the
test binary directory, which pushed Directory.SetCurrentDirectory past
MAX_PATH and threw PathTooLongException on developer machines with a
moderately long repo path (Helix's short C:\h\w paths hid this in CI).
Shorten the scratch layout and add the same parallelization comment the
sibling StaticWebAssets multithreading tests carry.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@jankratochvilcz

Copy link
Copy Markdown
Contributor Author

Refreshed this PR and chased down the red CI (build 1534425).

Why it was red: two unrelated Helix work items failed — dotnet-watch.Tests.dll.2 (macOS) and Microsoft.NET.Sdk.StaticWebAssets.Tests.dll.12 (Windows x64). The only test failure in the SWA work item was StaticWebAssetEndpointsIntegrationTest.Build_DefaultDocumentAndSpaFallback_CreatesAdditionalEndpoints, which asserts on staticwebassets.build.json (written by GenerateStaticWebAssetsManifest) — a file this PR does not touch. The same code was fully green at e22f5d84, and the only commit since was logging-only, so this looks like infrastructure flakiness rather than a regression. main was green across the surrounding builds, and I ran the whole StaticWebAssetEndpointsIntegrationTest + GenerateStaticWebAssetEndpointsManifestTest set locally against a freshly built SDK — 12/12 pass.

What actually needed fixing: the new multithreading test nested <ClassName>/<guid>/decoy/spawn under the test binary directory, which pushed Directory.SetCurrentDirectory past MAX_PATH and threw PathTooLongException on a dev box with a moderately long repo path. Helix's short C:\h\w\... paths hid it in CI. Shortened the scratch layout and added the same "parallelization is disabled assembly-wide" comment the sibling SWA multithreading tests carry.

Also merged current main (the branch was ~140 commits behind; merge was clean). Re-approval needed since the push dismissed the stale reviews — @OvesN @AlesProkop.

@jankratochvilcz
jankratochvilcz enabled auto-merge (squash) August 3, 2026 15:16
@jankratochvilcz

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@jankratochvilcz
jankratochvilcz merged commit 26f715e into dotnet:main Aug 4, 2026
30 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 5, 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 GenerateStaticWebAssetEndpointsManifest in SDK

4 participants