Skip to content

Commit

Permalink
[browser] Fix ConvertDllsToWebCil stack dump on bad input to be error…
Browse files Browse the repository at this point in the history
… message with input path (dotnet#101162)
  • Loading branch information
danmoseley committed May 4, 2024
1 parent 64d537a commit 358b0a4
Showing 1 changed file with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -59,52 +60,66 @@ public override bool Execute()
continue;
}

var dllFilePath = candidate.ItemSpec;
var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension;
string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture"
? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue"))
: OutputPath;

string finalWebcil = Path.Combine(candidatePath, webcilFileName);

if (Utils.IsNewerThan(dllFilePath, finalWebcil))
try
{
var tmpWebcil = Path.Combine(tmpDir, webcilFileName);
var logAdapter = new LogAdapter(Log);
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter);
webcilWriter.ConvertToWebcil();

if (!Directory.Exists(candidatePath))
Directory.CreateDirectory(candidatePath);

if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
else
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
TaskItem webcilItem = ConvertDll(tmpDir, candidate);
webCilCandidates.Add(webcilItem);
}
else
catch (Exception ex)
{
Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}");
Log.LogError($"Failed to convert '{candidate.ItemSpec}' to webcil: {ex.Message}");
return false;
}
}

WebCilCandidates = webCilCandidates.ToArray();
return true;
}

_fileWrites.Add(finalWebcil);
private TaskItem ConvertDll(string tmpDir, ITaskItem candidate)
{
var dllFilePath = candidate.ItemSpec;
var webcilFileName = Path.GetFileNameWithoutExtension(dllFilePath) + Utils.WebcilInWasmExtension;
string candidatePath = candidate.GetMetadata("AssetTraitName") == "Culture"
? Path.Combine(OutputPath, candidate.GetMetadata("AssetTraitValue"))
: OutputPath;

var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata());
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension));
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil);
string finalWebcil = Path.Combine(candidatePath, webcilFileName);

if (webcilItem.GetMetadata("AssetTraitName") == "Culture")
{
string relatedAsset = webcilItem.GetMetadata("RelatedAsset");
relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension);
webcilItem.SetMetadata("RelatedAsset", relatedAsset);
Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}.");
}
if (Utils.IsNewerThan(dllFilePath, finalWebcil))
{
var tmpWebcil = Path.Combine(tmpDir, webcilFileName);
var logAdapter = new LogAdapter(Log);
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: dllFilePath, outputPath: tmpWebcil, logger: logAdapter);
webcilWriter.ConvertToWebcil();

webCilCandidates.Add(webcilItem);
if (!Directory.Exists(candidatePath))
Directory.CreateDirectory(candidatePath);

if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
else
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
}
else
{
Log.LogMessage(MessageImportance.Low, $"Skipping {dllFilePath} as it is older than the output file {finalWebcil}");
}

WebCilCandidates = webCilCandidates.ToArray();
return true;
_fileWrites.Add(finalWebcil);

var webcilItem = new TaskItem(finalWebcil, candidate.CloneCustomMetadata());
webcilItem.SetMetadata("RelativePath", Path.ChangeExtension(candidate.GetMetadata("RelativePath"), Utils.WebcilInWasmExtension));
webcilItem.SetMetadata("OriginalItemSpec", finalWebcil);

if (webcilItem.GetMetadata("AssetTraitName") == "Culture")
{
string relatedAsset = webcilItem.GetMetadata("RelatedAsset");
relatedAsset = Path.ChangeExtension(relatedAsset, Utils.WebcilInWasmExtension);
webcilItem.SetMetadata("RelatedAsset", relatedAsset);
Log.LogMessage(MessageImportance.Low, $"Changing related asset of {webcilItem} to {relatedAsset}.");
}

return webcilItem;
}
}

0 comments on commit 358b0a4

Please sign in to comment.