Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getPackageVersion in Helpers causes OutOfBoundsException errors on systems with multiple composer installs #833

Open
N1ghteyes opened this issue Oct 22, 2021 · 5 comments

Comments

@N1ghteyes
Copy link

Run into a weird problem with a wordpress build, with the following error:

[22-Oct-2021 08:55:16 UTC] PHP Fatal error:  Uncaught OutOfBoundsException: Package "calcinai/xero-php" is not installed in public_html/wp-content/plugins/woocommerce-eu-vat-assistant/src/vendor/composer/InstalledVersions.php:188

That pathing looked odd to me, so i Tracked the issue to here:

public static function getPackageVersion()
    {
        if (!is_callable('\\Composer\\InstalledVersions::getPrettyVersion')) {
            return self::DEFAULT_VERSION;
        }

        return \Composer\InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
    }

The reference to \Composer is loading the composer namespace instantiated by the woocommerce-eu-vat-assistant plugin, not the custom plugin that is calling it.

For clarity, several plugins have vendor directories included, which means several versions of the \Composer namespace which means that \Composer\InstalledVersions is calling the wrong composer.json for checks, thus is failing (so far as i can tell).

Not sure what the fix is here - i'll provide one if i can work it out. Anyone else run into the same thing?

@peteralewis
Copy link

Hit the same issue.

v2.2.4 didn't have this issue, although the file you reference was still labelled v2.2.1 in the header comments, I reverted back to this and no longer hit the error...

const PACKAGE_VERSION_FILE = '/VERSION';
public static function getPackageVersion()
{
    if (!file_exists(self::PACKAGE_VERSION_FILE)) {
        return self::DEFAULT_VERSION;
    }

    return file_get_contents(self::PACKAGE_VERSION_FILE);
}

@calcinai
Copy link
Owner

calcinai commented Dec 9, 2021

Oh man. @N1ghteyes did you find a solution to this?

FYI, the reason for this was for Xero to see which versions of the library were bring used during support requests.

@N1ghteyes
Copy link
Author

N1ghteyes commented Dec 10, 2021

@calcinai I hacked it in the end to return a set value (rather than checking against composer), so no not really a fix I'm afraid.

I suspect the actual fix here is to maintain a version constant that can be referenced - it'll just mean a manual update to it every time you push a minor version.

@ValCanBuild
Copy link
Contributor

ValCanBuild commented Jan 19, 2022

For reference, this is what I ended up doing (based on @N1ghteyes comment above). I created a custom \XeroPHP\Application subclass and overrode the constructor and just passed it a hardcoded version for the User-Agent string instead of having it use Helpers::getPackageVersion():

class CustomXeroApplication extends \XeroPHP\Application
{
    /** @noinspection MagicMethodsValidityInspection
     * @noinspection PhpMissingParentConstructorInspection
     */
    public function __construct($token, $tenantId)
    {
        $this->config = static::$_config_defaults;

        $transport = new Client([
            'headers' => [
                'User-Agent' => sprintf(static::USER_AGENT_STRING, 'v2.4.1'),
                'Authorization' => sprintf('Bearer %s', $token),
                'Xero-tenant-id' => $tenantId,
            ],
        ]);

        $reflectionProperty = new \ReflectionProperty(\XeroPHP\Application::class, 'transport');
        $reflectionProperty->setAccessible(true);
        $reflectionProperty->setValue($this, $transport);
    }
}

@calcinai
Copy link
Owner

@ValCanBuild I think the best way to do this is just to add a file as part of the build pipeline (and the tag). Just not completely sure how this would work with composer releases in terms of keeping the git tag consistent with the version in the code.

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

No branches or pull requests

4 participants