Skip to content

Commit

Permalink
Fix Switch-Process to copy the current env to the new process (#18452)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT authored and TravisEz13 committed Nov 21, 2022
1 parent fb46702 commit 06745b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Expand Up @@ -4,6 +4,7 @@
#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -69,9 +70,19 @@ protected override void EndProcessing()
// 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);
Expand Down Expand Up @@ -101,15 +112,18 @@ protected override void EndProcessing()
/// 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")]
Expand Down
12 changes: 12 additions & 0 deletions test/powershell/Modules/Microsoft.PowerShell.Core/Exec.Tests.ps1
Expand Up @@ -48,6 +48,18 @@ Describe 'Switch-Process tests for Unix' -Tags 'CI' {
($p | Get-Process).Name -eq 'sleep'
} -timeout 60000 -interval 100 | Should -BeTrue
}

It 'Error is returned if target command is not found' {
$invalidCommand = 'doesNotExist'
$e = { Switch-Process $invalidCommand } | Should -Throw -ErrorId 'CommandNotFound,Microsoft.PowerShell.Commands.SwitchProcessCommand' -PassThru
$e.Exception.Message | Should -BeLike "*'$invalidCommand'*"
$e.TargetObject | Should -BeExactly $invalidCommand
}

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' {
Expand Down

0 comments on commit 06745b1

Please sign in to comment.