Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 2, 2025

Description

The GenerateFSharpILLinkSubstitutions MSBuild target was using ItemName="Embed" which doesn't properly add items as embedded resources. Changed to ItemName="EmbeddedResource" so ILLink.Substitutions.xml is correctly embedded in F# assemblies during compilation.

This enables the ILLinker to automatically remove F# metadata resources (signature/optimization data) during trimming, reducing trimmed assembly size.

Changes:

  • Microsoft.FSharp.NetSdk.targets: Fix <Output TaskParameter="GeneratedItems" ItemName="Embed" />ItemName="EmbeddedResource"
  • Updated FSharpMetadataResource_Trimming_Test.fsproj to use the FSharpTargetsShim property instead of duplicating target logic:
    • Removed duplicated UsingTask and Target blocks
    • Added FSharpTargetsShim property to redirect to locally built Microsoft.FSharp.NetSdk.targets
    • This uses the SDK's shim mechanism (see Microsoft.NET.Sdk.FSharpTargetsShim.targets)
  • Simplified UseLocalCompiler.Directory.Build.props by removing redundant UsingTask elements:
    • Removed FSharpEmbedResourceText, FSharpEmbedResXSource, and GenerateILLinkSubstitutions UsingTask overrides
    • FSharpTargetsShim already handles all task registration via the imported Microsoft.FSharp.Targets file

How FSharpTargetsShim Works:

The FSharpTargetsShim property is part of the .NET SDK's F# integration. When set, the SDK imports that file instead of the bundled F# targets.

Task Registration Chain:

  1. SDK imports $(FSharpTargetsShim)Microsoft.FSharp.NetSdk.targets
  2. Microsoft.FSharp.NetSdk.targets imports Microsoft.FSharp.Targets
  3. Microsoft.FSharp.Targets registers all F# build tasks: Fsc, Fsi, FSharpEmbedResourceText, FSharpEmbedResXSource, CreateFSharpManifestResourceName, WriteCodeFragment, FSharpPlatformInformation, SubstituteText
  4. Microsoft.FSharp.NetSdk.targets also registers MapSourceRoots and GenerateILLinkSubstitutions

Since setting FSharpTargetsShim triggers this chain, the individual UsingTask overrides in UseLocalCompiler.Directory.Build.props were redundant and have been removed.

Fixes #18591

Checklist

  • Test cases added

  • Performance benchmarks added in case of performance changes

  • Release notes entry updated:

    This is a build infrastructure fix. The GenerateILLinkSubstitutions task was added in a previous PR but the MSBuild integration was incorrect. No new release notes entry needed as this fixes an unreleased feature.

    If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

Original prompt

Improve MSBuild Integration and Test Coverage for IlLink Substitution Tasks

Product Fix

  • In Microsoft.FSharp.NetSdk.targets, change any MSBuild Output lines like:
    <Output TaskParameter="GeneratedItems" ItemName="Embed" />
    to:
    <Output TaskParameter="GeneratedItems" ItemName="EmbeddedResource" />
    This ensures resources generated by GenerateILLinkSubstitutions are correctly included in the EmbeddedResource item group.

Test Coverage Fix

  • Remove any manual registration of the MSBuild task (UsingTask or Target blocks) from the FSharpMetadataResource_Trimming_Test.fsproj or similar test projects. The test should NOT use its own version of the target/task.
  • Instead, for local/CI validation, add:
    <Import Project="$(MSBuildThisFileDirectory)../../../../src/FSharp.Build/Microsoft.FSharp.NetSdk.targets" />
    (Adjust path as needed.) This ensures the test picks up the freshly built targets from source and verifies real integration.
  • When the product fix ships in the public SDK, remove the explicit Import and rely solely on <Project Sdk="Microsoft.FSharp.NetSdk"> integration for tests.

Outcome

  • Tests will catch miswiring, integration, or embedding errors in the product targets immediately—preventing accidental "happy path" test results due to local overrides.
  • Ensures that SDK and product integration is always validated before release.

Checklist

  • Change ItemName to EmbeddedResource in product targets.
  • Remove manual task/target registration from test .fsproj.
  • Import targets from source during development/CI, not from released SDK.
  • Switch test to SDK-only import after release.

This pull request was created as a result of the following prompt from Copilot chat.

Improve MSBuild Integration and Test Coverage for IlLink Substitution Tasks

Product Fix

  • In Microsoft.FSharp.NetSdk.targets, change any MSBuild Output lines like:
    <Output TaskParameter="GeneratedItems" ItemName="Embed" />
    to:
    <Output TaskParameter="GeneratedItems" ItemName="EmbeddedResource" />
    This ensures resources generated by GenerateILLinkSubstitutions are correctly included in the EmbeddedResource item group.

Test Coverage Fix

  • Remove any manual registration of the MSBuild task (UsingTask or Target blocks) from the FSharpMetadataResource_Trimming_Test.fsproj or similar test projects. The test should NOT use its own version of the target/task.
  • Instead, for local/CI validation, add:
    <Import Project="$(MSBuildThisFileDirectory)../../../../src/FSharp.Build/Microsoft.FSharp.NetSdk.targets" />
    (Adjust path as needed.) This ensures the test picks up the freshly built targets from source and verifies real integration.
  • When the product fix ships in the public SDK, remove the explicit Import and rely solely on <Project Sdk="Microsoft.FSharp.NetSdk"> integration for tests.

Outcome

  • Tests will catch miswiring, integration, or embedding errors in the product targets immediately—preventing accidental "happy path" test results due to local overrides.
  • Ensures that SDK and product integration is always validated before release.

Checklist

  • Change ItemName to EmbeddedResource in product targets.
  • Remove manual task/target registration from test .fsproj.
  • Import targets from source during development/CI, not from released SDK.
  • Switch test to SDK-only import after release.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 2, 2025 08:25
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve MSBuild integration and test coverage for IlLink tasks Fix ILLink.Substitutions.xml not being embedded in F# assemblies Dec 2, 2025
Copilot AI requested a review from T-Gro December 2, 2025 08:39
@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

✅ No release notes required

… test

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI and others added 2 commits December 3, 2025 11:38
… test-product relationship

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI and others added 2 commits December 3, 2025 14:05
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
…ild.props

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
…argetsShim

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
@T-Gro T-Gro requested a review from abonie December 4, 2025 09:21
@T-Gro T-Gro marked this pull request as ready for review December 4, 2025 09:21
@T-Gro T-Gro requested a review from a team as a code owner December 4, 2025 09:21
@T-Gro T-Gro enabled auto-merge (squash) December 4, 2025 09:54
@T-Gro T-Gro merged commit 3686818 into main Dec 4, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Auto-generate IlLink.Substitutions.xml to Remove F# Metadata Resources

3 participants