Skip to content

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

Closed

Description

@eush77

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)
'.'

Activity

  1. added
    pathIssues and PRs related to the path subsystem.
    on Jan 23, 2016
  2. silverwind commented on Jan 24, 2016

    @silverwind
    Contributor

    I see what you mean, but '.' means 'current directory' to me, something which does not seem correct for me for arbitrary paths which are equal. It would only be correct when dir equals $PWD.

  3. eush77 commented on Jan 24, 2016

    @eush77
    Author

    I see what you mean, but '.' means 'current directory' to me, something which does not seem correct for me for arbitrary paths which are equal. It would only be correct when dir equals $PWD.

    That's not actually correct. Every directory has '.' and '..', and they can appear anywhere in the path:

    $ cd /tmp
    $ cd ../var/./opt/../../.
    $ pwd
    /
  4. zeusdeux commented on Jan 24, 2016

    @zeusdeux
    Contributor

    Shouldn't path.relative(dir, dir) just return dir?

  5. silverwind commented on Jan 24, 2016

    @silverwind
    Contributor

    Shouldn't path.relative(dir, dir) just return dir?

    No, that would imply there's another sub-directory like /dir/dir when dir = '/dir'.

    That's not actually correct. Every directory has '.' and '..', and they can appear anywhere in the path:

    Right, I see returning '.' has some uses, like process.chdir(path.relative(process.cwd(), 'dir')). Still, I'm uncomfortable with breaking documented behaviour.

  6. evanlucas commented on Jan 29, 2016

    @evanlucas
    Contributor

    This is documented at https://nodejs.org/dist/latest-v5.x/docs/api/path.html#path_path_relative_from_to.

    I'll go ahead and close. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pathIssues 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