Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down