When the netcore2.1\ref (or other netcore*\ref folder) does not exist, the Directory.EnumerateFiles fails to enumerate assemblies as expected. However because the error is not caught, the custom type fails to be declared even if the ref folder and reference assemblies are not needed for the definition.
This was discovered on Azure Functions for PowerShell because the PowerShell worker directories don't contain the ref subfolder. While this could be fixed in the Azure Functions Host, it should be handled gracefully in case other applications are deployed in a similar way.
Steps to reproduce
Using Azure Functions for PowerShell, attempt to load a custom Type Definition with Add-Type. This is taken from ACME-PS:
$interfaces = @"
public interface IKey
{
object ExportKey();
}
"@
Add-Type -TypeDefinition $interfaces
Expected behavior
Type definition is loaded successfully
Actual behavior
2019-11-20T21:22:50.294 [Error] ERROR: Add-Type : Could not find a part of the path 'D:\Program Files (x86)\SiteExtensions\Functions\2.0.12858\32bit\workers\powershell\runtimes\win-x86\lib\netcoreapp2.1\ref'.
At D:\home\site\wwwroot\Modules\ACME-PS\1.1.1\TypeDefinitions.ps1:25 char:1
+ Add-Type -TypeDefinition $interfaces
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand
Script stack trace:
at <ScriptBlock>, D:\home\site\wwwroot\Modules\ACME-PS\1.1.1\TypeDefinitions.ps1: line 25
at Update-AzWebAppCertificateLE, D:\home\site\wwwroot\Modules\SALInfrastructureFunctions\Export\Update-AzWebAppCertificateLE.ps1: line 66
at <ScriptBlock>, D:\home\site\wwwroot\GenericHttp\run.ps1: line 80
System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\Program Files (x86)\SiteExtensions\Functions\2.0.12858\32bit\workers\powershell\runtimes\win-x86\lib\netcoreapp2.1\ref'.
at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, EnumerationOptions options)
at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options)
at System.IO.Enumeration.FileSystemEnumerableFactory.UserFiles(String directory, String expression, EnumerationOptions options)
at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
at System.IO.Directory.EnumerateFiles(String path, String searchPattern, SearchOption searchOption)
at Microsoft.PowerShell.Commands.AddTypeCommand.InitDefaultRefAssemblies()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at System.Lazy`1.get_Value()
at Microsoft.PowerShell.Commands.AddTypeCommand.GetPortableExecutableReferences()
at Microsoft.PowerShell.Commands.AddTypeCommand.CompileToAssembly(List`1 syntaxTrees, CompilationOptions compilationOptions, EmitOptions emitOptions)
at Microsoft.PowerShell.Commands.AddTypeCommand.SourceCodeProcessing()
at Microsoft.PowerShell.Commands.AddTypeCommand.EndProcessing()
at System.Management.Automation.Cmdlet.DoEndProcessing()
at System.Management.Automation.CommandProcessorBase.Complete()
Environment data
Name Value
---- -----
PSVersion 6.2.3
PSEdition Core
GitCommitId 6.2.3
OS Microsoft Windows 10.0.14393
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
When the netcore2.1\ref (or other netcore*\ref folder) does not exist, the Directory.EnumerateFiles fails to enumerate assemblies as expected. However because the error is not caught, the custom type fails to be declared even if the ref folder and reference assemblies are not needed for the definition.
This was discovered on Azure Functions for PowerShell because the PowerShell worker directories don't contain the ref subfolder. While this could be fixed in the Azure Functions Host, it should be handled gracefully in case other applications are deployed in a similar way.
Steps to reproduce
Using Azure Functions for PowerShell, attempt to load a custom Type Definition with Add-Type. This is taken from ACME-PS:
Expected behavior
Type definition is loaded successfully
Actual behavior
Environment data