Skip to content

Commit

Permalink
Merge pull request #130 from clue-labs/windows-rename
Browse files Browse the repository at this point in the history
Improve Windows support by retrying rename to fix slow network drives
  • Loading branch information
SimonFrings authored Feb 14, 2022
2 parents c5232dc + 6bd159a commit 8fc98b1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
- 5.3
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
- run: composer install
- run: vendor/bin/phpunit --coverage-text
if: ${{ matrix.php >= 7.3 }}
Expand All @@ -45,4 +45,5 @@ jobs:
if: ${{ matrix.php >= 7.1 && matrix.php < 8.0 }}
- run: composer require symfony/console:^3.0 --dry-run --working-dir=tests/install-as-dep
if: ${{ matrix.php >= 5.5 && matrix.php < 8.0 }}
- run: composer install --dry-run --working-dir=tests/install-as-dep
- run: cd tests/install-as-dep && composer install && php query.php
- run: cd tests/install-as-dep && php -d phar.readonly=0 vendor/bin/phar-composer build . query.phar && php query.phar
9 changes: 7 additions & 2 deletions src/Phar/PharComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public function build()
// failure to write will emit a warning (ignore) and throw an (uncaught) exception
try {
@$targetPhar->stopBuffering();
$targetPhar = null;
} catch (\PharException $e) {
throw new \RuntimeException('Unable to write phar: ' . $e->getMessage());
}
Expand All @@ -223,8 +224,12 @@ public function build()
$this->log(' - Overwriting existing file <info>' . $target . '</info> (' . $this->getSize($target) . ')');
}

if (rename($tmp, $target) === false) {
throw new \UnexpectedValueException('Unable to rename temporary phar archive to "'.$target.'"');
if (@rename($tmp, $target) === false) {
// retry renaming after sleeping to give slow network drives some time to flush data
sleep(5);
if (rename($tmp, $target) === false) {
throw new \UnexpectedValueException('Unable to rename temporary phar archive to "'.$target.'"');
}
}

$time = max(microtime(true) - $time, 0);
Expand Down
3 changes: 3 additions & 0 deletions tests/install-as-dep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/query.phar
/vendor/
8 changes: 7 additions & 1 deletion tests/install-as-dep/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
"require-dev": {
"clue/phar-composer": "*@dev"
},
"bin": [
"query.php"
],
"repositories": [
{
"type": "path",
"url": "../../"
"url": "../../",
"options": {
"symlink": false
}
}
]
}
5 changes: 5 additions & 0 deletions tests/install-as-dep/query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require __DIR__ . '/vendor/autoload.php';

echo 'Answer: 42' . PHP_EOL;

0 comments on commit 8fc98b1

Please sign in to comment.