Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/memory/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ For hosted WebAssembly applications, the browser-facing client project owns the
initializer and configuration assets. The launching server consumes those referenced
assets and hosts the forwarding route, while `dotnet watch` reads the client project's
deterministic private-key output. This avoids competing host/client keys.
The [Web SDK](../../src/WebSdk/Web/Targets/Sdk.Server.targets) marks server-owned
browser-tools assets `AssetMode=Local`, which the shared
[reference-export predicate](../../src/StaticWebAssetsSdk/Tasks/Data/StaticAssetsManifest.cs)
excludes regardless of project mode. WebAssembly-owned browser-tools assets keep `All`
so the hosted client's server can consume them.

The [WebAssembly SDK](../../src/WasmSdk/Sdk/Sdk.targets) and the
[Web SDK](../../src/WebSdk/Web/Targets/Sdk.Server.targets) opt in by naming their asset
Expand Down
15 changes: 13 additions & 2 deletions src/StaticWebAssetsSdk/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ The generated initializer checks the provider-owned
configuration; this response is not a generated file or Static Web Asset.
Hosted WebAssembly's client owns these outputs; its server consumes the referenced assets
rather than generating a competing route and keypair. The assets are build only and must
never reach publish output. Its generated watch initializer also initializes the SDK Hot
never reach publish output. Server Web SDK browser-tools assets use `AssetMode=Local`:
they remain available to their own project but are never exported through project
references, even in `Root` project mode. WebAssembly-owned browser-tools assets retain
`AssetMode=All` so a hosted client's assets reach its server. Use the shared manifest-mode
reference filter rather than adding browser-tools-specific export filtering.
Its generated watch initializer also initializes the SDK Hot
Reload agent on .NET 10+; older target frameworks use the runtime's apply API. Do not add a
second Hot Reload agent initializer—the shared browser-tools rendezvous carries the SDK
agent's apply functions directly.
Expand All @@ -67,6 +72,12 @@ These invariants must hold at all times. Violating any of them is a bug.
- **Referential integrity**: Every non-primary asset (`Related` or `Alternative`) must point to a valid asset definition via `RelatedAsset`. If an asset is removed, all assets that reference it must also be removed.
- **Endpoint validity**: Every `StaticWebAssetEndpoint` must point to an existing `StaticWebAsset` via `AssetFile`. There must be no endpoints referencing non-existent assets.
- **Endpoint uniqueness**: Endpoints are unique by the `(Route, AssetFile)` pair. No two endpoints share the same route and asset file combination.
- **Local visibility**: Checked-in compressed alternatives of `Local` primaries must also
be `Local`. Do not export discovery patterns whose physical root contains `Local`
files: runtime discovery has no exclusion list. Shared files retain explicit manifest
entries, but newly added shared files in affected roots require a rebuild. Keep the
owning project's discovery and unrelated roots unchanged. See
[project boundaries](Architecture.md#4-how-assets-cross-project-boundaries).

**Required properties (every asset must have)**

Expand All @@ -79,7 +90,7 @@ These invariants must hold at all times. Violating any of them is a bug.

- `SourceType` ∈ {`Discovered`, `Computed`, `Project`, `Package`, `Framework`}.
- `AssetKind` ∈ {`Build`, `Publish`, `All`}.
- `AssetMode` ∈ {`All`, `CurrentProject`, `Reference`}.
- `AssetMode` ∈ {`All`, `CurrentProject`, `Reference`, `Local`}.
- `AssetRole` ∈ {`Primary`, `Related`, `Alternative`}.

**Conditional requirements**
Expand Down
29 changes: 27 additions & 2 deletions src/StaticWebAssetsSdk/Architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ outputs without null entries, as in

- `SourceType` ∈ {`Discovered`, `Computed`, `Project`, `Package`, `Framework`}.
- `AssetKind` ∈ {`Build`, `Publish`, `All`}.
- `AssetMode` ∈ {`All`, `CurrentProject`, `Reference`}.
- `AssetMode` ∈ {`All`, `CurrentProject`, `Reference`, `Local`}.
- `AssetRole` ∈ {`Primary`, `Related`, `Alternative`}.

**Conditional requirements**
Expand Down Expand Up @@ -101,7 +101,7 @@ The only supported token is `fingerprint` (base-36 SHA-256).
| Property | Type | Description | Invariants |
|----------|------|-------------|------------|
| `AssetKind` | `string` | When the asset is active. | `Build`, `Publish`, or `All`. Defaults to `All` unless `CopyToPublishDirectory` is `Never`, in which case defaults to `Build`. |
| `AssetMode` | `string` | Visibility scope. | `All` (current project + references), `CurrentProject` (owning project only), `Reference` (referencing projects only). Defaults to `All`. |
| `AssetMode` | `string` | Visibility scope. | `All` (current project + references), `CurrentProject` (current-project assets, also exported by `Root` projects), `Reference` (exported by `Default` projects, excluded from `Root` projects' local output and exports), `Local` (available locally, never exported through project references, independent of project mode). Defaults to `All`. |
| `AssetRole` | `string` | Relationship to other assets. | `Primary`, `Related`, or `Alternative`. Defaults to `Primary`. |
| `RelatedAsset` | `string` | Full path to the asset this one is associated with. Forms a chain: an `Alternative` can point to a `Related` asset, which in turn points to a `Primary`. | Required when `AssetRole` is `Related` or `Alternative`. Must reference a valid asset `Identity` in the pipeline. Empty for `Primary`. |
| `AssetTraitName` | `string` | Name of the trait that distinguishes an alternative (e.g., `Content-Encoding`). | Required when `AssetRole` is `Alternative`. |
Expand Down Expand Up @@ -252,6 +252,31 @@ The MSBuild property `StaticWebAssetProjectMode` controls how a project particip

The mode is stored in the build manifest's `Mode` field and used at manifest-read time to decide which assets to include in the current project vs. expose as references.

`AssetMode=Local` is independent of project mode. The shared
[`ManifestModes.ShouldIncludeAssetAsReference`](Tasks/Data/StaticAssetsManifest.cs)
predicate excludes it from both build and publish project-reference exports.
Local assets still participate in the owning project's development, endpoint and publish
pipelines according to their `AssetKind` and copy metadata. Compressed alternatives inherit
the mode, and reference endpoints are projected from the retained assets by
[`ComputeEndpointsForReferenceStaticWebAssets`](Tasks/ComputeEndpointsForReferenceStaticWebAssets.cs).
[`DiscoverPrecompressedAssets`](Tasks/Compression/DiscoverPrecompressedAssets.cs) also
marks checked-in gzip and Brotli alternatives `Local` when their primary is `Local`,
even if the compressed file has the default `All` mode. It preserves those files'
original item identities so reference publish cleanup also removes their `Content`
items instead of copying them outside the static web assets pipeline.

Development discovery patterns cannot express per-file exclusions.
[`ComputeReferenceStaticWebAssetItems`](Tasks/ComputeReferenceStaticWebAssetItems.cs)
therefore omits exported patterns whose physical content root contains a `Local` file.
Shared files already included in the manifest remain available, including edits to those
files; newly added shared files in those roots require a rebuild. The owning project's
discovery patterns and exports from unrelated roots are unchanged. Generated browser-tools
files live outside `wwwroot` and do not disable its discovery.

Server Web SDK Hot Reload browser-tools assets use this mode to avoid exporting
project-specific initializers and configurations. WebAssembly browser-tools assets keep `All`
because a hosted client's server must consume them.

### Discovered — Current Project

Files in the current project's `wwwroot` directory. Scanned by `ResolveProjectStaticWebAssets`. These are the simplest assets: `SourceType=Discovered`, `SourceId` is the current project's package ID, `ContentRoot` is `wwwroot/`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Microsoft.DotNet.HotReload.BrowserToolsProtocol.RoutePrefix. -->
<HotReloadBrowserToolsRoutePrefix Condition="'$(HotReloadBrowserToolsRoutePrefix)' == ''">/_framework/dotnet-browser-tools</HotReloadBrowserToolsRoutePrefix>
<HotReloadBrowserToolsSettingsPath Condition="'$(HotReloadBrowserToolsSettingsPath)' == ''">$(HotReloadBrowserToolsRoutePrefix)/hot-reload-settings.json</HotReloadBrowserToolsSettingsPath>
<HotReloadBrowserToolsAssetMode Condition="'$(HotReloadBrowserToolsAssetMode)' == ''">All</HotReloadBrowserToolsAssetMode>

<!-- Imported after the opting-in SDK sets its browser tools properties so this is available during evaluation. -->
<HotReloadBrowserToolsEnabled>false</HotReloadBrowserToolsEnabled>
Expand Down Expand Up @@ -168,6 +169,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ContentRoot="$(_HotReloadBrowserToolsIntermediatePath)"
BasePath="$(StaticWebAssetBasePath)"
AssetKind="Build"
AssetMode="$(HotReloadBrowserToolsAssetMode)"
CopyToPublishDirectory="Never"
AssetTraitName="JSModule"
AssetTraitValue="JSLibraryModule"
Expand Down Expand Up @@ -200,6 +202,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ContentRoot="$(_HotReloadBrowserToolsIntermediatePath)"
BasePath="$(StaticWebAssetBasePath)"
AssetKind="Build"
AssetMode="$(HotReloadBrowserToolsAssetMode)"
CopyToPublishDirectory="Never">
<Output TaskParameter="Assets" ItemName="StaticWebAsset" />
<Output TaskParameter="Assets" ItemName="_HotReloadBrowserToolsClientAsset" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ Copyright (c) .NET Foundation. All rights reserved.
* "BasePath": The base path where the asset is exposed.
* "RelativePath": The relative path for the asset.
* "AssetKind": The asset kind ('Build', 'Publish' or 'All') This is inferred from CopyToOutputDirectory/CopyToPublishDirectory
* "AssetMode": The asset mode ('CurrentProject', 'Reference', 'All')
* "AssetMode": The asset mode ('CurrentProject', 'Reference', 'All', 'Local').
'Local' assets are available locally but never exported through project references,
independent of StaticWebAssetProjectMode.
* "AssetRole": The asset role ('Primary', 'Related', 'Alternative')
* "AssetMergeSource": The source of the asset when it is being embedded in another TFM.
* "AssetMergeBehavior": The merge behavior when an asset conflicts with another asset in the same TFM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,18 @@ private static void UpdateCompressedAsset(StaticWebAsset asset, StaticWebAsset r
var relativePath = relatedAsset.EmbedTokens(relatedAsset.RelativePath);

asset.RelativePath = $"{relativePath}{fileExtension}";
asset.OriginalItemSpec = relatedAsset.Identity;
asset.RelatedAsset = relatedAsset.Identity;
asset.AssetRole = "Alternative";
asset.AssetTraitName = "Content-Encoding";
asset.AssetTraitValue = assetTraitValue;
if (relatedAsset.IsLocal())
{
// Preserve the compressed file's original item so reference publish cleanup removes its Content item too.
asset.AssetMode = StaticWebAsset.AssetModes.Local;
}
else
{
asset.OriginalItemSpec = relatedAsset.Identity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System.Globalization;
using Microsoft.AspNetCore.StaticWebAssets.Tasks.Utils;
using Microsoft.Build.Framework;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
Expand Down Expand Up @@ -115,6 +116,7 @@ public override bool Execute()
var patterns = new List<StaticWebAssetsDiscoveryPattern>();
if (Patterns != null)
{
var localAssetDirectories = GetLocalAssetDirectories();
foreach (var pattern in Patterns)
{
if (!StaticWebAssetsDiscoveryPattern.HasSourceId(pattern, Source))
Expand All @@ -123,8 +125,18 @@ public override bool Execute()
}
else
{
var discoveryPattern = StaticWebAssetsDiscoveryPattern.FromTaskItem(pattern);
var contentRoot = StaticWebAsset.NormalizeContentRootPath(discoveryPattern.ContentRoot, TaskEnvironment);
if (localAssetDirectories.Contains(contentRoot))
{
// Runtime discovery patterns cannot express exclusions. Shared files still have explicit asset entries.
Log.LogMessage(MessageImportance.Low,
"Skipping pattern '{0}' because its content root contains Local assets.", discoveryPattern.Name);
continue;
}

Log.LogMessage(MessageImportance.Low, "Including pattern '{0}' because is defined in the current project.", pattern.ToString());
patterns.Add(StaticWebAssetsDiscoveryPattern.FromTaskItem(pattern));
patterns.Add(discoveryPattern);
}
}
}
Expand All @@ -140,6 +152,26 @@ public override bool Execute()
return !Log.HasLoggedErrors;
}

private HashSet<string> GetLocalAssetDirectories()
{
var directories = new HashSet<string>(OSPath.PathComparer);
foreach (var asset in StaticWebAsset.FromTaskItemGroup(Assets, TaskEnvironment))
{
if (!asset.HasSourceId(Source) || !asset.IsLocal())
{
continue;
}

var directory = Path.GetDirectoryName(asset.Identity);
while (directory != null &&
directories.Add(StaticWebAsset.NormalizeContentRootPath(directory, TaskEnvironment)))
{
directory = Path.GetDirectoryName(directory);
}
}
return directories;
}

private StaticWebAssetGlobMatcher CreateFrameworkMatcher()
{
if (string.IsNullOrEmpty(FrameworkPattern))
Expand Down
5 changes: 3 additions & 2 deletions src/StaticWebAssetsSdk/Tasks/Data/StaticAssetsManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ internal static bool IsRoot(string projectMode) =>
internal static bool ShouldIncludeAssetInCurrentProject(StaticWebAsset asset, string projectMode) => IsRoot(projectMode) && !asset.IsForReferencedProjectsOnly();

internal static bool ShouldIncludeAssetAsReference(StaticWebAsset asset, string projectMode) =>
IsRoot(projectMode) && !asset.IsForReferencedProjectsOnly() ||
IsDefault(projectMode) && !asset.IsForCurrentProjectOnly();
!asset.IsLocal() &&
Comment thread
javiercn marked this conversation as resolved.
Resolved
(IsRoot(projectMode) && !asset.IsForReferencedProjectsOnly() ||
IsDefault(projectMode) && !asset.IsForCurrentProjectOnly());
Comment thread
javiercn marked this conversation as resolved.
Resolved

}

Expand Down
5 changes: 5 additions & 0 deletions src/StaticWebAssetsSdk/Tasks/Data/StaticWebAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ public void Validate()
case AssetModes.All:
case AssetModes.CurrentProject:
case AssetModes.Reference:
case AssetModes.Local:
break;
default:
throw new InvalidOperationException($"Unknown Asset mode '{AssetMode}' for '{Identity}'.");
Expand Down Expand Up @@ -1179,6 +1180,9 @@ public bool IsBuildAndPublish()
public bool IsForCurrentProjectOnly()
=> string.Equals(AssetMode, AssetModes.CurrentProject, StringComparison.Ordinal);

public bool IsLocal()
=> string.Equals(AssetMode, AssetModes.Local, StringComparison.Ordinal);

public bool IsForReferencedProjectsOnly()
=> string.Equals(AssetMode, AssetModes.Reference, StringComparison.Ordinal);

Expand Down Expand Up @@ -1462,6 +1466,7 @@ public bool Equals(StaticWebAsset other) =>

public static class AssetModes
{
public const string Local = nameof(Local);
public const string CurrentProject = nameof(CurrentProject);
public const string Reference = nameof(Reference);
public const string All = nameof(All);
Expand Down
1 change: 1 addition & 0 deletions src/WebSdk/Web/Targets/Sdk.Server.targets
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<HotReloadBrowserToolsAssetPrefix>Microsoft.NET.Sdk.BlazorWeb.HotReload</HotReloadBrowserToolsAssetPrefix>
<HotReloadBrowserToolsInitializerFile>$(MSBuildThisFileDirectory)HotReload\Microsoft.NET.Sdk.BlazorWeb.HotReload.lib.module.js</HotReloadBrowserToolsInitializerFile>
<HotReloadBrowserToolsRegisterInitializerAsJSModule>true</HotReloadBrowserToolsRegisterInitializerAsJSModule>
<HotReloadBrowserToolsAssetMode>Local</HotReloadBrowserToolsAssetMode>
</PropertyGroup>

<Import Sdk="Microsoft.NET.Sdk.StaticWebAssets"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.BrowserTools.Config.js.gz",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Alternative",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand All @@ -150,7 +150,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.BrowserTools.js.gz",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Alternative",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand All @@ -174,7 +174,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.lib.module.js.gz",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Alternative",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand Down Expand Up @@ -438,7 +438,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.BrowserTools.Config.js",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Primary",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand All @@ -462,7 +462,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.BrowserTools.js",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Primary",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand All @@ -486,7 +486,7 @@
"BasePath": "/",
"RelativePath": "_framework/Microsoft.NET.Sdk.BlazorWeb.HotReload.lib.module.js",
"AssetKind": "Build",
"AssetMode": "All",
"AssetMode": "Local",
"AssetRole": "Primary",
"AssetMergeBehavior": "",
"AssetMergeSource": "",
Expand Down
Loading
Loading