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

Inconsistent behavior of path.basename(path, ext) #21358

Open
ChALkeR opened this issue Jun 15, 2018 · 7 comments
Open

Inconsistent behavior of path.basename(path, ext) #21358

ChALkeR opened this issue Jun 15, 2018 · 7 comments
Labels
discuss Issues opened for discussions and feedbacks. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. path Issues and PRs related to the path subsystem.

Comments

@ChALkeR
Copy link
Member

ChALkeR commented Jun 15, 2018

I believe this was introduced somewhere in #5123, which changed the behavior of the ext argument.

This is observed on all supported branches and was even recently backported to 4.x.

Documentation:
https://nodejs.org/api/path.html#path_path_basename_path_ext

Observe the input and try to predict the output:

> ['a', 'a/', 'a//'].map(x => path.posix.basename(x))
[ 'a', 'a', 'a' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'b'))
[ 'a', 'a', 'a' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'a'))
[ '', 'a', 'a' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'a/'))
[ 'a', '', 'a' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'a//'))
[ 'a', 'a', '' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'aa'))
[ 'a', 'a/', 'a//' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'bb'))
[ 'a', 'a', 'a' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'aaa'))
[ 'a', 'a', 'a//' ]
> ['a', 'a/', 'a//'].map(x => path.posix.basename(x,'aaaa'))
[ 'a', 'a', 'a' ]
> ['dd', '/dd', 'd/dd', 'd/dd/'].map(x => path.posix.basename(x))
[ 'dd', 'dd', 'dd', 'dd' ]
> ['dd', '/dd', 'd/dd', 'd/dd/'].map(x => path.posix.basename(x, 'd'))
[ 'd', 'd', 'd', 'd' ]
> ['dd', '/dd', 'd/dd', 'd/dd/'].map(x => path.posix.basename(x, 'dd'))
[ '', 'dd', 'dd', 'dd' ]
> ['dd', '/dd', 'd/dd', 'd/dd/'].map(x => path.posix.basename(x, 'ddd'))
[ 'dd', 'dd', 'dd', 'dd/' ]

There are more, but all the inconsistencies with the previous behavior involve at least one of those:

  1. Either the path ends with /,
  2. Or ext includes /,
  3. Or ext equals to the actual resolved basename (i.e. path.endsWith('/' + ext)).

More specifically, the following check covers all the cases inconsistent behavior to my knowledge:
path.endsWith('/') || ext.includes('/') || path.endsWith('/' + ext)
(note that it also includes cases of consistent behavior).


Reminder: before #5123, this was how ext behave:

if (ext && f.substr(-1 * ext.length) === ext) {
    f = f.substr(0, f.length - ext.length);
}

I.e. it just sliced off the suffix (after doing everything else).

@ChALkeR ChALkeR added the path Issues and PRs related to the path subsystem. label Jun 15, 2018
@ChALkeR ChALkeR changed the title Strange behavior of path.basename(path, ext) with path separators Inconsistent behavior of path.basename(path, ext) with path separators Jun 16, 2018
@ChALkeR ChALkeR changed the title Inconsistent behavior of path.basename(path, ext) with path separators Inconsistent behavior of path.basename(path, ext) Jun 16, 2018
@ChALkeR
Copy link
Member Author

ChALkeR commented Jun 16, 2018

Note: the above don't come from some actual usecase that is broken by this, I used a bruteforce script that compares the old impl and the new impl outputs to find this.

@Trott
Copy link
Member

Trott commented Jun 16, 2018

My inclination (without thinking about it too much and possibly being ignorant of some history here) would be to compare the results to that of the basename CLI tool and consider any differences a bug. (Of course, that assumes that there aren't significant differences in basename implementation on different POSIX platforms and whatnot.)

@ChALkeR
Copy link
Member Author

ChALkeR commented Jun 16, 2018

@Trott In #7519, it was decided that path.basename shouldn't have the exact same behavior as basename(1).

Also, the cases metioned here are unlikely to be met in the «good» code path and use-case, and I don't remember any reports about cases of actual breakage due to #5123 which (as I believe) initially introduced the inconsistencies metioned here.

Converting to basename(1), on the other hand, to my estimation would be a significant breaking change as it might affect widely used cases. E.g. basename('/') output differs from what basename / gives.

/cc @bnoordhuis

@ChALkeR
Copy link
Member Author

ChALkeR commented Jun 21, 2018

/cc @mscdex @MylesBorins

@ChALkeR
Copy link
Member Author

ChALkeR commented Jul 24, 2018

/cc @nodejs/collaborators, thoughts?

@ryzokuken
Copy link
Contributor

@ChALkeR any updates?

@ChALkeR
Copy link
Member Author

ChALkeR commented Oct 28, 2018

@ryzokuken I didn't get any other comments to this, I think.
I'm still waiting a reply whether there is some rationale behind this change.

@jasnell jasnell added discuss Issues opened for discussions and feedbacks. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. labels Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issues opened for discussions and feedbacks. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. path Issues and PRs related to the path subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants