Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Static Web Assets] Fix relative path matching #45039

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// 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;
using Moq;

namespace Microsoft.NET.Sdk.Razor.Tests
{
public class DiscoverStaticWebAssetsTest
public class DefineStaticWebAssetsTest
{
[Fact]
public void DiscoversMatchingAssetsBasedOnPattern()
Expand Down Expand Up @@ -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<string>();
var buildEngine = new Mock<IBuildEngine>();
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
.Callback<BuildErrorEventArgs>(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<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}),
new TaskItem(Path.Combine(Environment.CurrentDirectory, "Debug", "Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map"),
new Dictionary<string,string>{ ["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,
Expand Down
Loading