From 17126a848e14f2563814e05ef80387b21fed0241 Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Fri, 22 Nov 2024 14:00:50 +0100 Subject: [PATCH] Fix relative path matching --- .../Tasks/DefineStaticWebAssets.cs | 11 ++++++ ...tsTest.cs => DefineStaticWebAssetsTest.cs} | 39 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) rename test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/{DiscoverStaticWebAssetsTest.cs => DefineStaticWebAssetsTest.cs} (93%) diff --git a/src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs b/src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs index 499460f002c7..dd82b0d68221 100644 --- a/src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs +++ b/src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs @@ -98,6 +98,17 @@ public override bool Execute() if (SourceType == StaticWebAsset.SourceTypes.Discovered) { var candidateMatchPath = GetDiscoveryCandidateMatchPath(candidate); + if (candidateMatchPath == candidate.ItemSpec) + { + var normalizedContentRoot = StaticWebAsset.NormalizeContentRootPath(ContentRoot); + var normalizedAssetPath = Path.GetFullPath(candidate.GetMetadata("FullPath")); + if (normalizedAssetPath.StartsWith(normalizedContentRoot)) + { + var result = normalizedAssetPath.Substring(normalizedContentRoot.Length); + Log.LogMessage(MessageImportance.Low, "FullPath '{0}' starts with content root '{1}' for candidate '{2}'. Using '{3}' as relative path.", normalizedAssetPath, normalizedContentRoot, candidate.ItemSpec, result); + candidateMatchPath = result; + } + } relativePathCandidate = candidateMatchPath; if (matcher != null && string.IsNullOrEmpty(candidate.GetMetadata("RelativePath"))) { diff --git a/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DiscoverStaticWebAssetsTest.cs b/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DefineStaticWebAssetsTest.cs similarity index 93% rename from test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DiscoverStaticWebAssetsTest.cs rename to test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DefineStaticWebAssetsTest.cs index 6efb1078da42..5600078e7cd2 100644 --- a/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DiscoverStaticWebAssetsTest.cs +++ b/test/Microsoft.NET.Sdk.Razor.Tests/StaticWebAssets/DefineStaticWebAssetsTest.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; +using System.Diagnostics; using Microsoft.AspNetCore.StaticWebAssets.Tasks; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; @@ -8,7 +10,7 @@ namespace Microsoft.NET.Sdk.Razor.Tests { - public class DiscoverStaticWebAssetsTest + public class DefineStaticWebAssetsTest { [Fact] public void DiscoversMatchingAssetsBasedOnPattern() @@ -539,6 +541,41 @@ public void NormalizesContentRoot(string contentRoot, string expected) asset.GetMetadata(nameof(StaticWebAsset.ContentRoot)).Should().Be(expected); } + [Fact] + public void ComputesRelativePath_ForAssets() + { + var errorMessages = new List(); + var buildEngine = new Mock(); + buildEngine.Setup(e => e.LogErrorEvent(It.IsAny())) + .Callback(args => errorMessages.Add(args.Message)); + + var task = new DefineStaticWebAssets + { + BuildEngine = buildEngine.Object, + CandidateAssets = [ + new TaskItem(Path.Combine(Environment.CurrentDirectory, "Debug", "Microsoft.AspNetCore.Components.CustomElements.lib.module.js"), + new Dictionary{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}), + new TaskItem(Path.Combine(Environment.CurrentDirectory, "Debug", "Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map"), + new Dictionary{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}) + ], + RelativePathPattern = "**", + SourceType = "Discovered", + SourceId = "Microsoft.AspNetCore.Components.CustomElements", + ContentRoot = Path.Combine(Environment.CurrentDirectory, "Debug"), + BasePath = "_content/Microsoft.AspNetCore.Components.CustomElements" + }; + + // Act + var result = task.Execute(); + + // Assert + result.Should().BeTrue(); + task.Assets.Length.Should().Be(2); + task.Assets[0].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js"); + task.Assets[0].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements"); + task.Assets[1].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map"); + task.Assets[1].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements"); + } private static ITaskItem CreateCandidate( string itemSpec,