Skip to content
Merged
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions run-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2723,8 +2723,12 @@ function run_test(string $php, $file, array $env): string
if (!$passed || $leaked) {
// write .sh
if (strpos($log_format, 'S') !== false) {
$env_lines = [];
$env_lines = ['unset $(env | cut -d= -f1)'];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes any additional variable that may have been inherited from the parent process.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe your github.com comment should be a comment in the source for this magic sauce :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed :)

foreach ($env as $env_var => $env_val) {
if (strval($env_val) === '') {
// proc_open does not pass empty env vars
continue;
}
Comment on lines +2730 to +2733
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proc_open does not pass empty env vars, so if we export empty env vars here, the test will see these vars when running with the .sh file, but not when running it via run-tests.php.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arnaud-lb: is that really proc_open()? for me proc_open() does empty vars fine. My guess is that 'unset $(env | cut -d= -f1)' does the actual trick here. Otherwise this would pretty much constitute a bug if not ... .

Copy link
Member Author

@arnaud-lb arnaud-lb Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to double check, but yes I confirm that proc_open does not pass variables whose value is empty string: https://github.com/arnaud-lb/php-src/blob/484d528b40645f823047b53bcb5ba4228fb612c0/ext/standard/proc_open.c#L178

proc_open(
    [PHP_BINARY, '-n', '-r', 'var_dump(getenv());'],
    [],
    $pipes,
    env_vars: ['foo' => 'bar', 'not-passed' => ''],
);

// Output:

array(1) {
  ["foo"]=>
  string(3) "bar"
}
proc_open(
    'printenv',
    [],
    $pipes,
    env_vars: ['foo' => 'bar', 'not-passed' => ''],
);

// Output:

PWD=...
foo=bar
SHLVL=0
_=/usr/bin/printenv

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's the spot, here is how I did it working, should have probably posted that earlier, sigh:

$descriptor_spec = [];
$env   = ['TRUTH='];
$pipes = [];
$proc  = proc_open(['env'], $descriptor_spec, $pipes, null, $env);
$exit  = proc_close($proc);

output:

TRUTH=

Thanks for double checking @arnaud-lb, this clarifies the comment in the run-tests.php script for me, it depends on passing the $env_vars as list or associative.

$env_lines[] = "export $env_var=" . escapeshellarg($env_val ?? "");
}
$exported_environment = "\n" . implode("\n", $env_lines) . "\n";
Expand All @@ -2733,7 +2737,7 @@ function run_test(string $php, $file, array $env): string
{$exported_environment}
case "$1" in
"gdb")
gdb --args {$orig_cmd}
gdb -ex 'unset environment LINES' -ex 'unset environment COLUMNS' --args {$orig_cmd}
;;
"lldb")
lldb -- {$orig_cmd}
Expand Down
Loading