Skip to content

Conversation

ilonatommy
Copy link
Member

Fixes #77174.

@ilonatommy ilonatommy self-assigned this Sep 25, 2025
@ilonatommy ilonatommy added arch-wasm WebAssembly architecture area-Build-mono labels Sep 25, 2025
@JakeYallop
Copy link
Contributor

Its also worth pointing that dotnet.d.ts is currently explicitly removed from the build via:

_WasmBuildFilesToRemove:

<Output TaskParameter="FilesToRemove" ItemName="_WasmBuildFilesToRemove" />

Which is an output from the ComputeWasmBuildAssets task, in that task:

if (AssetsComputingHelper.ShouldFilterCandidate(candidate, TimeZoneSupport, InvariantGlobalization, LoadFullICUData, CopySymbols, customIcuCandidateFilename, EnableThreads, EnableDiagnostics, EmitSourceMap, out var reason))

".ts" when fromMonoPackage && fileName == "dotnet.d" => "dotnet type definition is not used by Blazor",

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

@Copilot 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

Adds an opt-in MSBuild property to copy the generated dotnet.d.ts TypeScript definition file into a WebAssembly project's wwwroot during build, addressing issue #77174.

  • Introduces WasmEmitTypeScriptDefinitions property (default false) in relevant .targets.
  • Adds MSBuild target _EnsureDotnetTypeScriptDefinitions to perform the conditional copy.
  • Adds a test validating the file copy on build (Debug/Release).

Reviewed Changes

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

File Description
src/mono/wasm/build/WasmApp.Common.targets Documents and defines the new WasmEmitTypeScriptDefinitions property.
src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets Adds property default again and new target to copy dotnet.d.ts into wwwroot.
src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs Adds test ensuring dotnet.d.ts is copied on build when the property is enabled.

<WasmStripILAfterAOT Condition="'$(WasmStripILAfterAOT)' == ''">true</WasmStripILAfterAOT>
<WasmRuntimeAssetsLocation Condition="'$(WasmRuntimeAssetsLocation)' == ''">_framework</WasmRuntimeAssetsLocation>
<MetricsSupport Condition="'$(MetricsSupport)' == ''">false</MetricsSupport>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

The default for WasmEmitTypeScriptDefinitions is also being set in Microsoft.NET.Sdk.WebAssembly.Browser.targets, creating duplicate sources of truth that can drift; consider centralizing the default in a single imported location (or guard one with Condition="'$(WasmEmitTypeScriptDefinitions)'=='' and '$(DefiningFileFlag)'!='true'") to avoid divergence.

Suggested change
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == '' and '$(DefiningFileFlag)' != 'true'">false</WasmEmitTypeScriptDefinitions>

Copilot uses AI. Check for mistakes.

<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true</_WasmCopyOutputSymbolsToOutputDirectory>
<_WasmEnableThreads>$(WasmEnableThreads)</_WasmEnableThreads>
<_WasmEnableThreads Condition="'$(_WasmEnableThreads)' == ''">false</_WasmEnableThreads>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

This repeats the default assignment also added in WasmApp.Common.targets; please consolidate the default definition to one place to prevent future inconsistencies.

Suggested change
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>

Copilot uses AI. Check for mistakes.

<_RuntimePackDir Condition="'$(_RuntimePackDir)' == ''">%(ResolvedRuntimePack.PackageDirectory)</_RuntimePackDir>
<_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))</_RuntimePackNativeDir>
<_DotnetTypesSourcePath>$(_RuntimePackNativeDir)dotnet.d.ts</_DotnetTypesSourcePath>
<_DotnetTypesDestPath>$(MSBuildProjectDirectory)\wwwroot\dotnet.d.ts</_DotnetTypesDestPath>
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

[nitpick] Hardcoded backslashes reduce clarity on non-Windows; prefer a normalized construction such as $([MSBuild]::NormalizeDirectory('$(MSBuildProjectDirectory)','wwwroot'))dotnet.d.ts or $(MSBuildProjectDirectory)$(DirectorySeparatorChar)wwwroot$(DirectorySeparatorChar)dotnet.d.ts for cross-platform consistency.

Suggested change
<_DotnetTypesDestPath>$(MSBuildProjectDirectory)\wwwroot\dotnet.d.ts</_DotnetTypesDestPath>
<_DotnetTypesDestPath>$(MSBuildProjectDirectory)$(DirectorySeparatorChar)wwwroot$(DirectorySeparatorChar)dotnet.d.ts</_DotnetTypesDestPath>

Copilot uses AI. Check for mistakes.

// Verify dotnet.d.ts is not in wwwroot after creation
Assert.False(File.Exists(dotnetDtsWwwrootPath), $"dotnet.d.ts should not exist at {dotnetDtsWwwrootPath} after creation when WasmEmitTypeScriptDefinitions is used");

// Build to trigger the _EnsureDotnetTypeScriptDefinitions target on restore
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

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

The comment is misleading—_EnsureDotnetTypeScriptDefinitions runs AfterTargets _ResolveWasmConfiguration during build, not restore; update the comment to reflect it triggers during the build phase.

Suggested change
// Build to trigger the _EnsureDotnetTypeScriptDefinitions target on restore
// Build to trigger the _EnsureDotnetTypeScriptDefinitions target during the build phase

Copilot uses AI. Check for mistakes.

<WasmStripILAfterAOT Condition="'$(WasmStripILAfterAOT)' == ''">true</WasmStripILAfterAOT>
<WasmRuntimeAssetsLocation Condition="'$(WasmRuntimeAssetsLocation)' == ''">_framework</WasmRuntimeAssetsLocation>
<MetricsSupport Condition="'$(MetricsSupport)' == ''">false</MetricsSupport>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>

There is no need to set the property here as well

Comment on lines +868 to +869
<_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))</_RuntimePackNativeDir>
<_DotnetTypesSourcePath>$(_RuntimePackNativeDir)dotnet.d.ts</_DotnetTypesSourcePath>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))</_RuntimePackNativeDir>
<_DotnetTypesSourcePath>$(_RuntimePackNativeDir)dotnet.d.ts</_DotnetTypesSourcePath>
<_DotnetTypesSourcePath>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native', 'dotnet.d.ts'))</_DotnetTypesSourcePath>

<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true</_WasmCopyOutputSymbolsToOutputDirectory>
<_WasmEnableThreads>$(WasmEnableThreads)</_WasmEnableThreads>
<_WasmEnableThreads Condition="'$(_WasmEnableThreads)' == ''">false</_WasmEnableThreads>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>
Copy link
Member

Choose a reason for hiding this comment

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

Please, follow the pattern with assign to private property first, and then set the default value to the private property

[Theory]
[InlineData(Configuration.Debug)]
[InlineData(Configuration.Release)]
public void TypeScriptDefinitionsCopiedToWwwrootOnBuild(Configuration config)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test case for false resulting in file not exist

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dotnet.d.ts should be include in the wasmbrowser template
4 participants