[release/v7.3.1] Fix Switch-Process to copy the current env to the new process (#18632)

* Fix `Switch-Process` to copy the current env to the new process (#18452)

* Update test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1

Co-authored-by: Steve Lee <slee@microsoft.com>
This commit is contained in:
Travis Plunk
2022-11-21 14:52:51 -08:00
committed by GitHub
co-authored by Steve Lee
parent fb46702bbc
commit 15666cf5ba
2 changed files with 22 additions and 3 deletions
@@ -4,6 +4,7 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Runtime.InteropServices;
@@ -69,9 +70,19 @@ namespace Microsoft.PowerShell.Commands
// need null terminator at end
execArgs[execArgs.Length - 1] = null;
var env = Environment.GetEnvironmentVariables();
var envBlock = new string?[env.Count + 1];
int j = 0;
foreach (DictionaryEntry entry in env)
{
envBlock[j++] = entry.Key + "=" + entry.Value;
}
envBlock[envBlock.Length - 1] = null;
// setup termios for a child process as .NET modifies termios dynamically for use with ReadKey()
ConfigureTerminalForChildProcess(true);
int exitCode = Exec(command.Source, execArgs);
int exitCode = Exec(command.Source, execArgs, envBlock);
if (exitCode < 0)
{
ConfigureTerminalForChildProcess(false);
@@ -101,15 +112,18 @@ namespace Microsoft.PowerShell.Commands
/// The arguments to send through to the executable.
/// Array must have its final element be null.
/// </param>
/// <param name="env">
/// The environment variables to send through to the executable in the form of "key=value".
/// Array must have its final element be null.
/// <returns>
/// An exit code if exec failed, but if successful the calling process will be overwritten.
/// </returns>
[DllImport("libc",
EntryPoint = "execv",
EntryPoint = "execve",
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi,
SetLastError = true)]
private static extern int Exec(string path, string?[] args);
private static extern int Exec(string path, string?[] args, string?[] env);
// leverage .NET runtime's native library which abstracts the need to handle different OS and architectures for termios api
[DllImport("libSystem.Native", EntryPoint = "SystemNative_ConfigureTerminalForChildProcess")]
@@ -48,6 +48,11 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' {
($p | Get-Process).Name -eq 'sleep'
} -timeout 60000 -interval 100 | Should -BeTrue
}
It 'The environment will be copied to the child process' {
$env = pwsh -noprofile -outputformat text -command { $env:TEST_FOO='my test = value'; Switch-Process bash -c 'echo $TEST_FOO' }
$env | Should -BeExactly 'my test = value'
}
}
Describe 'Switch-Process for Windows' -Tag 'CI' {