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
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,36 @@ Copyright (c) .NET Foundation. All rights reserved.
<FileWrites Include="@(CsWinRTGeneratorInteropAssemblyPath)"/>
</ItemGroup>
</Target>

<!--
============================================================
_FixGeneratedXbfFilesLink

When UseArtifactsOutput is enabled, the IntermediateOutputPath is redirected to a location
outside the project directory. The AppX Package Targets process _GeneratedXbfFiles items
and compute their relative paths for resources.pri generation. When XBF files reside outside
the project directory, the path computation loses the subdirectory structure, causing XBF
files to appear at the root of resources.pri instead of their correct subdirectory paths.

This target fixes the issue by setting the Link metadata on _GeneratedXbfFiles items to the
correct path relative to $(IntermediateOutputPath), which preserves subdirectory structure
(e.g., "Resources\Styles.xbf" rather than just "Styles.xbf").

See: https://github.com/dotnet/sdk/issues/53556
============================================================
-->
<Target Name="_FixGeneratedXbfFilesLink"
AfterTargets="MarkupCompilePass2"
Condition="'$(UseUwp)' == 'true'">
<PropertyGroup>
<!-- Normalize IntermediateOutputPath to an absolute path with a trailing slash for use in MakeRelative. -->
<_NormalizedIntermediateOutputPath>$([MSBuild]::EnsureTrailingSlash($([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)'))))</_NormalizedIntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<_GeneratedXbfFiles Update="@(_GeneratedXbfFiles)"
Condition="'%(Link)' == '' and !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">
<Link>$([MSBuild]::MakeRelative($(_NormalizedIntermediateOutputPath), %(FullPath)))</Link>
</_GeneratedXbfFiles>
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -864,5 +864,82 @@ private string GetPropertyValue(TestAsset testAsset, string propertyName)

return getValueCommand.GetValues().Single();
}

// Regression test for https://github.com/dotnet/sdk/issues/53556
// When UseArtifactsOutput=true, IntermediateOutputPath is moved outside the project directory.
// The _FixGeneratedXbfFilesLink target should set Link metadata on _GeneratedXbfFiles items
// to preserve subdirectory paths in resources.pri (e.g., "Resources\Styles.xbf" not "Styles.xbf").
[WindowsOnlyFact]
public void ItSetsCorrectLinkMetadataOnGeneratedXbfFilesWhenUsingArtifactsOutput()
{
const string targetFramework = "net10.0-windows10.0.26100.0";

var testProject = new TestProject()
{
Name = "UwpXbfLinkTest",
ProjectSdk = "Microsoft.NET.Sdk",
TargetFrameworks = targetFramework
};
testProject.AdditionalProperties["UseUwp"] = "true";
testProject.AdditionalProperties["UseUwpTools"] = "false";

var testAsset = TestAssetsManager.CreateTestProject(testProject)
.WithProjectChanges(project =>
{
// Inject a target that populates _GeneratedXbfFiles with simulated XBF files
// in subdirectories of IntermediateOutputPath, as would happen with UseArtifactsOutput.
// This simulates what the XAML compiler (MarkupCompilePass2) normally produces.
var injectTarget = XElement.Parse("""
<Target Name="_InjectFakeGeneratedXbfFiles"
BeforeTargets="_FixGeneratedXbfFilesLink">
<ItemGroup>
<_GeneratedXbfFiles Include="$(IntermediateOutputPath)Resources\Styles.xbf" />
<_GeneratedXbfFiles Include="$(IntermediateOutputPath)Views\Home.xbf" />
<_GeneratedXbfFiles Include="$(IntermediateOutputPath)App.xbf" />
</ItemGroup>
</Target>
""");
project.Root.Add(injectTarget);
});

// Write a Directory.Build.props that enables artifacts output, which moves
// IntermediateOutputPath outside the project directory.
File.WriteAllText(Path.Combine(testAsset.Path, "Directory.Build.props"),
"""
<Project>
<PropertyGroup>
<UseArtifactsOutput>true</UseArtifactsOutput>
</PropertyGroup>
</Project>
""");

var getValuesCommand = new GetValuesCommand(
Log,
Path.Combine(testAsset.Path, testProject.Name),
targetFramework,
"_GeneratedXbfFiles",
GetValuesCommand.ValueType.Item)
{
ShouldRestore = false,
DependsOnTargets = "_FixGeneratedXbfFilesLink"
};
getValuesCommand.MetadataNames.Add("Link");

getValuesCommand.Execute().Should().Pass();

var items = getValuesCommand.GetValuesWithMetadata();

// Items in subdirectories should have Link metadata that preserves the subdirectory path.
// Without the fix, these would be empty or just the filename (e.g., "Styles.xbf").
var stylesItem = items.Single(i => Path.GetFileName(i.value).Equals("Styles.xbf", StringComparison.OrdinalIgnoreCase));
stylesItem.metadata["Link"].Should().Be(@"Resources\Styles.xbf");

var homeItem = items.Single(i => Path.GetFileName(i.value).Equals("Home.xbf", StringComparison.OrdinalIgnoreCase));
homeItem.metadata["Link"].Should().Be(@"Views\Home.xbf");

// Root-level XBF item should have Link metadata with just the filename.
var appItem = items.Single(i => Path.GetFileName(i.value).Equals("App.xbf", StringComparison.OrdinalIgnoreCase));
appItem.metadata["Link"].Should().Be("App.xbf");
}
}
}
Loading