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

[Filesystem] Fix makePathRelative when the original path does not end with a slash #40051

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,11 @@ public function makePathRelative($endPath, $startPath)

$startPathArr = $splitPath($startPath);
$endPathArr = $splitPath($endPath);
$pathEnd = '/' === substr($endPath, -1) ? '/' : '';

if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) {
// End path is on another drive, so no relative path exists
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : '');
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).$pathEnd : '');
}

// Find for which directory the common path stops
Expand All @@ -504,7 +505,7 @@ public function makePathRelative($endPath, $startPath)
$endPathRemainder = implode('/', \array_slice($endPathArr, $index));

// Construct $endPath from traversing to the common path, then to the remaining $endPath
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.$pathEnd : '');

return '' === $relativePath ? './' : $relativePath;
}
Expand Down
38 changes: 19 additions & 19 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,32 +1114,32 @@ public function providePathsForMakePathRelative()
['/aa/bb/cc', '/aa/bb/cc/dd/', '../'],
['/aa/bb/cc/', '/aa/bb/cc/dd', '../'],
['/aa/bb/cc/', '/aa/bb/cc/dd/', '../'],
['/aa/bb/cc', '/aa', 'bb/cc/'],
jderusse marked this conversation as resolved.
Show resolved Hide resolved
['/aa/bb/cc', '/aa/', 'bb/cc/'],
['/aa/bb/cc', '/aa', 'bb/cc'],
['/aa/bb/cc', '/aa/', 'bb/cc'],
['/aa/bb/cc/', '/aa', 'bb/cc/'],
['/aa/bb/cc/', '/aa/', 'bb/cc/'],
['/a/aab/bb', '/a/aa', '../aab/bb/'],
['/a/aab/bb', '/a/aa/', '../aab/bb/'],
['/a/aab/bb', '/a/aa', '../aab/bb'],
['/a/aab/bb', '/a/aa/', '../aab/bb'],
['/a/aab/bb/', '/a/aa', '../aab/bb/'],
['/a/aab/bb/', '/a/aa/', '../aab/bb/'],
['/a/aab/bb/', '/', 'a/aab/bb/'],
['/a/aab/bb/', '/b/aab', '../../a/aab/bb/'],
['/aab/bb', '/aa', '../aab/bb/'],
['/aab', '/aa', '../aab/'],
['/aa/bb/cc', '/aa/dd/..', 'bb/cc/'],
['/aa/../bb/cc', '/aa/dd/..', '../bb/cc/'],
['/aa/bb/../../cc', '/aa/../dd/..', 'cc/'],
['/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'],
['/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'],
['C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
['C:/aa/bb/cc', 'c:/aa/dd/..', 'bb/cc/'],
['c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'],
['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'],
['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'],
['/aab/bb', '/aa', '../aab/bb'],
['/aab', '/aa', '../aab'],
['/aa/bb/cc', '/aa/dd/..', 'bb/cc'],
['/aa/../bb/cc', '/aa/dd/..', '../bb/cc'],
['/aa/bb/../../cc', '/aa/../dd/..', 'cc'],
['/../aa/bb/cc', '/aa/dd/..', 'bb/cc'],
['/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc'],
['C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc'],
['C:/aa/bb/cc', 'c:/aa/dd/..', 'bb/cc'],
['c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc'],
['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc'],
['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc'],
['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc'],
['D:/', 'C:/aa/../bb/cc', 'D:/'],
['D:/aa/bb', 'C:/aa', 'D:/aa/bb/'],
['D:/../../aa/../bb/cc', 'C:/aa/dd/..', 'D:/bb/cc/'],
['D:/aa/bb', 'C:/aa', 'D:/aa/bb'],
['D:/../../aa/../bb/cc', 'C:/aa/dd/..', 'D:/bb/cc'],
];

if ('\\' === \DIRECTORY_SEPARATOR) {
Expand Down