Skip to content

Instantly share code, notes, and snippets.

@guillemcanal
Last active March 13, 2022 15:50
Show Gist options
  • Save guillemcanal/92f4a97bb31d25522a9a7658a5ff5019 to your computer and use it in GitHub Desktop.
Save guillemcanal/92f4a97bb31d25522a9a7658a5ff5019 to your computer and use it in GitHub Desktop.
Scripts using the XdebugHandler componant will load PHP extensions twice on a Nix environment
<?php
require __DIR__ . '/vendor/autoload.php';
// Most PHP CLI tools run this snippet to disable Xdebug at runtime
use Composer\XdebugHandler\XdebugHandler;
$xdebug = new XdebugHandler('myCliApp');
$xdebug->setPersistent();
$xdebug->check();
unset($xdebug);
// This will contains the ini script created by XdebugHandler without the xdebug extension
echo 'PHPRC: ', getenv('PHPRC'), PHP_EOL;
// This should be empty, but on a Nix environment, the PHP binary is wrapped in a bash script
// which export PHP_INI_SCAN_DIR with a fixed value and then run the actual PHP binary with the exec command.
// Consequently, PHP extensions are loaded twice.
echo 'PHP Scan Dir : ', getenv('PHP_INI_SCAN_DIR'), PHP_EOL;
@drupol
Copy link

drupol commented Jan 29, 2022

To quickly reproduce the issue:

With XDebug extension

  1. Create a simple composer.json containing a require section as such:
...8<...
    "require": {
        "ext-xdebug": "*"
    },
...>8...
  1. Launch:
nix shell github:loophp/nix-shell#env-php81 --impure
  1. Run composer install:
composer install
  1. Download the test script from this gist:

wget https://gist.githubusercontent.com/guillemcanal/92f4a97bb31d25522a9a7658a5ff5019/raw/563e406374eed3d2994f9118d6ed381ab1ac721c/xdebug-handler-nix-boom.php

  1. php xdebug-handler-nix-boom.php

Without XDebug extension

  1. Launch:
nix shell github:loophp/nix-shell#env-php81
  1. Run composer install:
composer install
  1. Download the test script from this gist:

wget https://gist.githubusercontent.com/guillemcanal/92f4a97bb31d25522a9a7658a5ff5019/raw/563e406374eed3d2994f9118d6ed381ab1ac721c/xdebug-handler-nix-boom.php

  1. php xdebug-handler-nix-boom.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment