Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

path.relative(dir, dir) returns empty string #4826

Copy link
Copy link
@eush77

Description

@eush77
Issue body actions

path.relative returns empty string when its from and to arguments are identical.

> path.relative(dir, dir)
''

Since '' is not a valid path, this can blow up:

function ls (dir) {
  dir = path.relative(process.cwd(), dir);
  console.log(dir);
  // ...

  console.log(fs.readdirSync(dir));
}

This function ls works most of the time (nonexistent files and permissions aside), but not if dir points to the working directory:

> ls('.')

Error: ENOENT: no such file or directory, scandir ''
    at Error (native)
    at Object.fs.readdirSync (fs.js:856:18)
    at ls (repl:6:16)

I find the fact that path.relative returns a valid path, except in one corner case when the two arguments are identical, rather inconvenient.

However, this is probably intentional since there is a test for this introduced in #2106. But I haven't found any reasoning for this, and the reasoning of #2106 itself is not (directly) applicable here: it's one thing to defensively treat empty strings as '.', and the other to return an empty string instead of a valid path.

Expected bahavior:

> path.relative(dir, dir)
'.'

Metadata

Metadata

Assignees

No one assigned

    Labels

    pathIssues and PRs related to the path subsystem.Issues and PRs related to the path subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.