Skip to content

[5.28.3] Bootstrap File not resolved correctly #299

@llaville

Description

@llaville

While I finish my tests on my plugin, I've encounter this morning another use case where --bootstrap option is not resolved correctly.

Here are the details now :

Structure of code source

.
├── bin
├── captainhook.json
├── composer.json
├── composer.lock
├── docs
├── examples
├── mkdocs.yml
├── phpunit.xml.dist
├── README.md
├── src
├── tests
├── vendor
└── vendor-bin

Capt'n main config file captainhook.json into project folder

{
    "config": {
        "verbosity": "normal",
        "allow-failure": false,
        "ansi-colors": true,
        "git-directory": ".git",
        "fail-on-first-error": false,
        "bootstrap": "examples/vendor-bin-autoloader.php",
        "plugins": [
            {
                "plugin": "\\CaptainHook\\App\\Plugin\\Hook\\SimplePlugin"
            }
        ],
        "includes": [
            "examples/pre-push.phpstan.json",
            "examples/pre-push.phpunit.json"
        ]
    }
}

FYI, even if not used here in this Use Case (issue)

Capt'n config files into examples/ folder

At least one is enough: see examples/pre-push.phpunit.json following contents

{
    "pre-push": {
        "enabled": true,
        "actions": [
            {
                "action": "{$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin/}phpunit",
                "config": {
                    "label": "Unit tests (with PHPUnit)"
                },
                "conditions": [
                    {
                        "exec": "\\Bartlett\\CaptainHookBinPlugin\\Condition\\PackageInstalled",
                        "args": ["phpunit/phpunit"]
                    }
                ]
            }
        ]
    }
}

Custom Autoloader examples/vendor-bin-autoloader.php

<?php declare(strict_types=1);

use CaptainHook\App\Console\Runtime\Resolver;

$resolver = new Resolver($_SERVER['argv'][0]);

if ($resolver->isPharRelease()) {
    require dirname(__DIR__) . '/vendor/autoload.php';
}

foreach (glob(dirname(__DIR__) . '/vendor-bin/*/vendor/autoload.php') as $autoloadFile) {
    require $autoloadFile;
}

PHPUnit is installed via https://github.com/bamarni/composer-bin-plugin

See vendor-bin/phpunit/composer.json following contents

{
    "require-dev": {
        "phpunit/phpunit": "^10.5 || ^11.5 || ^12.5 || ^13.0"
    }
}

Invoke Capt'n 5.28.3

vendor/bin/captainhook hook:pre-push --bootstrap examples/vendor-bin-autoloader.php -c examples/pre-push.phpunit.json -vv

And got unexpected results

bootstrap file not found
Error triggered in file: /shared/backups/bartlett/captainhook-bin-plugin/vendor/captainhook/captainhook/src/Runner/Bootstrap/Util.php in line: 43

Investigation

When I check into src/Runner/Bootstrap/Util.php

        $bootstrapFile = dirname($config->getPath()) . '/' . $config->getBootstrap();

and prints details of line with

        \var_dump(['path' => $config->getPath(), 'bootstrap' => $config->getBootstrap(), 'resolved' => $bootstrapFile]);

I got :

array(3) {
  'path' =>
  string(78) "/shared/backups/bartlett/captainhook-bin-plugin/examples/pre-push.phpunit.json"
  'bootstrap' =>
  string(34) "examples/vendor-bin-autoloader.php"
  'resolved' =>
  string(91) "/shared/backups/bartlett/captainhook-bin-plugin/examples/examples/vendor-bin-autoloader.php"
}

Expected
string(91) "/shared/backups/bartlett/captainhook-bin-plugin/examples/vendor-bin-autoloader.php"

But got
string(91) "/shared/backups/bartlett/captainhook-bin-plugin/examples/examples/vendor-bin-autoloader.php"

FYI, when I invoke Cap't with main config file with : vendor/bin/captainhook

I got expected results (bootstrap file was correctly resolved)

/shared/backups/bartlett/captainhook-bin-plugin/vendor/captainhook/captainhook/src/Runner/Bootstrap/Util.php:37:
array(3) {
  'path' =>
  string(64) "/shared/backups/bartlett/captainhook-bin-plugin/captainhook.json"
  'bootstrap' =>
  string(34) "examples/vendor-bin-autoloader.php"
  'resolved' =>
  string(82) "/shared/backups/bartlett/captainhook-bin-plugin/examples/vendor-bin-autoloader.php"
}
pre-push:
Do  before pre-push runs
Do  before action {$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin/}phpstan analyse --configuration .github/linters/phpstan.neon.dist --verbose runs
 - Static Analysis (with PHPStan)                                    : done
Do  after action {$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin/}phpstan analyse --configuration .github/linters/phpstan.neon.dist --verbose runs
Do  before action {$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin/}phpunit runs
 - Unit tests (with PHPUnit)                                         : done
Do  after action {$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin/}phpunit runs
captainhook executed all actions successfully, took: 4.64s
Do  after pre-push runs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions