box-project / box

📦🚀 Fast, zero config application bundler with PHARs.
https://box-project.github.io/box
MIT License
1.14k stars 100 forks source link

Laravel cannot create phar Could not open input file: artisan #1001

Closed khanakia closed 1 year ago

khanakia commented 1 year ago

Bug report

Question Answer
Box version Box version 4.3.8@5534406
PHP version PHP 8.2.4
Platform with version MacOS
Github Repo Just any laravel repo
box.json.dist ```json { "main": "public/index.php", "chmod": "0755", "exclude-composer-files": false } ```
Output ```bash $ box compile > output ? Removing the existing PHAR "/test/public/index.phar" ? Checking Composer compatibility > Supported version detected ? No compactor to register ? Adding main file: /test/public/index.php ? Adding requirements checker ? Adding binary files > No file found ? Auto-discover files? Yes ? Exclude dev files? Yes ? Adding files > 7931 file(s) ? Generating new stub - Using shebang line: #!/usr/bin/env php - Using banner: > Generated by Humbug Box 4.3.8@5534406. > > @link https://github.com/humbug/box ? Dumping the Composer autoloader In ComposerOrchestrator.php line 207: Could not dump the autoloader. In ComposerOrchestrator.php line 210: The command "'/usr/local/bin/composer' 'dump-autoload' '--classmap-authoritative' '--no-dev' '--ansi'" failed. Exit Code: 1(General error) Working directory: /private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56685 Output: ================ Generating optimized autoload files (authoritative) Could not open input file: artisan Error Output: ================ > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1 compile [--debug] [--no-parallel] [--no-restart] [--dev] [--no-config] [--with-docker] [--composer-bin COMPOSER-BIN] [--allow-composer-check-failure] [-c|--config CONFIG] [-d|--working-dir WORKING-DIR] ```

It should compile and create a phar from laravel project but it do not

Screenshot 2023-06-09 at 4 13 08 PM

theofidry commented 1 year ago

Could it be that your artisan file is not included? You can verify that by using the --debug option and inspect the dumped directory.

Alternatively I would need to check what LaravelZero do to make it work.

khanakia commented 1 year ago

I have added a fresh laravel installation and i have not changed a single file. The only file i created is box.json in project root

i have uploaded the code here https://github.com/khanakia/box_test

You can check here's the debug output

