Skip to content

Commit 35ce9a8

Browse files
committed
Fix relative paths
1 parent 833aeb0 commit 35ce9a8

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/StaticWebAssetsSdk/Tasks/DefineStaticWebAssets.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ public override bool Execute()
110110
if (SourceType == StaticWebAsset.SourceTypes.Discovered)
111111
{
112112
var candidateMatchPath = GetDiscoveryCandidateMatchPath(candidate);
113+
if (Path.IsPathRooted(candidateMatchPath) && candidateMatchPath == candidate.ItemSpec)
114+
{
115+
var normalizedAssetPath = Path.GetFullPath(candidate.GetMetadata("FullPath"));
116+
var normalizedDirectoryPath = Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode);
117+
if (normalizedAssetPath.StartsWith(normalizedDirectoryPath))
118+
{
119+
var result = normalizedAssetPath.Substring(normalizedDirectoryPath.Length);
120+
Log.LogMessage(MessageImportance.Low, "FullPath '{0}' starts with content root '{1}' for candidate '{2}'. Using '{3}' as relative path.", normalizedAssetPath, normalizedDirectoryPath, candidate.ItemSpec, result);
121+
candidateMatchPath = result;
122+
}
123+
}
113124
relativePathCandidate = candidateMatchPath;
114125
if (matcher != null && string.IsNullOrEmpty(candidate.GetMetadata("RelativePath")))
115126
{

test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/DiscoverStaticWebAssetsTest.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,121 @@ public void DefineStaticWebAssetsCache_CanRoundtripManifest()
688688
File.Delete(manifestPath);
689689
}
690690
}
691+
692+
[Fact]
693+
public void ComputesRelativePath_ForDiscoveredAssetsWithFullPath()
694+
{
695+
var errorMessages = new List<string>();
696+
var buildEngine = new Mock<IBuildEngine>();
697+
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
698+
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message));
699+
buildEngine.SetupGet(e => e.ProjectFileOfTaskNode)
700+
.Returns(Path.Combine(Environment.CurrentDirectory, "Debug", "TestProject.csproj"));
701+
702+
var debugDir = Path.Combine(Environment.CurrentDirectory, "Debug", "wwwroot");
703+
var task = new DefineStaticWebAssets
704+
{
705+
BuildEngine = buildEngine.Object,
706+
CandidateAssets = [
707+
new TaskItem(Path.Combine(debugDir, "Microsoft.AspNetCore.Components.CustomElements.lib.module.js"),
708+
new Dictionary<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}),
709+
new TaskItem(Path.Combine(debugDir, "Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map"),
710+
new Dictionary<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"})
711+
],
712+
RelativePathPattern = "wwwroot/**",
713+
SourceType = "Discovered",
714+
SourceId = "Microsoft.AspNetCore.Components.CustomElements",
715+
ContentRoot = debugDir,
716+
BasePath = "_content/Microsoft.AspNetCore.Components.CustomElements",
717+
TestResolveFileDetails = _testResolveFileDetails,
718+
};
719+
720+
// Act
721+
var result = task.Execute();
722+
723+
// Assert
724+
result.Should().BeTrue($"Errors: {Environment.NewLine} {string.Join($"{Environment.NewLine} ", errorMessages)}");
725+
task.Assets.Length.Should().Be(2);
726+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js");
727+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements");
728+
task.Assets[1].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map");
729+
task.Assets[1].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements");
730+
}
731+
732+
[Fact]
733+
public void ComputesRelativePath_WorksForItemsWithRelativePaths()
734+
{
735+
var errorMessages = new List<string>();
736+
var buildEngine = new Mock<IBuildEngine>();
737+
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
738+
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message));
739+
buildEngine.SetupGet(e => e.ProjectFileOfTaskNode)
740+
.Returns(Path.Combine(Environment.CurrentDirectory, "Debug", "TestProject.csproj"));
741+
742+
var debugDir = Path.Combine(Environment.CurrentDirectory, "Debug", "wwwroot");
743+
var task = new DefineStaticWebAssets
744+
{
745+
BuildEngine = buildEngine.Object,
746+
CandidateAssets = [
747+
new TaskItem(Path.Combine("wwwroot", "Microsoft.AspNetCore.Components.CustomElements.lib.module.js"),
748+
new Dictionary<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}),
749+
new TaskItem(Path.Combine("wwwroot", "Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map"),
750+
new Dictionary<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"})
751+
],
752+
RelativePathPattern = "wwwroot/**",
753+
SourceType = "Discovered",
754+
SourceId = "Microsoft.AspNetCore.Components.CustomElements",
755+
ContentRoot = debugDir,
756+
BasePath = "_content/Microsoft.AspNetCore.Components.CustomElements",
757+
TestResolveFileDetails = _testResolveFileDetails,
758+
};
759+
760+
// Act
761+
var result = task.Execute();
762+
763+
// Assert
764+
result.Should().BeTrue($"Errors: {Environment.NewLine} {string.Join($"{Environment.NewLine} ", errorMessages)}");
765+
task.Assets.Length.Should().Be(2);
766+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js");
767+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements");
768+
task.Assets[1].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Microsoft.AspNetCore.Components.CustomElements.lib.module.js.map");
769+
task.Assets[1].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Microsoft.AspNetCore.Components.CustomElements");
770+
}
771+
772+
[Fact]
773+
public void ComputesRelativePath_ForAssets_ExplicitPaths()
774+
{
775+
var errorMessages = new List<string>();
776+
var buildEngine = new Mock<IBuildEngine>();
777+
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>()))
778+
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message));
779+
780+
var debugDir = Path.Combine(Environment.CurrentDirectory, "Debug");
781+
var task = new DefineStaticWebAssets
782+
{
783+
BuildEngine = buildEngine.Object,
784+
CandidateAssets = [
785+
new TaskItem("/home/user/work/Repo/Project/Components/Dropdown/Dropdown.razor.js",
786+
new Dictionary<string,string>{ ["Integrity"] = "integrity", ["Fingerprint"] = "fingerprint"}),
787+
],
788+
RelativePathPattern = "**",
789+
SourceType = "Discovered",
790+
SourceId = "Project",
791+
ContentRoot = "/home/user/work/Repo/Project",
792+
BasePath = "_content/Project",
793+
TestResolveFileDetails = _testResolveFileDetails,
794+
};
795+
796+
// Act
797+
var result = task.Execute();
798+
799+
// Assert
800+
result.Should().BeTrue($"Errors: {Environment.NewLine} {string.Join($"{Environment.NewLine} ", errorMessages)}");
801+
task.Assets.Length.Should().Be(1);
802+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.RelativePath)).Should().Be("Components/Dropdown/Dropdown.razor.js");
803+
task.Assets[0].GetMetadata(nameof(StaticWebAsset.BasePath)).Should().Be("_content/Project");
804+
}
805+
691806
private static TaskLoggingHelper CreateLogger()
692807
{
693808
var errorMessages = new List<string>();

0 commit comments

Comments
 (0)