Why is expanduser not used in abspath

I noticed that the tilde symbol is not handled by abspath. What function should I use to expand a path shorthand?

julia> expanduser("~/.julia")
"/home/michael/.julia"

julia> abspath("~/.julia")
"/home/michael/~/.julia"

A quick workaround would be :

julia> fullpath = abspath∘expanduser
abspath ∘ expanduser

julia> fullpath("~/.julia")
"/home/michael/.julia"
1 Like

Why is expanduser not used in abspath

~ is a valid directory name.

6 Likes

I need to question from where the ~ comes. If it comes from string literals inside the code, then ok. But if you are doing this to strings that come from the user you can make impossible to refer to a folder/file called ~.

That makes it quite tricky then. :thinking:

The problem I had was something like this.

julia> dir = "~/.julia"
"~/.julia"

julia> isdir(dir)
false

julia> isdir(expanduser(dir))
true

julia> isdir(abspath(dir))
false

Maybe something like this would work

function fullpath(path)
  if ispath(path)
    abspath(path)
  else 
    alt_path = expanduser(path)
    ispath(alt_path) ? alt_path : abspath(path)
  end
end

abspath uses isabspath to check whether a path is already absolute, which simply checks whether the path starts with /, it doesn’t do any checks about the existence of the path on disk. A path starting with ~/... is a legitimate non-absolute path.

3 Likes