Skip to content

Finder exclude not working as expected with absolute paths #25945

@theofidry

Description

@theofidry
Q A
Bug report? yes
Feature request? no
BC Break report? yes
RFC? no
Symfony version 4.0.3 (surely affects previous versions as well)

Given you have the following files:

/path/to/project
  \-- A/
        |-- a0
        |-- a1
        \-- B
             |-- b0
             \-- b1

The following script:

// /path/to/project/index.php
<?php declare(strict_types=1);

use Symfony\Component\Finder\Finder;

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

$finder = Finder::create()
    ->files()
    ->in(__DIR__)
    ->exclude(__DIR__.'/A/B')
;

// This is just to pretty print those files path
$files = array_values(
    array_map(
        function (SplFileInfo $fileInfo): string {
            $path = $fileInfo->getRealPath();

            return str_replace(__DIR__.DIRECTORY_SEPARATOR, '', $path);
        },
        iterator_to_array($finder)
    )
);

var_dump($files);

Will output:

- A/a0
- A/a1
- A/B/b0
- A/B/b1

Where I would have expected the files A/B/b0 and A/B/b1 to have been excluded.

If you however switch the exclude to relative paths, i.e. you change ->exclude(__DIR__.'/A/B') by ->exclude('A/B') this will work as expected.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions