PowerShell - Ignore LIB env var when building C# code (#75698) (#75724)

* PowerShell - Ignore environment variables when building C# code

* Just unset LIB for now

* Fix sanity issue

(cherry picked from commit 097bc07b66)
pull/75855/head
Jordan Borean 3 years ago committed by GitHub
parent 2cdc3834c9
commit 87e77a93a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- PowerShell - Ignore the ``LIB`` environment variable when compiling C# Ansible code

@ -332,7 +332,28 @@ Function Add-CSharpType {
# compile the code together and check for errors
$provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider
$compile = $provider.CompileAssemblyFromDom($compile_parameters, $compile_units)
# This calls csc.exe which can take compiler options from environment variables. Currently these env vars
# are known to have problems so they are unset:
# LIB - additional library paths will fail the compilation if they are invalid
$originalEnv = @{}
try {
'LIB' | ForEach-Object -Process {
$value = Get-Item -LiteralPath "Env:\$_" -ErrorAction SilentlyContinue
if ($value) {
$originalEnv[$_] = $value
Remove-Item -LiteralPath "Env:\$_"
}
}
$compile = $provider.CompileAssemblyFromDom($compile_parameters, $compile_units)
}
finally {
foreach ($kvp in $originalEnv.GetEnumerator()) {
[System.Environment]::SetEnvironmentVariable($kvp.Key, $kvp.Value, "Process")
}
}
if ($compile.Errors.HasErrors) {
$msg = "Failed to compile C# code: "
foreach ($e in $compile.Errors) {

@ -295,5 +295,28 @@ namespace Namespace11
Add-CSharpType -Reference $arch_class
Assert-Equals -actual ([Namespace11.Class11]::GetIntPtrSize()) -expected ([System.IntPtr]::Size)
$lib_set = @'
using System;
namespace Namespace12
{
public class Class12
{
public static string GetString()
{
return "b";
}
}
}
'@
$env:LIB = "C:\fake\folder\path"
try {
Add-CSharpType -Reference $lib_set
}
finally {
Remove-Item -LiteralPath env:\LIB
}
Assert-Equals -actual ([Namespace12.Class12]::GetString()) -expected "b"
$result.res = "success"
Exit-Json -obj $result

Loading…
Cancel
Save