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

Simplify the PHAR build script #656

Merged
merged 2 commits into from
May 15, 2018
Merged

Simplify the PHAR build script #656

merged 2 commits into from
May 15, 2018

Conversation

theofidry
Copy link
Contributor

I originally wanted to try if I was hitting a too many files open limit when compressing the PHAR. I cant' seem to reproduce it on my machine so I was looking for a real project where this was happening.

I did not hit the issue and simplified the build script:

  • Leverage PhpScoper from inside Box instead: it's faster
  • Simplify the Box config
  • Instead of relying on global dependencies (for example you won't be able to install php-scoper & box on mine), I prefer to leverage bamarni composer bin plugin. You can find a bit more of it's usage in its doc or the article I wrote.

The last step, running the psalm.phar PHAR does not work: I think I'm missing something obvious but can't figure out what.

Also not if you wish to see the files added to the PHAR, e.g. to debug the PHP-Scoper effect, you can:

  • use an IDE like PhpStorm which is able to index PHAR files and inspect them
  • use the --debug mode of compile which is going to dump the files added to the PHAR a directory .box

@coveralls
Copy link

coveralls commented Apr 8, 2018

Coverage Status

Coverage remained the same at 84.715% when pulling 2df9d2d on theofidry:master into 3119a1f on vimeo:master.

@muglug
Copy link
Collaborator

muglug commented Apr 9, 2018

I can't get the Phar to build at all on my work machine (4-year-old Macbook Pro). I get OOM errors.

@muglug
Copy link
Collaborator

muglug commented Apr 9, 2018

I'll try to get it building on my home machine too, maybe a bad config somewhere.

@theofidry
Copy link
Contributor Author

theofidry commented Apr 9, 2018 via email

[ -d build ] || mkdir build
[ -d build/psalm ] || mkdir build/psalm
# increase FD limit, or Phar compression will fail
ulimit -Sn 4096
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you encounter that failure or it was here for good measure?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen that, when building psalm.phar including dev dependencies.

Copy link
Collaborator

@weirdan weirdan May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I can reproduce it:

$ cd psalm
$ git checkout c13fda4034b33eb859e8810b10b0322fca17b858 
$ patch -p1 <<EOM
diff --git a/bin/build-phar.sh b/bin/build-phar.sh
index 1e05c796..7843dd05 100755
--- a/bin/build-phar.sh
+++ b/bin/build-phar.sh
@@ -1,8 +1,8 @@
 #!/usr/bin/env bash
 composer global require humbug/box:dev-master
-composer install --no-dev
+composer install # --no-dev
 [ -d build ] || mkdir build
-box build
+php -dphar.readonly=0 `which box` build
EOM
patching file bin/build-phar.sh
Hunk #1 succeeded at 1 with fuzz 2.
$ bin/build-phar.sh
[......snip......]
Box (repo)


 // Loading the configuration file "/home/weirdan/src/psalm/box.json".                                                  

Building the PHAR "/home/weirdan/src/psalm/build/psalm.phar"
? Registering compactors
  + KevinGH\Box\Compactor\Php
? Adding main file: /home/weirdan/src/psalm/psalm
? Adding binary files
    > No file found
? Adding files
    > 4496 file(s)
? Generating new stub
  - Using shebang line: #!/usr/bin/env php
  - Using banner:
    > Generated by Humbug Box.
    > 
    > @link https://github.com/humbug/box
? Compressing with the algorithm "BZ2"

In Compile.php line 415:
                                   
  unable to create temporary file  
                                   

build [-c|--config CONFIG] [--debug] [-d|--working-dir WORKING-DIR]
[......snip......]

The box version is 3.0.0-alpha.0 ( f5eec71df2df3a2a1f39a50ba77e8bb4312fef51 )
ulimit -n is set to 1024
Phar version is 2.0.2
PHP version is 7.2.4-1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in Box now

Copy link
Contributor Author

@theofidry theofidry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumped box and added some comments, ready for a first review :)

@@ -1,37 +1,6 @@
#!/usr/bin/env bash
if ! php -r 'extension_loaded("bz2") or exit(1);' ; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now handled in Box


composer global require 'humbug/php-scoper:^1.0@dev'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been replaced by bamarni/composer-bin-plugin, up to you if you're cool with that or you prefer the composer global or maybe switch to a wget the Box PHAR

[ -d build ] || mkdir build
[ -d build/psalm ] || mkdir build/psalm
# increase FD limit, or Phar compression will fail
ulimit -Sn 4096
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in Box now

rm -f bin/psalm.phar

# Prefixes the code to be bundled
php -d memory_limit=-1 `which php-scoper` add-prefix --prefix='PsalmPhar' --output-dir=build/psalm --force
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scoping is done via Box now, the only issue is humbug/php-scoper#178 so right now the prefix is really random

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the memory is also bumped by Box although the number is quite arbitrary so it may need more

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deterministic prefix was on purpose to enable better cache re-use, but I don't really mind caches being busted between new phars.


# Re-dump the loader to account for the prefixing
# and optimize the loader
composer dump-autoload --working-dir=build/psalm --classmap-authoritative --no-dev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

including only the non-dev files (and avoiding to scope the dev files) is done by Box

"chmod" : "0755",
"shebang" : "#!/usr/bin/env php",
"stub" : true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default value already

"chmod" : "0755",
"shebang" : "#!/usr/bin/env php",
"stub" : true
"compression": "GZ"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was no compression, not sure if that was voluntary because you wanted to avoid your users to have the zlib extension

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely on purpose (because bzip was causing issues for users without it). If 99% of PHP installs have zlib, happy to use that.

return [
'finders' => [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part (finding the files) is handled by Box

@@ -136,4 +115,7 @@ function ($filePath, $prefix, $contents) {
return $contents;
},
],
'whitelist' => [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whitelist is useless so could be removed

src/psalm.php Outdated
@@ -127,7 +127,7 @@
$options['r'] = $options['root'];
}

$current_dir = (string)getcwd() . DIRECTORY_SEPARATOR;
$current_dir = (string)getcwd();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure why we have the trailing /. This was making fail the find autoloader since we ended up with // in the path

@theofidry
Copy link
Contributor Author

@muglug in theory this should be ready. I think however some code changed and maybe the PHP-Scoper config needs to be updated.

@theofidry
Copy link
Contributor Author

Updated and the builds are green.

I may have missed them, but if they is no e2e tests for the PHAR I would recommend to add one (in another PR)

@muglug muglug merged commit 1ecf1c4 into vimeo:master May 15, 2018
@muglug
Copy link
Collaborator

muglug commented May 15, 2018

Thanks a lot!

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

Successfully merging this pull request may close these issues.

None yet

4 participants