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

* PowerShell - Ignore environment variables when building C# code

* Just unset LIB for now

* Fix sanity issue

(cherry picked from commit 097bc07b66)
pull/75864/head
Jordan Borean 3 years ago committed by GitHub
parent d9d5d2d93e
commit 15f5886479
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 # compile the code together and check for errors
$provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider $provider = New-Object -TypeName Microsoft.CSharp.CSharpCodeProvider
# 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) $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) { if ($compile.Errors.HasErrors) {
$msg = "Failed to compile C# code: " $msg = "Failed to compile C# code: "
foreach ($e in $compile.Errors) { foreach ($e in $compile.Errors) {

@ -295,5 +295,28 @@ namespace Namespace11
Add-CSharpType -Reference $arch_class Add-CSharpType -Reference $arch_class
Assert-Equals -actual ([Namespace11.Class11]::GetIntPtrSize()) -expected ([System.IntPtr]::Size) 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" $result.res = "success"
Exit-Json -obj $result Exit-Json -obj $result

Loading…
Cancel
Save