Elixir: Path.join unexpected behavior

Created on 12 Aug 2019  路  8Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.9.1 (compiled with Erlang/OTP 20)
  • Operating system: Ubuntu 18.04

Current behavior

Path.join("folder1/subfolder2/", "/")           
"folder1/subfolder2/."

Expected behavior

Path.join("folder1/subfolder2/", "/")           
"folder1/subfolder2/"

Dot, in the end, is weird and unexpected, is this bug?

Didn't find this behavior in documentation

All 8 comments

Yup, it is a bug. :) We should have a special case to handle when the right side is "/". A PR would be welcome!

To be clear, I think the result should be "folder1/subfolder2" as join removes trailing slashes.

Do you want to take a stab at this @SofaKing18? Could also look into it.

For the sake of completion, the issue also exists with join/1. E.g.

Path.join(["/foo", "bar", "/"]) -> "/foo/bar/."

I would like to make PR, but if @cybrox want - go for it :)

@SofaKing18 Ah, feel free to do so then. I'm just trying to pick up issues that the reporter does not PR themselves ;)

@josevalim Path.join/2 states _The right path will always be expanded to its relative format_, I feel a hint to this behaviour should be added to Path.join/1 as well. I think the following can be confusing?

iex(13)> Path.join(["", "/", "/usr/bin"])
"./usr/bin"

iex(14)> Path.join(["/", "/", "/usr/bin"])
"/./usr/bin"

Path.join/1 is implemented on top of Path.join/2 so fixing one should fix the other.

I know. Fixing the reported issue would fix the second example, the first one would still persist, though. Shouldn't an empty list element be ignored instead of making the whole path relative?

Considering the following works fine, it seems a bit odd that a leading empty element would have such a huge impact while an empty element anywhere else does not matter.

iex(7)> Path.join(["/usr", "", "", "/bin", "/", "share"])   
"/usr/bin/share"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmeiklejohn picture cmeiklejohn  路  3Comments

DEvil0000 picture DEvil0000  路  3Comments

vothane picture vothane  路  3Comments

andrewcottage picture andrewcottage  路  3Comments

Irio picture Irio  路  3Comments