common-lisphome-directorypathname

Expand a file name with a tilde to its fullpath (Common Lisp)


I have a directory name (as string) with a tilde: ~/projects.

I want to get its fullpath: /home/user/projects. How do I do that ?

The goal is to pass it to uiop:run-program, that doesn't seem to do the right thing©.


With this answer: How to translate (make-pathname :directory '(:absolute :home "directoryiwant") into absolute path

(merge-pathnames 
          (make-pathname
           :directory '(:relative "~/projects"))
          (user-homedir-pathname))
#P"/home/me/~/projects/"

=> WRONG

Thank you.


edit I'll share more context.

I wanted to run a program through uiop:launch-program. I had a user-defined list of directories such as ~/projects. Using it as is created the ./~/projects directory instead of /home/user/projects.

truename doesn't work if the directory doesn't exist.

On SBCL, (namestring "~/doesntexist") returns also its tilde.

merge-pathnames didn't work, still the tilde problem.

Feeding ensure-directories-exist with this result created a directory named ~.

Given the answers, I had no choice but to adapt the logic to expand the directory name of a directory we actually want to exist.

;; Create a directory
;; Ensure its name (string) ends with a slash.
(setf mydir
        (str:concat (string-right-trim (list #\/) mydir)
                    "/"))
(ensure-directories-exist base)

Then I could use its truename.


Solution

  • There is a native-namestring function in uiop, which should be available in all implementations:

    (uiop:native-namestring "~/projects")
    => /home/user/projects