Skip to content

Commit fd36202

Browse files
danmoseleyRuihan-Yin
authored andcommitted
[browser] Fix ConvertDllsToWebCil stack dump on bad input to be error message with input path (dotnet#101162)
1 parent 7108d59 commit fd36202

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs

+52-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using Microsoft.Build.Framework;
@@ -59,52 +60,66 @@ public override bool Execute()
5960
continue;
6061
}
6162

62-
var dllFilePath = candidate.ItemSpec;
63-
var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension;
64-
string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture"
65-
? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue"))
66-
: OutputPath;
67-
68-
string finalWebcil = Path.Combine(candidatePath, webcilFileName);
69-
70-
if (Utils.IsNewerThan(dllFilePath, finalWebcil))
63+
try
7164
{
72-
var tmpWebcil = Path.Combine(tmpDir, webcilFileName);
73-
var logAdapter = new LogAdapter(Log);
74-
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter);
75-
webcilWriter.ConvertToWebcil();
76-
77-
if (!Directory.Exists(candidatePath))
78-
Directory.CreateDirectory(candidatePath);
79-
80-
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
81-
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
82-
else
83-
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
65+
TaskItem webcilItem = ConvertDll(tmpDir, candidate);
66+
webCilCandidates.Add(webcilItem);
8467
}
85-
else
68+
catch (Exception ex)
8669
{
87-
Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}");
70+
Log.LogError($"Failed to convert '{candidate.ItemSpec}' to webcil: {ex.Message}");
71+
return false;
8872
}
73+
}
74+
75+
WebCilCandidates = webCilCandidates.ToArray();
76+
return true;
77+
}
8978

90-
_fileWrites.Add(finalWebcil);
79+
private TaskItem ConvertDll(string tmpDir, ITaskItem candidate)
80+
{
81+
var dllFilePath = candidate.ItemSpec;
82+
var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension;
83+
string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture"
84+
? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue"))
85+
: OutputPath;
9186

92-
var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata());
93-
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension));
94-
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil);
87+
string finalWebcil = Path.Combine(candidatePath, webcilFileName);
9588

96-
if (webcilItem.GetMetadata("AssetTraitName") == "Culture")
97-
{
98-
string relatedAsset = webcilItem.GetMetadata("RelatedAsset");
99-
relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension);
100-
webcilItem.SetMetadata("RelatedAsset", relatedAsset);
101-
Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}.");
102-
}
89+
if (Utils.IsNewerThan(dllFilePath, finalWebcil))
90+
{
91+
var tmpWebcil = Path.Combine(tmpDir, webcilFileName);
92+
var logAdapter = new LogAdapter(Log);
93+
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter);
94+
webcilWriter.ConvertToWebcil();
10395

104-
webCilCandidates.Add(webcilItem);
96+
if (!Directory.Exists(candidatePath))
97+
Directory.CreateDirectory(candidatePath);
98+
99+
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
100+
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
101+
else
102+
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
103+
}
104+
else
105+
{
106+
Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}");
105107
}
106108

107-
WebCilCandidates = webCilCandidates.ToArray();
108-
return true;
109+
_fileWrites.Add(finalWebcil);
110+
111+
var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata());
112+
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension));
113+
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil);
114+
115+
if (webcilItem.GetMetadata("AssetTraitName") == "Culture")
116+
{
117+
string relatedAsset = webcilItem.GetMetadata("RelatedAsset");
118+
relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension);
119+
webcilItem.SetMetadata("RelatedAsset", relatedAsset);
120+
Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}.");
121+
}
122+
123+
return webcilItem;
109124
}
110125
}

0 commit comments

Comments
 (0)