Skip to content

Conversation

@arnaud-lb
Copy link
Member

@arnaud-lb arnaud-lb commented Apr 11, 2025

run-tests.php conveniently generates a .sh file for each failed test.

The file starts by setting environment variables to the value they had during the test run.

Unfortunately, sometimes extra env vars will sneak in and prevent reproduction:

  • Variables are inherited from the process that executes the .sh file
  • The .sh file exports empty variables, but proc_open() does not
  • When running under gdb, the variables LINES and COLUMNS are set

Here I ensure that these variables do not sneak in anymore in these cases when using the .sh file.

// 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 :)

Comment on lines +2728 to +2731
if (strval($env_val) === '') {
// proc_open does not pass empty env vars
continue;
}
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.

@arnaud-lb arnaud-lb merged commit 0eea5d3 into php:master Apr 29, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants