Skip to content

Conversation

@maraf
Copy link
Member

@maraf maraf commented Oct 22, 2025

Use WasmFilesToIncludeInFileSystem items to automatically load files to VFS when the application is starting.

The identity of WasmFilesToIncludeInFileSystem needs to be a relative path to files a file in wwwroot (either physical or linked). The optional TargetPath metadata allows to modify file location in VFS. If the metadata is omitted, a default location will resolved from relative file path in wwwroot.

Example:

  • Having a wwwroot/file.txt file
    • <WasmFilesToIncludeInFileSystem Include="file.txt" /> will load the file to /file.txt
    • <WasmFilesToIncludeInFileSystem Include="file.txt" TargetPath="/mydata/file.txt" /> will load the file to /mydata/file.txt
  • Having a wwwroot/subdir/file.txt file
    • <WasmFilesToIncludeInFileSystem Include="subdir/file.txt" /> will load the file to /subdir/file.txt
    • <WasmFilesToIncludeInFileSystem Include="subdir/file.txt" TargetPath="/mydata/file.txt" /> will load the file to /mydata/file.txt

Fixes #99223

@maraf maraf added this to the 11.0.0 milestone Oct 22, 2025
@maraf maraf self-assigned this Oct 22, 2025
@maraf maraf added arch-wasm WebAssembly architecture area-Build-mono os-browser Browser variant of arch-wasm labels Oct 22, 2025
</ItemGroup>

<ItemGroup>
<WasmFilesToIncludeInFileSystem Include="Vfs1.txt" TargetPath="/myfiles/Vfs1.txt" />
Copy link
Member

Choose a reason for hiding this comment

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

What would happen if there is reference to non-existent file ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It would get ignored. I could make it fail the build

Copy link
Member

Choose a reason for hiding this comment

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

That may be better

@maraf maraf marked this pull request as ready for review October 27, 2025 14:16
Copilot AI review requested due to automatic review settings October 27, 2025 14:16
Copy link
Contributor

Copilot AI left a comment

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 implements support for automatically loading files into the WebAssembly Virtual File System (VFS) using the WasmFilesToIncludeInFileSystem item in the WasmSdk. Files specified with this item are loaded into VFS at application startup, with support for custom target paths via TargetPath metadata.

Key Changes:

  • Added VFS resource support in boot JSON generation to handle files marked with vfs: trait
  • Updated bundler-friendly mode to correctly reference VFS assets with relative paths
  • Implemented target path resolution with fallback to file's wwwroot relative path

Reviewed Changes

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

Show a summary per file
File Description
src/tasks/WasmAppBuilder/WasmAppBuilder.cs Updated virtual path prefix to respect RuntimeAssetsLocation
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs Added VFS resource handling in boot config generation
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs Fixed VFS asset path generation for bundler-friendly mode
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/subdir/subsubdir/Vfs3.txt Test file for nested VFS path without custom TargetPath
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/subdir/Vfs2.txt Test file for VFS with custom TargetPath
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js Added test scenario entry point
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/Vfs1.txt Test file for root-level VFS with custom TargetPath
src/mono/wasm/testassets/WasmBasicTestApp/App/WasmBasicTestApp.csproj Added WasmFilesToIncludeInFileSystem declarations for testing
src/mono/wasm/testassets/WasmBasicTestApp/App/FilesToIncludeInFileSystemTest.cs Test code to verify VFS file existence
src/mono/wasm/testassets/JavascriptBundlers/rollup.config.mjs Updated rollup to handle .txt files
src/mono/wasm/Wasm.Build.Tests/FilesToIncludeInFileSystemTests.cs Integration test for VFS functionality
src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets MSBuild integration for WasmFilesToIncludeInFileSystem
eng/testing/scenarios/BuildWasmAppsJobsList.txt Registered new test class for CI

{
Log.LogMessage(MessageImportance.Low, "Candidate '{0}' is defined as VFS resource '{1}'.", resource.ItemSpec, assetTraitValue);

var targetPath = assetTraitValue.Substring("vfs:".Length);
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

Use string slice operator assetTraitValue[\"vfs:\".Length..] instead of Substring for better performance and modern C# style.

Suggested change
var targetPath = assetTraitValue.Substring("vfs:".Length);
var targetPath = assetTraitValue["vfs:".Length..];

Copilot uses AI. Check for mistakes.
{
string escaped = EscapeName(string.Concat(asset.name));
imports.Add($"import * as {escaped} from \"./{asset.name}\";");
imports.Add($"import {escaped} from \"./{asset.name}\";");
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

Removing the * as from the import statement changes the import from a namespace import to a default import. This breaks compatibility for modules that export a namespace rather than a default export. If VFS assets are expected to be modules with namespace exports, this change will cause runtime errors.

Suggested change
imports.Add($"import {escaped} from \"./{asset.name}\";");
imports.Add($"import * as {escaped} from \"./{asset.name}\";");

Copilot uses AI. Check for mistakes.

<ItemGroup>
<WasmFilesToIncludeInFileSystem Condition="'%(WasmFilesToIncludeInFileSystem.TargetPath)' == ''">
<TargetPath>%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

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

The TargetPath should be prefixed with '/' to ensure it's an absolute path in the VFS. According to the PR description, files should be loaded to paths like '/file.txt' and '/subdir/file.txt', but this sets TargetPath to 'file.txt' or 'subdir/file.txt' without the leading slash.

Suggested change
<TargetPath>%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>
<TargetPath>/%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-Build-mono os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wasm] VFS/common WasmApp properties ignored in wasmbrowser template

2 participants