Skip to content

Commit 1d0adc4

Browse files
authored
Do not crash when a broken symlink is encountered (#91)
"Broken symlink at {$relative_filepath}. Target missing at {$item->getLinkTarget()}."
1 parent 06dbfdb commit 1d0adc4

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

features/distignore.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,23 @@ Feature: Generate a distribution archive of a project
385385
| targz | tar.gz | tar -zxvf | foo |
386386
| zip | zip | unzip | bar7 |
387387
| targz | tar.gz | tar -zxvf | bar8 |
388+
389+
Scenario: Does not crash when a broken symlink is encountered
390+
# @see https://github.com/wp-cli/dist-archive-command/issues/86
391+
Given an empty directory
392+
And an empty foo/target-directory directory
393+
And a foo/.distignore file:
394+
"""
395+
"""
396+
397+
When I run `ln -s {RUN_DIR}/foo/target-directory {RUN_DIR}/foo/symlink`
398+
Then STDERR should be empty
399+
400+
When I run `rm -rf {RUN_DIR}/foo/target-directory`
401+
Then STDERR should be empty
402+
403+
When I try `wp dist-archive foo`
404+
Then STDERR should contain:
405+
"""
406+
Error: Broken symlink at /symlink. Target missing at
407+
"""

src/Dist_Archive_Command.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,18 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
484484
*/
485485
foreach ( $iterator as $item ) {
486486
$relative_filepath = str_replace( $source_dir_path, '', $item->getPathname() );
487-
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
488-
$excluded_files[] = $relative_filepath;
489-
} else {
490-
$included_files[] = $relative_filepath;
487+
try {
488+
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
489+
$excluded_files[] = $relative_filepath;
490+
} else {
491+
$included_files[] = $relative_filepath;
492+
}
493+
} catch ( \Inmarelibero\GitIgnoreChecker\Exception\InvalidArgumentException $exception ) {
494+
if ( $item->isLink() && ! file_exists( readlink( $item->getPathname() ) ) ) {
495+
WP_CLI::error( "Broken symlink at {$relative_filepath}. Target missing at {$item->getLinkTarget()}." );
496+
} else {
497+
WP_CLI::error( $exception->getMessage() );
498+
}
491499
}
492500
}
493501

0 commit comments

Comments
 (0)