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] makePathRelative with existing files, remove ending / #47424

Open
wants to merge 2 commits into
base: 7.2
Choose a base branch
from

Conversation

petarmarj
Copy link

@petarmarj petarmarj commented Aug 29, 2022

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? yes?
Tickets
License MIT
Doc PR

Bug description: Filesystem::makePathRelative adds wrongly an ending slash / to a resulting relative path, although provided endPath points to existing file.  

Situation tl;dr:

$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$filesystem->makePathRelative(
    endPath: '/var/www/html/project/foo/bar/tmp/test.txt',
    startPath: '/var/www/html/project'
); // output: "foo/bar/tmp/test.txt/"

Discussion: I thought at first that the method should be applied only to folders. However, Filesystem provides operations for files and folders. The provided relative path is therefore unexpected. The problem was discussed in pull request #40051 without solving the issue because of the awareness of causing troubles in existing codebases. This issue exists because folder paths are occasionally defined with an ending slash. In the closed pull request, a solution was proposed that did not check if an endPath targets an existing file.

Deprecations: There should be no deprecations. Those who wanted to solve the described problem checked the unnecessary presence of the last slash.

Reproduce bug:

<?php

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

$filesystem = new \Symfony\Component\Filesystem\Filesystem();

// Init situation
$projectDir = __DIR__;
$tmpFolderPath = "{$projectDir}/foo/bar/tmp";
$tmpFilePath = "{$tmpFolderPath}/test.txt";

$filesystem->mkdir($tmpFolderPath, 0700);
$filesystem->touch($tmpFilePath);

// Output: foo/bar/tmp/test.txt/
// Expected output: foo/bar/tmp/test.txt
$relativePath = $filesystem->makePathRelative($tmpFilePath, $projectDir);

// Debug info
dump([
    'projectDir' => $projectDir, // ✔ "/var/www/html/project"
    'tmpFolderPath' => $tmpFolderPath, // ✔ "/var/www/html/project/foo/bar/tmp"
    'tmpFilePath' => $tmpFilePath, // ✔ "/var/www/html/project/foo/bar/tmp/test.txt"
    'relativePath' => $relativePath, // ✘ "foo/bar/tmp/test.txt/" – expected: "foo/bar/tmp/test.txt"
    'tmpFolderPath.isFile' => file_exists($tmpFolderPath), // ✔ true
    'tmpFolderPath.isFolder' => is_dir($tmpFolderPath), // ✔ true
    'tmpFilePath.isFile' => file_exists($tmpFilePath), // ✔ true
    'tmpFilePath.isFolder' => is_dir($tmpFilePath), // ✔ false
]);

Tests: Filesystem has no tests that are trying to test with files instead of folders. New tests have to be defined. My PHP knowledge is not sufficient to create these tests reliably.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.2 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@@ -669,7 +674,6 @@ public function dumpFile(string $filename, $content)
* Appends content to an existing file.
*
* @param string|resource $content The content to append
* @param bool $lock Whether the file should be locked when writing to it
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed by phpcs with Symfony standard.

@OskarStark
Copy link
Contributor

Filesystem has no tests that are trying to test with files instead of files.

The sentence is confusing

@petarmarj
Copy link
Author

Filesystem has no tests that are trying to test with files instead of files.

The sentence is confusing

Thanks. I meant folders. 😅

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants