symfony: [Runtime] Not compatible with PHARs

Symfony version(s) affected

6.2.6

Description

This is a follow up of #41771, which already reported the problem. The solution offered there was to use an alternative runtime template but I think the Symfony one should work out of the box with PHARs.

Related Box issues:

Bundling a standard Symfony app into a PHAR using the default runtime component results in:

PHP Warning:  Constant  already defined in /home/imper/projects/phartest/build/app.phar on line 16
PHP Fatal error:  Uncaught TypeError: Invalid return value: callable object expected, "int" returned from "./build/app.phar". in phar:///home/imper/projects/phartest/build/app.phar/vendor/autoload_runtime.php:12
Stack trace:
#0 phar:///home/imper/projects/phartest/build/app.phar/bin/console(10): require_once()
#1 /home/imper/projects/phartest/build/app.phar(14): require('...')
#2 {main}
  thrown in phar:///home/imper/projects/phartest/build/app.phar/vendor/autoload_runtime.php on line 12

How to reproduce

Checkout https://github.com/box-project/box/pull/886 and execute make e2e_symfony_runtime.

Alternatively:

  • Create a brand new standard Symfony app
  • Get your hand on a Box binary
  • Create the following Box config:
{
    "files-bin": [
        ".env.local.php",
        "src/Controller/.gitignore"
    ],
    "directories": [
        "config",
        "var"
    ],
    "force-autodiscovery": true,
    "check-requirements": false,
    "exclude-composer-files": false
}

The TL:DR; from this config is: nothing really fancy, include somehow the Symfony files that cannot be inferred by Composer.

And then, run:

$ box compile
$ ./bin/console.phar

Possible Solution

The solution suggested in #41771 is in my opinion not satisfactory. Indeed:

  • this requires a number of non-trivial steps in order to make it work
  • creating a new runtime template which actually no longer really fits the runtime component. As a consequence you do end up with something working for the PHAR, but outside the PHAR, you console script is no longer working with the runtime context. So you might as well just ditch the runtime component there IMO, it is simpler to fallback to the old console script.

I have checked the script and the issue is that somehow:

// autoload_runtime.php
$app = require $_SERVER['SCRIPT_FILENAME'];

ends up with $app = 1, but I just don’t get why, it requires the file correctly and the return value should be the callable (even when doing step by step debugging, you end up doing the right path). It’s as if the callable was suddenly magically mapped to 1 in the context of the PHAR.

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 20 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@llaville in the meantime I’ve discussed this with Theo and the general idea was to produce a PHAR-specific runtime: https://github.com/php-runtime/runtime/issues/135. The runtime will ship the autoload template with the bugfix since Symfony is unlikely to treat it as such, meaning the runtime should support older Symfony versions until they’re EOL’d.

Then it’s up to Box to integrate this runtime: https://github.com/box-project/box/issues/892

I think the first solution proposed https://github.com/symfony/symfony/pull/49536 was more of a bugfix, but now looks more like a feature. But for what it’s worth I’m happy either way as long one of those “fix” lands somehow

setting “autoload_template”: “autoload_runtime.template” is not documented

Doc missing I suppose. Can’t box generate some appropriate vendor/autoload_runtime.php in the phar it builds? Would that work?