DEBUG OUTPUT ```sh box compile --debug [debug] Current memory limit: "512MB" [debug] Checking BOX_ALLOW_XDEBUG [debug] phar.readonly is enabled [debug] The Xdebug extension is not loaded [debug] Configured `phar.readonly=0` [debug] Process restarting (BOX_ALLOW_XDEBUG=internal||1|*|*) [debug] Running /opt/homebrew/Cellar/php/8.2.7/bin/php /Users/khanakia/.composer/vendor/bin/box compile --debug [debug] Current memory limit: "512MB" [debug] Checking BOX_ALLOW_XDEBUG [debug] Restarted (43 ms). The Xdebug extension is not loaded ____ / __ )____ _ __ / __ / __ \| |/_/ / /_/ / /_/ /> < /_____/\____/_/|_| Box version 4.3.8@5534406 // Loading the configuration file "/Users/khanakia/Downloads/blog/box.json". 🔨 Building the PHAR "/Users/khanakia/Downloads/blog/public/index.phar" [debug] Increased the maximum number of open file descriptors from ("256", "unlimited") to ("2176", "unlimited") ? Checking Composer compatibility > '/usr/local/bin/composer' '--version' > 2.6-dev+5a96e6ae1335144a8944622820f921a34711731e (Box requires ^2.2.0) > Supported version detected ? No compactor to register ? Adding main file: /Users/khanakia/Downloads/blog/public/index.php ? Adding requirements checker ? Adding binary files > No file found ? Auto-discover files? Yes ? Exclude dev files? Yes ? Adding files > 5071 file(s) ? Generating new stub - Using shebang line: #!/usr/bin/env php - Using banner: > Generated by Humbug Box 4.3.8@5534406. > > @link https://github.com/humbug/box ? Dumping the Composer autoloader > '/usr/local/bin/composer' 'dump-autoload' '--classmap-authoritative' '--no-dev' '-vvv' '--ansi' [debug] Restored the maximum number of open file descriptors In ComposerOrchestrator.php line 207: [RuntimeException] Could not dump the autoloader. Exception trace: at /Users/khanakia/.composer/vendor/humbug/box/src/Composer/ComposerOrchestrator.php:207 KevinGH\Box\Composer\ComposerOrchestrator::dumpAutoloader() at /Users/khanakia/.composer/vendor/humbug/box/src/Composer/ComposerOrchestrator.php:117 KevinGH\Box\Composer\ComposerOrchestrator::dumpAutoload() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:664 KevinGH\Box\Console\Command\Compile::KevinGH\Box\Console\Command\{closure}() at /Users/khanakia/.composer/vendor/humbug/box/src/Box.php:136 KevinGH\Box\Box->endBuffering() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:662 KevinGH\Box\Console\Command\Compile::commit() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:269 KevinGH\Box\Console\Command\Compile->createPhar() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:226 KevinGH\Box\Console\Command\Compile->execute() at /Users/khanakia/.composer/vendor/fidry/console/src/Command/SymfonyCommand.php:93 Fidry\Console\Command\SymfonyCommand->execute() at /Users/khanakia/.composer/vendor/symfony/console/Command/Command.php:312 Symfony\Component\Console\Command\Command->run() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:1022 Symfony\Component\Console\Application->doRunCommand() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:314 Symfony\Component\Console\Application->doRun() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:168 Symfony\Component\Console\Application->run() at /Users/khanakia/.composer/vendor/fidry/console/src/Application/ApplicationRunner.php:66 Fidry\Console\Application\ApplicationRunner->run() at /Users/khanakia/.composer/vendor/humbug/box/bin/box:60 include() at /Users/khanakia/.composer/vendor/bin/box:119 In ComposerOrchestrator.php line 210: [Symfony\Component\Process\Exception\ProcessFailedException] The command "'/usr/local/bin/composer' 'dump-autoload' '--classmap-authoritative' '--no-dev' '-vvv' '--ansi'" failed. Exit Code: 1(General error) Working directory: /private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177 Output: ================ Generating optimized autoload files (authoritative) Could not open input file: artisan Error Output: ================ Running 2.6-dev+5a96e6ae1335144a8944622820f921a34711731e (2023-05-23 21:49:53) with PHP 8.2.7 on Darwin / 22.5.0 Reading ./composer.json (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177/composer.json) Loading config file ./composer.json (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177/composer.json) Checked CA file /opt/homebrew/etc/ca-certificates/cert.pem: valid Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): 'git' 'branch' '-a' '--no-color' '--no-abbrev' '-v' Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): git describe --exact-match --tags Executing command (CWD): git --version Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): git log --pretty="%H" -n1 HEAD --no-show-signature Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): hg branch Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): fossil branch list Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): fossil tag list Executing command (/private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177): svn info --xml Reading /Users/khanakia/.composer/composer.json Loading config file /Users/khanakia/.composer/composer.json (/Users/khanakia/.composer/composer.json) Reading /private/var/folders/qk/jzsg52995wn53dv8bw62dl1w0000gn/T/box/Box56177/vendor/composer/installed.json Reading /Users/khanakia/.composer/vendor/composer/installed.json > post-autoload-dump: Illuminate\Foundation\ComposerScripts::postAutoloadDump > post-autoload-dump: @php artisan package:discover --ansi Executing command (CWD): '/opt/homebrew/Cellar/php/8.2.7/bin/php' -d allow_url_fopen='1' -d disable_functions='' -d memory_limit='1536M' artisan package:discover -- ansi Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1 Exception trace: at /Users/khanakia/.composer/vendor/humbug/box/src/Composer/ComposerOrchestrator.php:210 KevinGH\Box\Composer\ComposerOrchestrator::dumpAutoloader() at /Users/khanakia/.composer/vendor/humbug/box/src/Composer/ComposerOrchestrator.php:117 KevinGH\Box\Composer\ComposerOrchestrator::dumpAutoload() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:664 KevinGH\Box\Console\Command\Compile::KevinGH\Box\Console\Command\{closure}() at /Users/khanakia/.composer/vendor/humbug/box/src/Box.php:136 KevinGH\Box\Box->endBuffering() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:662 KevinGH\Box\Console\Command\Compile::commit() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:269 KevinGH\Box\Console\Command\Compile->createPhar() at /Users/khanakia/.composer/vendor/humbug/box/src/Console/Command/Compile.php:226 KevinGH\Box\Console\Command\Compile->execute() at /Users/khanakia/.composer/vendor/fidry/console/src/Command/SymfonyCommand.php:93 Fidry\Console\Command\SymfonyCommand->execute() at /Users/khanakia/.composer/vendor/symfony/console/Command/Command.php:312 Symfony\Component\Console\Command\Command->run() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:1022 Symfony\Component\Console\Application->doRunCommand() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:314 Symfony\Component\Console\Application->doRun() at /Users/khanakia/.composer/vendor/symfony/console/Application.php:168 Symfony\Component\Console\Application->run() at /Users/khanakia/.composer/vendor/fidry/console/src/Application/ApplicationRunner.php:66 Fidry\Console\Application\ApplicationRunner->run() at /Users/khanakia/.composer/vendor/humbug/box/bin/box:60 include() at /Users/khanakia/.composer/vendor/bin/box:119 compile [--debug] [--no-parallel] [--no-restart] [--dev] [--no-config] [--with-docker] [--composer-bin COMPOSER-BIN] [--allow-composer-check-failure] [-c|--config CONFIG] [-d|--working-dir WORKING-DIR] [debug] Restarted process exited 1 ```
netbrothers-tr commented 1 year ago

