Skip to content

[Finder] ignoreVCSIgnored can't handle comment lines #32985

@Jeroeny

Description

@Jeroeny

Symfony version(s) affected: 4.3, 4.4

Description
When using ignoreVCSIgnored as argument with the Symfony Finder, it will construct a regex equivalent of the gitignore pattern. However it seems that when a comment line (prefixed with #) is present in the .gitignore, the regex used to remove comment lines matches every line and thus returns $gitignoreFileContent as empty.

https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Finder/Gitignore.php

How to reproduce
At line 30 in Gitignore, the toRegex function will try to remove all lines that are comments. However when doing so, it seems to mismatch the lines and remove all lines. This only happens when there is at least one comment line present.

Code reproduction

$gitignoreFileContent = <<<GITIGNORE
# comment 

###> comment

/.idea/
/build/
/vendor/

# comment
GITIGNORE;

$result = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent);
$desiredResult = preg_replace('/^[^\\\r\n]*#.*/m', '', $gitignoreFileContent);

var_dump($result);
var_dump($desiredResult);

output:

string(0) ""
string(30) "



/.idea/
/build/
/vendor/

"

https://3v4l.org/bMQnW

Possible Solution
If I'm not doing anything wrong and this is indeed a bug, then we could fix this by adjusting the regular expression. Not sure if my given example regex (/^[^\\\r\n]*#.*/m) is the most beautiful solution, but I would gladly make a pull request.

Additional context

Example where no comment line is present

<?php

$gitignoreFileContent = <<<GITIGNORE


/.idea/
/build/
/vendor/

GITIGNORE;

$result = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent);
$desiredResult = preg_replace('/^[^\r\n]*#.*/m', '', $gitignoreFileContent);

var_dump($result);
var_dump($desiredResult);

output:

string(27) "

/.idea/
/build/
/vendor/
"
string(27) "

/.idea/
/build/
/vendor/
"

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