Skip to content

Commit

Permalink
Stop unnecessary exceptions from being thrown from crossgen when a re…
Browse files Browse the repository at this point in the history
…ference is not actually a managed dll (#70191)

- Makes ad hoc usage of crossgen2 much more convenient on Windows as the exceptions generated are annoying to deal with in the debugger
  • Loading branch information
davidwrighton authored Jun 6, 2022
1 parent 542bcf4 commit febd927
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ public EcmaModule GetModuleForSimpleName(string simpleName, bool throwIfNotFound
return null;
}

public EcmaModule GetModuleFromPath(string filePath)
public EcmaModule GetModuleFromPath(string filePath, bool throwOnFailureToLoad = true)
{
return GetOrAddModuleFromPath(filePath, true);
return GetOrAddModuleFromPath(filePath, true, throwOnFailureToLoad: throwOnFailureToLoad);
}

public EcmaModule GetMetadataOnlyModuleFromPath(string filePath)
{
return GetOrAddModuleFromPath(filePath, false);
}

private EcmaModule GetOrAddModuleFromPath(string filePath, bool useForBinding)
private EcmaModule GetOrAddModuleFromPath(string filePath, bool useForBinding, bool throwOnFailureToLoad = true)
{
filePath = Path.GetFullPath(filePath);

Expand All @@ -144,7 +144,7 @@ private EcmaModule GetOrAddModuleFromPath(string filePath, bool useForBinding)
return entry.Module;
}

return AddModule(filePath, null, useForBinding);
return AddModule(filePath, null, useForBinding, throwOnFailureToLoad: throwOnFailureToLoad);
}

public static unsafe PEReader OpenPEFile(string filePath, out MemoryMappedViewAccessor mappedViewAccessor)
Expand Down Expand Up @@ -182,7 +182,7 @@ public static unsafe PEReader OpenPEFile(string filePath, out MemoryMappedViewAc
}
}

private EcmaModule AddModule(string filePath, string expectedSimpleName, bool useForBinding, ModuleData oldModuleData = null)
private EcmaModule AddModule(string filePath, string expectedSimpleName, bool useForBinding, ModuleData oldModuleData = null, bool throwOnFailureToLoad = true)
{
filePath = Path.GetFullPath(filePath);

Expand Down Expand Up @@ -210,6 +210,11 @@ private EcmaModule AddModule(string filePath, string expectedSimpleName, bool us
pdbReader = oldModuleData.Module.PdbReader;
}

if (!peReader.HasMetadata && !throwOnFailureToLoad)
{
return null;
}

EcmaModule module = EcmaModule.Create(this, peReader, containingAssembly: null, pdbReader);

MetadataReader metadataReader = module.MetadataReader;
Expand Down
11 changes: 9 additions & 2 deletions src/coreclr/tools/aot/crossgen2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,10 @@ private int Run(string[] args)
{
try
{
EcmaModule module = _typeSystemContext.GetModuleFromPath(referenceFile);
EcmaModule module = _typeSystemContext.GetModuleFromPath(referenceFile, throwOnFailureToLoad: false);
if (module == null)
continue;

_referenceableModules.Add(module);
if (_commandLineOptions.InputBubble && _inputbubblereferenceFilePaths.Count == 0)
{
Expand All @@ -490,7 +493,11 @@ private int Run(string[] args)
{
try
{
EcmaModule module = _typeSystemContext.GetModuleFromPath(referenceFile);
EcmaModule module = _typeSystemContext.GetModuleFromPath(referenceFile, throwOnFailureToLoad: false);

if (module == null)
continue;

versionBubbleModulesHash.Add(module);
}
catch { } // Ignore non-managed pe files
Expand Down

0 comments on commit febd927

Please sign in to comment.