@khanakia, I cloned your repo and tried it with Box 4.3.8 and had no problems. I'm on an Ubuntu box.

khanakia commented 1 year ago

@netbrothers-tr Did you run composer install command first? It seems you compiled without vendor directory.

netbrothers-tr commented 1 year ago

@netbrothers-tr Did you run composer install command first? It seems you compiled without vendor directory.

@khanakia, I actually didn't and now I'm wondering how this even worked. ;-) Thanks for the hint.

However, I ran box again, this time with all dependencies installed. It turns out, the line "@php artisan package:discover --ansi" in the "scripts" > "post-autoload-dump" section of the composer file cannot be executed in box context, because it is run from some other directory.

You can fix this in two ways:

  1. Remove the line in your composer.json. It probably isn't need at compile time.
  2. Adjust the line, such that the artisan command has the full path like so for example: "@php /path/to/project/artisan package:discover --ansi"

@theofidry, is there something you could tweak using the --working-dir= composer option?

khanakia commented 1 year ago

@netbrothers-tr Also how can i fix the file path issues ?

Basically, i trying to phar the whole laravel application and want be able to run it.

PHP Warning:  require_once(phar:///Users/test/Downloads/box_test-main/public/index.phar/public/../bootstrap/app.php): Failed to open stream: phar error: "bootstrap/app.php" is not a file in phar "/Users/test/Downloads/box_test-main/public/index.phar" in phar:///Users/test/Downloads/box_test-main/public/index.phar/public/index.php on line 47
netbrothers-tr commented 1 year ago

@khanakia, how did you get this warning? And some more questions:

If you are trying to build a command-line tool, consider using Laravel Zero instead.

khanakia commented 1 year ago

Basically, I was trying to protect the source code in some way to make it hard to be added new features. Anyway i see that's won't be possible this way. Thanks :smiley:

netbrothers-tr commented 1 year ago

Okay, now I see your motivation. Thanks for clarifying! Well, although it should be technically possible to create a Phar from a whole web application, it might become pretty difficult. If obfuscation is the main goal, there is quiet a number of tools out there, which do the job. To protect your code completely, ionCube comes to my mind, which is a commercial solution.